Sub topic 4
Asking for input and showing results
A shortcut that always does the same thing is useful. A shortcut that asks you a question, or tells you what it found, is much more flexible. This chapter covers the actions that talk to the person running the shortcut: Ask for Input, Choose from Menu, Choose from List, Show Result, Show Alert and Show Notification.
It assumes you can already add actions to a shortcut, connect them, and use magic variables to pass an action's output to a later action. It also assumes you know how to run a shortcut from the editor with the Run button.
The two directions of interaction
Interaction actions work in one of two directions:
- Input actions pause the shortcut, show a window, and wait. The answer becomes the action's output, and later actions can use it.
- Output actions display something to the user. Most of them do not change the data flowing through the shortcut.
The pause is the important part. While a prompt is on screen, the shortcut is suspended. If the person dismisses the prompt with Cancel, the shortcut stops.
Ask for Input
Ask for Input shows a dialog with a prompt and a field. You configure it with three settings:
- Input Type is one of Text, Number, URL, Date, Time, or Date and Time. It decides which keyboard or picker the field uses and what kind of value comes out.
- Prompt is the question shown to the user. Write it as a complete question, such as "How many days should I look back?" rather than "Days".
- Default Answer pre-fills the field. Use it when one answer is correct most of the time, so the user can press Return.
The output is a magic variable named Provided Input. Its type matches the Input Type you chose.
Why the input type matters
The Input Type is a small form of validation. With Number selected, the field only accepts a numeric value, so a later Calculate action never receives the text "abc". With Text selected, everything is a string, even if the user types 42.
| Input Type | Output | Typical use |
|---|---|---|
| Text | Text | Names, search terms, notes |
| Number | Number | Counts, thresholds, amounts |
| URL | URL | Links to open or fetch |
| Date | Date | Deadlines, report ranges |
| Time | Date (time of day) | Reminders, alarms |
| Date and Time | Date | Calendar events |
Text input can run to several lines. Check the action's options for a multiline setting, because the name and location of that option can vary between macOS versions.
Worked example: a quick note shortcut
This shortcut asks for a note and then shows it back with a character count.
- Add Ask for Input. Set Input Type to Text and Prompt to "What should the note say?".
- Add Count and set it to count Characters in Provided Input.
- Add Text and type:
Your note hasthen insert the Count variable, thencharacters. - Add Show Result and pass it the Text action's output.
Run it from the editor. A dialog appears, you type a note and click Done, and a second window reports the count. Open the Count action's result in the editor and check that it holds a number, not text.
Show Result, Show Alert and Show Notification
The three display actions look similar but behave differently.
| Action | What it shows | Does the shortcut wait? | Output |
|---|---|---|---|
| Show Result | A window with the content you pass in | Yes, until the user dismisses it | None you normally use |
| Show Alert | A dialog with a title, a message and buttons | Yes, until the user responds | None you normally use |
| Show Notification | A notification banner | No, the shortcut continues | None you normally use |
Show Result
Show Result displays whatever you give it. It works best with text. If you pass a file or a list, it shows a summary of the item rather than a neat sentence, so convert the value to text first when you want to control the wording.
Use it for the final answer of a shortcut, such as "Renamed 12 files". Don't scatter it between every step, because each one stops the shortcut until you click.
Show Alert
Show Alert has a Title, a Message, and a Show Cancel Button toggle. With the toggle on, the user can choose Cancel, which stops the shortcut. With the toggle off, the alert only informs.
This makes Show Alert a confirmation gate. Put one before any action that cannot be undone, such as deleting files:
- Add Show Alert. Title: "Delete these files?" Message: the count of files you are about to delete.
- Leave Show Cancel Button on.
- Put the delete action after the alert.
If the user clicks Cancel, the shortcut ends before the delete action runs.
Show Notification
Show Notification posts a banner and moves on immediately. Use it for status messages in shortcuts that run without anyone watching, such as an automation that finishes in the background. It also depends on notification permission: if you have turned off notifications for Shortcuts in System Settings, the banner will not appear, and the shortcut will still carry on as if it had.
Choose from Menu
Choose from Menu presents a prompt and a set of buttons. Each button leads to its own branch of actions inside the same block. The block has this structure:
- The Choose from Menu action holds the prompt and the list of menu items.
- Each item has a branch that contains the actions to run when it is picked.
- An End Menu action closes the block.
Only one branch runs each time. The action also outputs the Chosen Item, the text of the label that was picked. You can use it after End Menu, for example to log which option was used.
Building a menu
- Add Choose from Menu and set the Prompt to "What do you want to do?".
- Edit the item labels. Keep them short and action-oriented: "Resize", "Convert", "Rename".
- Drag actions into each branch.
- Add a Show Result after End Menu if every branch should end with the same message.
Menu items are fixed when you build the shortcut. You cannot generate them from data. When the options come from a list, use Choose from List instead.
Choose from List
Choose from List takes a list as input and lets the user pick from it. This is what you use when the options are produced by earlier actions, such as a list of files from Find Files or a list of calendar events.
It has these options:
- Prompt is the heading above the list.
- Select Multiple lets the user tick several items. The output is then a list.
- Select All Initially starts with everything ticked, which is quicker when the user usually wants most items and only removes a few.
The output is the Chosen Item or Chosen Items. The type is preserved. If you passed files in, you get files back, not names. That means you can pass the result straight to an action such as Rename File or Get File Size.
Combining input with Shortcut Input
A shortcut can receive content from Finder or another app. When it does, asking again would annoy the user. A good pattern is to ask only when nothing was provided:
- Add an If action and set the condition to Shortcut Input has any value.
- In the If branch, use Shortcut Input as the content.
- In the Otherwise branch, add Ask for Input or Select File.
- End the If.
The output of an If block is the output of whichever branch ran. This gives the next action one value to work with, whichever route supplied it.
Common errors
These are the problems learners hit most often with interaction actions.
- The shortcut stops without an error. The user, or you while testing, clicked Cancel. This is expected behavior. Test by clicking Done.
- A number behaves like text. You used Input Type Text. Switch to Number, or add a Get Numbers from Input action.
- A menu branch is empty. A branch with no actions does nothing. That is valid, but it usually means you forgot to fill it.
- Show Result shows something odd. You passed a complex item, such as a file. Convert it to text first, with Get Details of Files or a Text action.
- Too many windows. Every Show Result is a stop. Combine the information into one Text action and show it once.
- A prompt appears in the wrong place. A shortcut run from the command line or an automation still needs somewhere to show a prompt. Test those paths and decide whether to remove the prompt.
Design guidelines
- Ask the least you can. Every prompt slows the person down, so use Default Answer and Shortcut Input to skip questions.
- Match the Input Type to the data, so the system validates for you.
- Keep a single final output. One Show Result at the end is easier to follow than several.
- Put confirmation alerts directly before destructive actions, not at the start of the shortcut.
- Name menu items as verbs. "Resize" tells the user what happens. "Option 1" does not.
Exercise
You are building a shortcut that deletes the files a user selects in Finder. Which actions would you use to let the user review the files and confirm before anything is deleted, and what happens if the user clicks Cancel?
Show answer
Pass the Shortcut Input (the files) to Choose from List with Select Multiple and Select All Initially turned on, so the user can untick files to keep. Then add Show Alert with a message that includes the count of chosen files, and leave Show Cancel Button on. Put the delete action after the alert. If the user clicks Cancel in either the list or the alert, the shortcut stops before the delete action runs, so nothing is removed.
Exercise
A shortcut uses Ask for Input with Input Type Text to ask for a number of days, then adds 7 to the answer with Calculate. Why might this behave badly, and what is the fix?
Show answer
With Input Type Text the value is a string, so the user can type anything, including words, and the calculation may fail or give the wrong result. The fix is to set Input Type to Number. The dialog then only accepts numeric input and the output is a number that Calculate can use directly.