Sub topic 4
Asking for input and showing results
This chapter assumes you can create a shortcut, add actions to it, and pass an action's output to the next action. It also assumes you know what a variable is. Here you will make a shortcut talk to the person running it. It asks questions, shows answers, and branches on a choice.
Three actions for talking to the user
A shortcut that runs with no interaction is fine for fixed jobs. Many useful shortcuts need a value that changes each time, such as a name or a folder. These three actions cover most cases.
| Action | What it does | What it outputs |
|---|---|---|
| Ask for Input | Shows a dialog with a field for text, a number, a date, a URL and so on | The value the user typed |
| Show Result | Displays a value in a window and waits for the user to close it | Nothing useful |
| Choose from Menu | Shows a list of choices and runs the steps under the choice picked | The result of the chosen branch |
Show Result also has a sibling, Show Notification. A notification does not block the shortcut. Use it for short status messages such as "Done".
Ask for Input
Add Ask for Input and look at its fields.
- Input Type sets what the user may enter: Text, Number, URL, Date, Time or Date and Time. The dialog rejects values that do not fit the type, so you get checking for free.
- Prompt is the question shown to the user. Make it specific. "Folder name" is better than "Enter text".
- Default Answer pre-fills the field. This saves time when one answer is usual.
The action's output is the typed value, stored in the magic variable named Provided Input. You can tap that variable in any later action. Because the Number type gives you a number and not text, you can use it directly in math and comparisons.
If the user clicks Cancel, the whole shortcut stops. That is the intended behavior, so you don't need to handle it yourself.
Show Result
Show Result takes one value and displays it. Put it after the action whose output you want to see. In its text field you can mix words with variables. For example, type Hello, and then insert the Provided Input variable to build a greeting.
The window waits until the user dismisses it. This pause is useful when the user must read something before the shortcut continues. It is a nuisance in a shortcut that runs on a schedule, so prefer Show Notification there.
Choose from Menu
Choose from Menu adds a set of branches. You type the menu's prompt and name each choice. Actions placed under a choice run only if the user picks it. The branches rejoin below the menu, and the shortcut continues from there.
A worked example
Build a shortcut named Quick Item with these steps.
- Add Ask for Input. Set Input Type to Text, Prompt to
Name for the new item, and leave Default Answer empty. - Add Choose from Menu with the prompt
What should I make?. Name the choicesFolderandText file. - Under
Folder, add New Folder and set its name to the Provided Input variable. Choose a destination such as Desktop. - Under
Text file, add Make Text File from the Documents actions. Set its contents to the Provided Input variable, then add Save File to store it. - Below the menu, add Show Notification with the text
Createdfollowed by the Provided Input variable.
Run it from the editor. A dialog asks for a name, a menu asks for the type, and a notification confirms the result.
Common errors
- The dialog accepts anything. Input Type is set to Text. Switch it to Number or URL to restrict input.
- Show Result displays the wrong thing. It shows whichever value you placed in it. Check which variable is inserted.
- The notification never appears. macOS may have notifications for Shortcuts turned off. Check System Settings, then Notifications.
- Both branches seem to run. Actions below the menu's end line run for every choice. Place choice-specific steps inside the branch.
Exercise
You want a shortcut to ask for a number of days, then display the number multiplied by 24 as hours. Which actions do you need, and which Input Type should you choose?
Show answer
Use Ask for Input with Input Type set to Number, then a Calculate action that multiplies Provided Input by 24, then Show Result for the calculated value. The Number type makes sure the input can be used in math.
Exercise
A shortcut runs on a schedule each morning. Should its final step be Show Result or Show Notification, and why?
Show answer
Show Notification. Show Result opens a window and waits for the user to close it, which blocks an unattended run. A notification reports the outcome without stopping the shortcut.