Sub topic 7
Receiving files and text with Shortcut Input
This chapter assumes you can build and run a multi-step shortcut. You should know what an action is, how a magic variable holds an action's output, and how to open the shortcut editor. Basic Finder use is enough. Terminal knowledge is optional and only matters for the shell script example near the end.
By the end you will be able to:
- Say what Shortcut Input is and where it comes from.
- Configure which kinds of content a shortcut accepts, and what happens when none arrives.
- Handle one item or many items in the same shortcut.
- Pass the input to a shell script.
What Shortcut Input is
Shortcut Input is the content a shortcut receives from outside when it starts. The content can be files, folders, selected text, images, URLs or other items. It is not something you create inside the shortcut. Another app or macOS hands it to the shortcut at the moment of running.
Shortcut Input is also a magic variable. You can insert it into any action's field, just like the output of an earlier action. Unlike other magic variables, it exists before the first action runs.
Common ways input arrives:
- You select files in Finder and run the shortcut as a Quick Action.
- You select text in another app and run the shortcut from the Services menu.
- You use the Share menu in an app that supports sharing.
- You run the shortcut from Terminal and give it an input file.
- Another shortcut uses the Run Shortcut action and passes it content.
If you start a shortcut by clicking it in the Shortcuts app, nothing is passed in. Shortcut Input is empty. Many first-time errors come from this.
Setting what the shortcut accepts
Open a shortcut in the editor and open its details with the info button in the toolbar. Two groups of settings control input.
The first is where the shortcut appears. Turning on Use as Quick Action adds it to Finder's Quick Actions. If you also enable the Services menu option, it appears there for selected text and files. Show in Share Sheet (the Share menu) is a similar setting. Each one makes the shortcut available from that place.
The second is what the shortcut accepts. A line at the top of the editor reads like this:
> Receive content types input from sources
Click the type to choose what the shortcut accepts, such as Files, Folders, Images, Text or URLs. Narrower choices are better. A shortcut that renames images should accept only images. Then it will not appear when you select a text file, and you avoid errors later in the flow.
Click the "If there's no input" setting to choose one of three behaviors:
| Setting | What happens when no input arrives | Use it when |
|---|---|---|
| Continue | The shortcut runs with empty Shortcut Input | Input is optional, and you handle the empty case yourself |
| Ask For | macOS prompts you to pick content of the accepted type | You want to run it by hand and still choose a file |
| Stop and Respond | The shortcut stops and shows a message you wrote | Input is required and running without it is a mistake |
The Ask For setting is useful while building. It lets you click Run in the editor and choose a test file, so you do not have to go through Finder every time.
Input is a list of items
Shortcut Input holds a list of items, even when you pass only one. If you select three files in Finder and run a Quick Action, Shortcut Input contains three file items.
Actions treat lists in different ways:
- Some actions work on the whole list, such as Combine Text or Count.
- Some actions work on the first item only, such as Quick Look in some cases.
- Some actions run once per item, such as Rename File or Resize Image.
You often need to be explicit. The safest pattern is a Repeat with Each action with Shortcut Input as the list. Inside the loop, Repeat Item is the current item. It is another magic variable.
A worked example is a shortcut that reports the name and size of each selected file:
- Add Repeat with Each and set its list to Shortcut Input.
- Inside the loop, add Get Details of Files and set the input to Repeat Item.
- Set the detail to Name, and add a second Get Details of Files action with the detail set to File Size.
- Add Text with the two values inserted as magic variables, for example
Name: [Name], Size: [File Size]. - After the loop, add Show Result and insert the Repeat Results variable.
Run it from Finder with one file, then with five files. The same shortcut handles both cases because the loop works on a list of any length.
Text input behaves differently from file input
When the input is text, each item is a text value. Services menu text arrives as a single item, even if it spans several lines. Use Split Text if you need one item per line.
When the input is a file, the item is a file object, not its contents. Files carry a name, a type and a location. To read a text file's content, use Get Text from Input, or use Get Contents of File where it is available. If you insert a file into a Text action, you may get the file's name, or its contents, depending on the action. If the result looks wrong, run the shortcut and check what each action outputs. That is covered in the debugging chapter.
A mixed selection is possible if your accepted types are broad. For example, a shortcut that accepts Files and Folders can receive both. If your actions depend on the type, narrow the accepted types, or filter the items with Filter Files before using them.
Handling empty input
Even with Stop and Respond set, a shortcut can reach an action with nothing to work on. For example, Services menu text can be empty, or a filter can remove every item. A defensive pattern uses If:
- Add If with the condition
Shortcut Inputhas any value. - Put the real work in the If branch.
- In the Otherwise branch, add Show Alert or Show Notification with a short message such as "Select at least one file first."
This is cheap to add. It also gives anyone who uses your shortcut a clear message in place of a silent failure.
Passing Shortcut Input to a shell script
The Run Shell Script action has an Input field, and Shortcut Input can go there. The action has a setting called Pass Input with two choices.
- to stdin sends the content as a stream of text to the script's standard input. Use this for text processing.
- as arguments sends each item as a separate command-line argument. Use this when you want file paths.
For text, this script reads from standard input and converts it to uppercase:
tr '[:lower:]' '[:upper:]'With Pass Input set to to stdin, the script returns the uppercase text as the action's output. A later action can use that output.
For files, set Pass Input to as arguments. Each file arrives as a path in $1, $2 and so on, and all of them are in "$@":
for f in "$@"; do
wc -c < "$f"
doneAlways quote "$f". File names often contain spaces, and unquoted variables split on them. The script prints one byte count per file, so a three-file selection yields three lines of output.
The shell script action has its own chapter later. For now, remember that the Pass Input choice decides whether your script sees text or file paths. Choosing the wrong one is the usual reason a script prints nothing or prints a file's path in place of its contents.
Supplying input from Terminal
The shortcuts command can pass input to a shortcut, which helps you test without opening Finder. The full tool is covered in a later chapter. This one command shows the idea:
shortcuts run "Uppercase Text" --input-path ~/Desktop/note.txtThe file at --input-path becomes Shortcut Input. The shortcut's accepted types still apply, so a shortcut that accepts only images will not take a text file.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Shortcut is missing from Finder's Quick Actions | Use as Quick Action is off, or the accepted types do not match your selection | Turn it on in the details panel and broaden the types |
| Shortcut runs but does nothing when clicked in the app | No input was passed and the setting is Continue | Use Ask For while testing, or add an If check |
| Only the first file is processed | Actions were applied to the list without a Repeat with Each loop | Wrap the work in a loop over Shortcut Input |
| Script receives a path when you expected content | Pass Input is set to as arguments | Switch to stdin, or read the file inside the script |
| Script fails on file names with spaces | Argument was not quoted | Use `"$f"` in the shell script |
Exercise
You built a Finder Quick Action that renames the selected files. It works when you select one file in Finder, but when you select three files only the first one is renamed. What is the most likely cause, and how do you fix it?
Show answer
The rename action is working on the whole Shortcut Input list in a way that affects only the first item, because no loop applies it to each item. Wrap the rename in a Repeat with Each action whose list is Shortcut Input, and give the rename action Repeat Item as its input. Then test with one, three and zero files selected.
Exercise
A shortcut uses Run Shell Script with the script tr '[:lower:]' '[:upper:]'. Which Pass Input setting should you choose when the input is selected text, and why would "as arguments" fail to give the result you want?
Show answer
Choose "to stdin". The tr command reads from standard input, so the text has to arrive as a stream. With "as arguments", the text is passed as command-line arguments and tr does not read them, so it would wait for input or return nothing useful.
Exercise
You want a shortcut to require selected text and to show the message "Select some text first" if you click it directly in the Shortcuts app. Which no-input setting do you use, and which alternative lets you keep running it by hand while testing?
Show answer
Use Stop and Respond with your message to require input. During testing, Ask For lets you pick content when you run it by hand, so you can still click Run in the editor.