File actions
A file action is a command you write once and then run on whatever you have selected in the file browser. Choose Terminal → Manage File Actions…, write the command with a placeholder where the file goes, and it appears at the bottom of the right-click menu in the file browser.
Three things are worth understanding before you write one, and they are the three sections at the end of this page: the file names you see in a command come from the server, an action says which pane it works on, and a name that is not what it looks like is marked or refused.
Writing an action
Section titled “Writing an action”Terminal → Manage File Actions… lists the actions you have, in the order they appear in the menu. New… opens the editor.
| Field | What it is |
|---|---|
| Name | What the menu entry says. |
| Command | The command to run, with placeholders where the files go. |
| Runs | Once per file, or Once for the whole selection. |
| Where | Remote pane, Local pane, or both. |
| Variables | Values macSCP asks you for before the action runs. |
Below the command, a preview shows what the action would actually run, per pane, on a sample selection. If something about the action cannot work, the editor says so in red and Save stays switched off until you fix it.
Placeholders
Section titled “Placeholders”The Insert buttons offer the placeholders that fit the way the action runs. Which ones those are depends on Runs:
| Placeholder | Stands for | Available when |
|---|---|---|
{{PATH}} | The full path of the selected file | Once per file |
{{NAME}} | Just the file’s name, without the folders | Once per file |
{{DIR}} | The folder the selected files are in | Either |
{{PATHS}} | Every selected path at once | Once for the whole selection |
So wc -l {{PATH}} set to Once per file runs once for each file you selected, and zip -r out.zip {{PATHS}} set to Once for the whole selection runs once with all of them. Writing {{PATH}} in an action that runs once for the whole selection is refused, and the editor tells you to write {{PATHS}} instead.
Asking for a value first
Section titled “Asking for a value first”If an action needs something you decide at the time — a compression level, a mode, a suffix — add it under Variables. macSCP asks for the values before it plans the run, so what you confirm is what it will do. A variable cannot be called PATH, NAME, DIR or PATHS: those names already belong to the selected file.
Which pane an action works on
Section titled “Which pane an action works on”An action declares where it may run, and it only appears in the menu of a pane it declared.
- Remote pane — the command is typed into this connection’s shell, the way you would type it yourself, and you watch it run in the terminal. A connection without a shell — WebDAV or S3 storage — cannot run one, and macSCP says so instead of running it.
- Local pane — the command is run directly on your Mac. There is no shell on this side, so nothing interprets pipes, redirections or quotes, and the editor refuses an action that uses them while the local pane is switched on. Take them out, or switch the local pane off. A command of more than one line is refused whichever panes it declares, because a line break is one of the characters described under Names that are not what they look like — write one command, or make a second action.
An action can declare both. It is then offered in both panes, and the confirmation sheet says which of the two it is about each time.
One action runs at a time per pane on your Mac. If you start a second one while the first is still going, macSCP says so instead of queueing it; wait, or cancel the running one. If it fails, you get a message naming the action — macSCP does not show you what the program printed.
Every run is confirmed first
Section titled “Every run is confirmed first”Choosing an action from the menu never runs it. A sheet opens first, headed Run action, and says:
This is what will run. The file names come from the server, so read them before you continue.
Under that is the command exactly as it will run, line by line, with the real file names filled in — and a line saying where it goes, either Typed into the shell of this connection. or Run directly on this Mac, with no shell. Run starts it; Cancel does nothing at all.
This sheet is the point of the whole feature, and it is worth being deliberate about it. The names in those lines are not names you typed. They came from the folder listing the server sent, so a server you do not control chose them, and this sheet is where you read what they do to your command before it runs. If a line does not look like what you meant, cancel.
Names that are not what they look like
Section titled “Names that are not what they look like”A file name can contain characters that take no visible space of their own — a character that reverses the direction of the text, a zero-width character, a line break, or an invisible control character. A name like that can make a command look harmless when it is not.
macSCP does two things about it:
- It shows them. Anywhere a file name or an action name appears — the confirmation sheet, the menu entry, the messages — a character of those kinds is drawn as a marker like
<U+200E>instead of being left invisible. The sheet highlights the markers, so they stand out from the rest of the line. What is marked is the invisible ones: control characters, the text-direction and zero-width formatting characters, and line and paragraph breaks. The characters that join an emoji together are not marked, because an ordinary name carries them and markers you learn to ignore are worse than none. A character that draws as something else — a letter from another script that looks like a Latin one, an accent stacked on top of its neighbour — is not marked either: it is visible, so you are reading it, and the sheet shows the name exactly as it is. - It refuses some of them. A name holding an ASCII control character — every code point below a space, which includes tab and line break, plus delete: 33 in all — cannot be put into a command that is typed into the server’s shell, because such a character would act on the input line whatever quotes are around it. macSCP will not run the action on that file, and the file has to be renamed on the server before the action can reach it. The refusal is that range and no wider; for anything else, the confirmation sheet is where you check what your command became. The same range is refused in the command you write, so an action — your own or one you imported — cannot carry one either.
Spaces, quotes and brackets are ordinary here: macSCP quotes them, so a name such as my file 'draft'.txt reaches the command as one single name.
A name that is not plain ASCII — accents, other scripts, emoji — is quoted the same way and passed through unchanged, with nothing added and nothing removed. How a particular server’s shell reads such a character is not something macSCP can promise for every server, so the confirmation sheet is again the place to look: what it shows is what will be sent.
Sharing actions
Section titled “Sharing actions”The ⋯ button in the File Actions window exports your actions to a .macscpactions file, and imports one. Snippets and file actions are kept apart: an export of one is never readable as the other, and macSCP says so rather than importing something odd.
An import never overwrites what you have. Every action in the file is checked the way the editor checks one, and the result tells you how many were imported, how many were already there, and which ones were refused and why.