Sub topic 3
Variables and magic variables
This chapter assumes you can open the Shortcuts app, add actions to a shortcut, and run it. It builds on the previous chapter, where each action passes its output to the next one. Here you will learn how to keep a value for later use, even after other actions have run in between.
Why you need variables
By default, an action hands its result only to the action directly below it. That works for short chains. It fails when a later step needs a value from earlier, such as a name you asked for at the start.
A variable is a named container that holds a value so you can use it anywhere further down the shortcut. Shortcuts gives you two kinds: regular variables that you create yourself, and magic variables that the app creates for you.
Regular variables
You create a regular variable with the Set Variable action. You give it a name and it stores whatever the previous action produced. You read it back with the Get Variable action, or by inserting it into the field of another action.
A typical sequence looks like this:
- Ask for Input (Text) asks "What is the project name?"
- Set Variable stores the answer in a variable called
ProjectName. - Later actions insert
ProjectNamewherever they need it.
You can also change a stored value. Add to Variable appends a new item to an existing variable. This is how you build a list while a shortcut repeats a step.
Magic variables
A magic variable is created automatically from an action's output. You don't need a Set Variable action. Every action that produces output can be used this way.
To use one, tap or click in an action's input field. A variable bar appears above the keyboard or in the field's menu, and it lists the outputs of the actions above. Choose one and it appears in the field as a small colored token. The token is named after the action that made it, for example "Ask for Input" or "Text".
Magic variables are convenient, but they have a drawback. If you have three actions called "Text", the tokens are hard to tell apart. You can rename the source action, or use Set Variable with a clear name when the value matters.
Choosing between them
| Situation | Better choice |
|---|---|
| Using the output of an action a few steps back, once | Magic variable |
| Using the same value many times | Regular variable |
| Building a list across a loop | Regular variable with Add to Variable |
| Making a long shortcut easier to read | Regular variable with a descriptive name |
Worked example: a dated note name
This shortcut asks for a title and builds a file name from it and today's date.
- Add Ask for Input with the prompt "Note title?".
- Add Set Variable and name it
Title. - Add Current Date. Its output is a magic variable.
- Add Format Date with the custom format
yyyy-MM-dd. - Add Text and type:
Titletoken, then a dash, then the Formatted Date token. - Add Show Result.
If you enter "Meeting" on 27 November 2026, the result is Meeting-2026-11-27. The title is a regular variable because you might want it again later. The formatted date is a magic variable because you use it only once, right away.
The diagram shows how the values travel.
Common errors
- A variable is empty. You inserted it before the action that sets it. Variables only exist for actions below the point where they are created.
- The wrong token is inserted. Two actions share a name. Rename one of them or use a named variable.
- A value has an unexpected type. A variable holding a number or a file behaves differently from text. Tap the token to see options such as properties. The next chapters cover content types in more detail.
- A list becomes one long string. Add to Variable on a text variable builds a list. Inserting that list into a Text action joins the items on separate lines.
Exercise
You ask for a name at the start of a shortcut, then run five other actions, and want to use the name in the last action. Which approach keeps the shortcut easiest to read, and why?
Show answer
Use Set Variable right after Ask for Input and give it a descriptive name such as UserName. Then insert that variable in the last action. A magic variable from Ask for Input would also work, but its generic name is easy to confuse with other actions as the shortcut grows.
Exercise
Why does a variable come out empty if you insert it into an action that sits above the Set Variable action that creates it?
Show answer
A variable only exists after the action that sets it has run. Actions run from top to bottom, so an earlier action has no value to read yet.