Before users can start using any custom actions, you must add them through the admin menu. These can be found in the File actions
section, in the administration settings menu. On a fresh install, some ready-made actions should already be available (albeit disabled for the user).
Other than the selected files, you can ask the user to input other details as part of the action. Any number of additional input values can be added in the script-creation view. The values can be accessed in your script via the get_input()
function.
By default, input types are text fields where the user may type in the value. However, other more restrictive input types may be used:
- Checkbox allows for a boolean (
true
/false
) input from the user. - Multiselect allows the user to select from a set of pre-determined inputs.
- File-picker allows the user to choose a document from their Nextcloud files.
Note: to allow a user to pick a folder, thehttpd/unix-directory
mimetype must be added to the filepick options.
When adding script inputs, don't forget to save the script after making any additions.
User input fields will always be optional; the action may still be triggered even if the user did not input anything. However, additional checks can be performed on the script itself, and an error may be returned to the user if any required fields were not (correctly) filled.
local file_name = get_input('file_name')
if (not file_name or string.len(file_name) == 0) then
abort('File name was not filled in.')
end
Reading through the function descriptions and snippets, you can get an idea of what is possible to do with file actions.
You can also find example scripts in the examples/
folder of this repository. These examples will also be included when you first install the app on your server.
The scripting API has been extended with Nextcloud-specific functions. These functions may require a "Folder" or a "File" as an input. Lua of course does not know what a file or a folder are, so instead we rely on Lua tables to carry the information required to represent these objects.
These tables may be manually created, but it is preferable to use functions that return these objects.
The Node
type is used to represent a file or folder (see: is_file
and is_folder
), it can also be expanded with meta_data
to include more information:
id
: the internal ID of the file or foldername
: the name of the file or folderpath
: the path (not including the name) to the file or folder from the user's home folder
uuid
: the unique identifier for the userdisplay_name
: the chosen display name by the useremail_address
: the user's email address
id
: the unique (numeric) ID of the tagname
: the tag's name (as shown in the Nextcloud interface)user_assignable
: whether the tag can be assigned by usersuser_visible
whether the tag is made visible to the usersaccess_level
: internal access level code for the tag (0: public
,1: restricted
,2: invisible
)
If you are writing a new script and need to test it, I would highly advise using a test environment. This will avoid any mishaps from affecting any live data.
If, for any reason, you must test your scripts on a production server and do not want to make the scripts available to users yet, you can limit the script to the "admin" group, by ticking the Limit to groups
option.
Actions can also be configured to work with Nextcloud's automated flows. Note that disabled actions are available to be configured with Flow, so you can hide these from users.
When running a script from a flow, get_input_files()
will only contain the file that triggered the flow.
Additionally, for "file rename" and "file copy" events, the previous file's path can be accessed with: get_input('old_node_path')
.
In some cases, it might be desirable to run scripts via occ
(e.g. running scripts on cron-jobs). To do this, you can use the command:
occ files_scripts:run <id> --user <userid> --inputs "{ \"input_var\": \"hello world\" }"
To get the <id>
value, you can use the scripts-list command:
occ files_scripts:list
To get the <userid>
value, you can use the user-list command:
occ user:list