Sub topic 3
Variables and magic variables
This chapter assumes you can open the Shortcuts app on your Mac, add actions to the shortcut editor, and run a shortcut. It also assumes you know that an action's output flows into the next action. Here you will learn how to hold on to a value so that later steps, not only the next one, can use it.
What a variable is in Shortcuts
A variable is a named container for a value. The value can be text, a number, a date, a file, a list of items, or a dictionary. When you place a variable in an action's field, Shortcuts shows it as a small colored pill. At run time, Shortcuts replaces the pill with the value the variable holds.
Shortcuts has three kinds of variable:
| Kind | How it is created | Can be reassigned? | Typical use |
|---|---|---|---|
| Magic variable | Automatically, from any action that produces output | No. It always points to that one action's output. | Reusing a result a few steps later |
| Named variable | With the **Set Variable** action | Yes | Counters, totals, values you update |
| Special variable | Provided by Shortcuts | Not by you | Shortcut Input, Clipboard, Current Date, Ask Each Time |
A variable exists only while a shortcut runs. When the run ends, its value is gone. Shortcuts has no built-in storage that carries variables from one run to the next. If you need to keep a value, write it to a file, a note, or the clipboard.
Magic variables
Most actions produce output. Ask for Input produces the text the user typed. Current Date produces a date. Get Contents of Folder produces a list of files. Each of these outputs is available as a magic variable, so you do not have to create it.
To use one:
- Click in a field that accepts variables, such as the field of a Text action.
- Choose Variables in the strip above the keyboard area, or type to search. The picker lists the outputs of the actions above the current one.
- Select the output you want. A pill appears in the field.
A magic variable is tied to a specific action. This has two consequences.
- The pill can only refer to actions above the one using it. If you drag the producing action below the consumer, the pill breaks and the field is empty at run time.
- If you delete the producing action, every pill that referred to it is removed.
You can rename a magic variable to make long shortcuts readable. Click the action's output, or the pill in a later field, and choose Rename. A pill called Asked Input is easier to follow when it is renamed Customer Name. Renaming changes only the label. The pill still points at the same action's output.
Reading properties from a pill
A pill can often expose a property of its value. Click a pill, and the options include properties that depend on the value's type. A file pill offers properties such as Name, File Extension, and File Size. A date pill offers formatting options. A pill whose value is text has no useful properties beyond the text itself. This lets you extract a piece of a value without adding another action.
For example, you can put a file pill in a Text action and choose its Name property. The text then contains the file's name rather than a file reference.
Named variables
Use Set Variable when a value must be changed later, or when you want a stable name that does not depend on one action's position. The action has two parts: a name and an input. The input is whatever flows in from the previous action, unless you replace it with a pill.
Related actions:
- Set Variable stores the input under a name. If the name already exists, the old value is replaced.
- Get Variable returns the current value of a named variable as output for the next action.
- Add to Variable appends an item to a named variable. If the variable does not exist or holds a single item, it becomes a list.
The difference between named and magic variables is easiest to see when the same step runs more than once.
# A magic variable is like a read-only result bound to one step.
result_of_step_3 = "report.pdf"
# A named variable is like an ordinary variable you can reassign.
total = 0
for size in [120, 300, 80]:
total = total + size # equivalent to Set Variable after each Calculate
print(total) # 500In Shortcuts, the total = total + size line corresponds to a Calculate action that adds the current total pill and the current item's size, followed by Set Variable with the name total.
Special variables
Special variables are always available from the picker, regardless of which actions come before.
- Shortcut Input is what the shortcut received when it started, such as selected files from Finder or text from a Services menu. A later chapter covers it in detail.
- Clipboard is whatever is on the clipboard at the moment the pill is read. Copying something later in the same run changes it.
- Current Date is the date and time at the moment the pill is evaluated, so two pills in the same run can hold slightly different times.
- Ask Each Time is not a value. It makes Shortcuts prompt you when the action runs, instead of using a fixed setting.
How data flows: a worked example
The shortcut below asks for a name, builds a greeting with a timestamp, and shows it. It uses all three kinds of variable.
| Step | Action | Notes |
|---|---|---|
| 1 | **Ask for Input** (type: Text, prompt: "Your name?") | Its output becomes a magic variable. Rename it **Name**. |
| 2 | **Current Date** | Rename its output **Now**. |
| 3 | **Format Date** (input: **Now**, format: Custom, `HH:mm`) | Output is text such as `14:05`. Rename it **Time**. |
| 4 | **Text**: `Hello, ` **Name** `. It is ` **Time** `.` | The two pills are inserted inline. |
| 5 | **Set Variable** (name: `greeting`, input: the Text from step 4) | Stores the sentence under a stable name. |
| 6 | **Show Result** (input: `greeting`) | Displays the sentence. |
Step 5 is optional in a short shortcut like this one, because the Text output is already a magic variable. It becomes useful when the shortcut grows and you want a name that survives rearranging.
Notice that Name is used in step 4, two actions after it was produced. Step 2 and step 3 sit between them. The normal "output feeds the next action" rule would have lost the name, but the magic variable keeps it available.
Variables in loops
Named variables are the usual way to accumulate a result across loop iterations. Suppose you want the total size of several files.
- Number
0, then Set Variabletotal. - Repeat with Each over the files.
- Inside the loop, Get Details of Files (File Size) on Repeat Item.
- Calculate:
totalplus the file size. Then Set Variabletotal. - After End Repeat, Show Result with
total.
Repeat Item and Repeat Index are magic variables that exist only inside the loop. Repeat Item is the current element. Repeat Index is its position, starting at 1. A later chapter covers loops fully. Here, the point is that total is a named variable because it must be reassigned on each pass, and a magic variable cannot be.
Common errors
- Pill is empty at run time. The producing action was moved below the consumer, deleted, or skipped by an If branch that did not run. Check that the action sits above the pill and executes on every path.
- Using a variable before setting it. Get Variable on a name that was never set returns nothing. Initialize counters at the top of the shortcut.
- Two variables with the same name by accident. Set Variable silently replaces the old value. Use distinct, descriptive names.
- Expecting a value to persist. A variable set during one run is not there in the next run. Save it to a file if you need it later.
- A value of the wrong type. A file pill in a Text action may insert the file's name or a reference, depending on the property you chose. Check the pill's property and the action's result in the editor.
- Confusing Clipboard and Shortcut Input. The clipboard can change during a run. Shortcut Input is fixed at the start.
Choosing between magic and named variables
Use a magic variable when you need a result once or twice, a few steps away from where it was produced. It requires no extra action and cannot be overwritten by mistake. Use a named variable when you need to change the value, accumulate a result in a loop, or refer to a value from several places in a shortcut that might be reorganized. In practice, most shortcuts use mostly magic variables and a few named ones.
Exercise
A shortcut has these steps: (1) Ask for Input, (2) Set Variable count to the number 0, (3) Repeat 3 times, with Calculate count + 1 and Set Variable count inside, (4) End Repeat, (5) Show Result of count. What does Show Result display, and why can't a magic variable replace count in this design?
Show answer
It displays 3. Each pass reads the current value of count, adds 1, and stores the result back under the same name. A magic variable is bound to one action's output and cannot be reassigned, so it could not carry the updated total from one pass to the next.
Exercise
You build a shortcut with Ask for Input first and a Text action last that uses the Ask for Input pill. Later you drag Ask for Input to the bottom, below the Text action. What happens when you run it, and how do you fix it?
Show answer
The pill refers to an action that now runs after the Text action, so it is broken and the Text action gets no value for it. Move Ask for Input back above the Text action, then reinsert the pill if it was removed.