Control flow
Use if to make a decision, menu to ask the user to choose, repeat to run a set number of times, and for to go through a list.
flow.stop() and exit() stop the whole shortcut, even inside a loop or branch. Use either one, not shortcuts.stop().
Friendly conditions
Friendly if blocks support else and these condition forms:
if value == other:
if value != other:
if count >= 3:
if count < limit:
if score is between 20 and 40:
if duration is between quantity(1, unit: "min") and quantity(5, unit: "min"):
if text starts with "prefix":
if text ends with "suffix":
if text contains "part":
if text does not contain "part":
if value has value:
if value has no value:
if value is empty:
if value is not empty:
if first == 1 and second is not empty:
if first == 1 or second == 2:Combining ranges
and binds before or; parentheses group Boolean and friendly conditions. Operands are eager, once, left to right, not short-circuiting. Use a nested if or ternary to guard a call. A range's and separates inclusive bounds, not logical operands:
shortcut:
score = 25
if score is between 20 and 40 and score has value:
result("Within range")
if score is between 1 and 10 or score is between 20 and 30:
result("Within either range")Is it missing, blank, or false?
- Use
has valueto check that something was returned or saved. A blank Text item,false, and0still count as present. - Use
is not emptyto check that text has characters. For known Text, this checks length. Other content uses native presence.has_valueis an alias foris not empty. - Use
if ready:to test whether a Boolean is true, not whether it exists. A missing optional Boolean does not pass the test.
For a value shared across branches, use a regular assignment. If any path continues without assigning it, check for presence before using it.
Guard clauses
Use a guard to exit early. Once it passes, you can use the value directly. An if pin has no value: block that calls flow.stop() works the same way. For a fallback that keeps running, use if instead.
shortcut:
pin = storage.get<Location>("ParkingPinLocation")
guard pin has value else exit()
maps.directions(destination: pin)To require two stored values before using them, write guard total has value and change has value else exit(). Both values are usable after that guard. An or check for presence does not guarantee that both exist.
For a Boolean decision, compare explicitly:
shortcut:
ready = false
guard ready == true else:
alert("Not ready yet.")
exit()
result("Ready")The single-line form's else must be return, exit(), or flow.stop(). The block form takes an indented block that must also exit the scope:
shortcut:
pin = storage.get<Location>("ParkingPinLocation")
guard pin has value else:
alert("No parking pin saved.")
exit()
maps.directions(destination: pin)The else branch of a guard must leave the current shortcut or function. break and continue are not supported. In the main shortcut, return value shows the value and stops; return stops without showing anything. Move any actions you still want to run above the return.
Use web.request(...) else: to handle a request that gives you no response. The else block must leave the current function or stop the shortcut:
shortcut:
endpoint = as_type(text("https://example.com/api"), "URL")
response = web.request(url: endpoint, method: "GET") else:
alert("The server returned no content.")
exit()
show(response)The single-line form works too: response = web.request(url: endpoint) else exit(). The else clause is only available on web.request; other calls reject it. Note that a hard network failure stops the shortcut inside Shortcuts itself — the else branch handles an empty or missing response, not an action-level failure.
Advanced: types and branch results
Use ordinary text and numbers in new conditions. Imported shortcuts can also keep specialized values from Apple Shortcuts.
If two branches need to set a value you will use later, assign to the same ordinary variable in both branches. Giving two action outputs the same name does not combine them. If a branch can reach the next action without setting the variable, check it before use. An alert alone does not stop the shortcut.
require(value) means “assume this is present.” It does not check the value, display a message, or stop the shortcut. Use a guard if the value can be missing.
Switch statements
switch runs the first matching case. If none matches, it runs default when you include one. To use an object created by the switch afterward, include a default and assign the same object type on every path that continues.
shortcut:
mode = text("b")
switch mode:
case "a":
result = text("Alpha")
case "b", "c":
result = text("Beta")
default:
result = text("Other")The switch subject is captured once before case comparisons: switch random(1, 3): draws only one number.
For a single random choice:
shortcut:
switch random(1, 3):
case 1:
result("One")
case 2:
result("Two")
default:
result("Three")Each case takes one or more comma-separated values (compared with or). Cases are exclusive: there is no fallthrough, and only the first matching case runs. default is optional and runs when nothing matches. A switch needs at least one case, and only one default clause is allowed.
Repeats
Use a fixed-count repeat or repeat over a collection:
shortcut:
values = list(["one", "two"])
total = lists.count(input: values)
if total == 2:
repeat 2 times:
for item in values:
result(item)
else:
result("Unexpected count")repeat 2: and repeat 2 times: mean the same thing. Inside a loop, use repeat_index for the repeat number and item for the current list item. A value set only inside the loop may be missing if it never runs. If the shortcut stops inside a loop, it doesn’t run another repeat or continue after the loop.
Named items and indexed loops
shortcut:
for position, fruit in ["Apple", "Pear"]:
result("\(position): \(fruit)")Use for fruit in values: to name each item, or for position, fruit in values: for its one-based index as well. This is shorthand for assigning fruit = item and, in the indexed form, position = repeat_index at the start of each iteration. The built-in item and repeat_index remain available. Existing loop-result syntax, such as for position, fruit in values as results:, is unchanged.
Choose fresh, distinct ordinary alias names, not enclosing mutable variables, helper parameters, reserved names or the loop-result name; item is allowed as the value alias. These are ordinary per-iteration bindings, not a new lexical scope: after the loop an alias can retain its last value, and may be absent if the loop was empty. Use distinct aliases in nested loops when retaining the outer item or index.
Condition operands, loop counts, and collection inputs are checked before the body; named references retain native Variable tokens.
In nested loops, repeat_index refers to the nearest repeat, and item to the nearest for item loop. A count-only repeat does not replace item; the outer value is available again after an inner loop ends.
Use as name on an if, repeat, for, or menu to use its result afterward. Add named "Display Name" if you want a label in Apple Shortcuts. Check for a missing result if the block might produce nothing:
repeat 2 times as repeated named "Repeated Results":
result(repeat_index)Map
map collects one body output per element into its assigned variable, compiling like for item in ... as ...:
shortcut:
names = list(["a", "b", "c"])
uppers = map names as name:
text.change_case(input: name, style: "UPPERCASE")
show(uppers)The as name clause is optional; without it the element is the usual item. The item name is bound to the list's element type, so body type errors point at the right type, and the result is List<T> where T is the body's output type. The item name must differ from the result variable. Like for, the input is not required to be statically known as a list; a non-list input fails at runtime in Shortcuts.
Menus
A friendly menu contains one or more string-titled cases. Its prompt is an expression evaluated before entering a case; it may use an already declared variable or text interpolation, but cannot read a variable first declared inside a case:
shortcut:
menu("Choose an option"):
case "Preview":
preview("Hello")
case "Share":
share("Hello")menu(): leaves the prompt unset, menu(""): supplies empty text, and menu("Choose"): supplies a title.
A named variable assigned in every case remains available afterward. Case result types are joined in the same way as exhaustive if branches.
For static image lists, use icon_list(prompt, items: [{title: "Name"}]). It returns a Contact. See Icon lists for images and limits.
Legacy menu(prompt, options: {title: image}) sorts titles. Images must contain nonempty base64 data. asset("Assets/icon.shortcutyasset") requires a configured asset root. Studio supports portable asset manifests returned by its media tools. Use their exact returned paths, not arbitrary disk paths or invented assets; see media tools.
For imported native control markers, see Imported workflows.
Use ordinary text and numbers in conditions. value == "" compares with empty text; value is empty tests for blank text when the value is known to be Text.
Dynamic menu case titles
A menu can derive a case title from a Text expression while preserving a separate static native title:
shortcut DynamicMenu:
day = text("Wednesday")
menu("Choose a day"):
case label(day, fallback: "Selected day"):
show(day)
case "Cancel":
exit()label(TEXT_EXPRESSION, fallback: "Static title") is valid only after case, not as a standalone action, variable initializer, or general expression. Existing case "Static title": syntax is unchanged. Each dynamic expression is evaluated once, in source order, in the scope entering the menu before any branch executes. A title cannot refer to values defined only inside a branch. Known nontext values are rejected; optional native Text references retain native behavior.
The fallback sets a static title in the shortcut. It does not replace an empty or missing value when the shortcut runs.
Use dynamic labels only in regular menu blocks, not imported native_menu blocks or menus with custom native item or prompt overrides. If a branch needs an earlier action’s result, assign that result to a variable and refer to it by name.
Static typed menu items and synonyms
For a typed native menu item whose branch keeps only its static title, use an exact matching literal and attributed: false:
shortcut StaticMenu:
menu("Choose"):
case label("Status", fallback: "Status", attributed: false, synonyms: ["State"]):
show("Ready")
case "Cancel":
exit()attributed: false requires a literal label equal to the fallback, not a runtime expression. It emits the typed menu item without adding an attributed branch-title field. Omitting this option retains the normal dynamic-label behavior. Optional synonyms is a literal text list of native metadata aliases; an omitted field and an explicit empty list remain distinct. Do not promise voice matching, runtime fallback, or other behavior from this metadata alone. The existing case-only scope and native-override restrictions still apply.