Functions and imports
Write reusable functions with def above shortcut:. A function returns a value and can take inputs. Call a function with no inputs using empty parentheses.
def show_banner():
banner = text("Hello")
result(banner)
return banner
shortcut:
shown = show_banner()Inside a function, return gives a value back to the caller. In the main shortcut, return value shows the value and stops the run; return stops without showing anything. See Control flow.
Use import to call functions from another file. Put the path in quotes and use as to give it a shorter name:
<!-- docs-project: greeting file: Main.shortcuty entry: Main.shortcuty -->
# Main.shortcuty
import "shared/Utilities" as Utilities
shortcut:
name = text("Sam")
message = Utilities.greet(name)
result(message)<!-- docs-project: greeting file: shared/Utilities.shortcuty entry: Main.shortcuty -->
# shared/Utilities.shortcuty
def greet(name):
greeting = text(name)
return greetingPass arguments by position or by name. If a function doesn’t stop the whole shortcut, each path through it must return a value. Arguments can be results from actions or other functions. A call with no arguments keeps the caller’s current implicit input; otherwise each argument updates it as it’s evaluated.
A typed helper can declare an optional argument with a default. For Helpers.shortcuty:
<!-- docs-project: scaling file: Helpers.shortcuty entry: Main.shortcuty -->
def scaled(value: Number, factor: Number = 2) -> Number:
return value * factorThe entry file can supply names in a different order:
<!-- docs-project: scaling file: Main.shortcuty entry: Main.shortcuty -->
import "Helpers" as H
shortcut:
result = H.scaled(factor: 3, value: 4)
numbers.round(input: result)Arguments run once in the order you write them. Then any defaults you left out run in the order they’re declared. Put required parameters before defaults, and positional arguments before named ones. A default can use earlier parameters or call another function. It can’t use later parameters, variables from the function body, or an unnamed output from the caller. Use variable("Name") for an existing named shortcut variable. Variables created by a default belong only to that call.
A type tells readers what the function accepts or returns. Use Number | Text for either type, List<Number> for a list of numbers, and Optional<Number> for a number that can be missing. Check for a missing value before using it. A type annotation doesn’t convert the value for you, and Value doesn’t mean Number. An empty list can satisfy a typed list; a list with unknown items can’t prove they’re all numbers.
If an argument has the wrong type, look at that argument or its default. If the returned value has the wrong type, look at the function’s return. Without a return type, the possible return values determine the result type; if one is null, the result can be missing.
A local helper can return as soon as it has a result:
def positive_or_zero(value: Number) -> Number:
if value > 0:
return value
return 0
shortcut:
result = positive_or_zero(-3)
numbers.round(input: result)Returning from a function does not stop the shortcut or leave the function that called it. If the only return is inside a loop, add a return after the loop in case it runs zero times. A path that stops the whole shortcut with flow.stop() does not need a return.
A return inside a loop skips the rest of that function’s work, but Shortcuts may still step through the remaining repeats. It isn’t an immediate loop break.
Each call gets its own parameters and local variables. Use variable("Name") to read a named shortcut variable only when the function has no local declaration of that name.
A parameter named input or clipboard takes the place of that built-in name inside the function. A repeat loop has its own repeat_index; a loop over a list also has its own item. Outside the loop, the previous names still work.
A function cannot call itself, directly or through another function. Use shortcuts.run_self when you intentionally want to run the whole shortcut again.
Without as, use the imported file’s name without its extension. Write the path relative to the file doing the importing. You can leave off .shortcuty or .shortcutscript.
Studio projects
Add helper files in Studio's Files panel and import them by relative path. Only the entry file needs the shortcut header. Export a .shortcutystudio file to retain source files, settings, and app identities together. See Projects for the Studio format.
Computed defaults
A default is what the function uses when you leave an argument out. It can be a value, a comparison, a call to another function, or an expression using ? : or ??. Put parentheses around arithmetic inside a comparison, as in (score + bonus) >= 10.
def status(score: Number, passed: Boolean = score >= 10) -> Text:
return passed == true ? "Pass" : "Try again"
shortcut:
result(status(12))
result(status(12, passed: false))Here, status(12) shows “Pass.” Passing passed: false skips the default and shows “Try again.” In if or guard, a Boolean tests true or false. Use has value to check whether something exists, even if it’s false.
Build still checks the syntax of unused functions, including imported functions and their defaults. It does not run them or check every possible result.
Expansion limits
If you get SS1306 or SS1307, simplify the chain of function calls or use a loop. See Build messages.
Keeping larger shortcuts efficient
Each function call adds its own copy of the function’s actions. A loop puts those actions in the shortcut once and runs them for each item. Use separate files to organize your project, not to shrink the built shortcut.
Keep values that later steps or repeats need. Fewer actions don’t always make a shortcut faster.
Icon lists
Use icon_list for a single-selection list with optional images and subtitles. It works in Studio without an asset root:
shortcut:
selected = icon_list("Choose", items: [
{title: "Preview", subtitle: "Inspect the result"},
{title: "Share", subtitle: "Send a copy"}
])
title = contacts.detail(contact: selected, property: "Name")
result(title)To add a picture, use image with base64 PNG or JPEG data or a project image. Leave it out if you don’t have a picture. Use fixed text and images, not variables or URLs. The list keeps the order you write. The choice is a Contact: read its title with contacts.detail(contact: selected, property: "Name") or its subtitle with "Company". Use unique titles if they determine what happens next. Cancel can stop the shortcut.
The picker uses Apple’s contact-style list without saving anything to Contacts. Check icons and subtitles on your device; their appearance can vary.
Use 1–100 items, labels up to 512 characters, and PNG or JPEG images. Each decoded image can be up to 256 KiB, with 1 MiB total. A URL does not load an image.
Pictured menus with stable values
Prefer vcard_menu when a pictured menu should return stable Text rather than a Contact:
Generate and attach pictured menu icons
Give each pictured row a real image if you don’t want initials. Reuse suitable project images before asking Studio to make new ones. Its built-in icons don’t need a paid image-model request.
- Write the menu first. Studio controls artwork generation on the server; the app requesting the build does not affect what gets embedded.
- Use a project image or ask Studio to make a built-in icon, such as
home,settings, orinfo. Use the same palette and size across related rows. - Set the row’s
imagetoasset("EXACT_RETURNED_PATH"), using the real path from Studio. Keep the image with the project; don’t paste its base64 into chat. - Build the shortcut. The vCard embeds the image as base64 in its PHOTO field. Check the way it looks in Apple Shortcuts.
- If no image is available, use a clear text menu instead.
Keep labels readable even without an image. A clock or progress icon can help identify a duration, but it doesn’t replace the label.
Each row returns its value, or its title if you leave value out. Use 1–100 rows with unique, single-line titles and no leading or trailing spaces. If any row has a value, don’t put dots in the titles. Use fixed text up to 512 characters for the prompt, title, subtitle, and value. A supplied value can’t be empty or contain control characters. Nothing is added to Contacts. Cancel follows Choose from List; it doesn’t pick a default.
Both helpers accept project PNG/JPEG images or their base64 bytes. They don’t accept URLs, SF Symbol names, or lists made while the shortcut runs. See Icon lists for image limits.
Use vcard_menu for a text result or icon_list for a Contact. Test the menu in Apple Shortcuts to check how it looks and what happens on Cancel.
Shared values across files
Declare a top-level constant in a helper file and read it through that file's import alias. Constants are literal data, not hidden runtime globals.
<!-- docs-project: shared-config file: Config.shortcuty entry: Main.shortcuty -->
const TITLE = "Daily report"
const OPTIONS = {
enabled: true,
labels: ["Work", "Personal"]
}
def heading(prefix: Text = TITLE) -> Text:
return prefix<!-- docs-project: shared-config file: Main.shortcuty entry: Main.shortcuty -->
import "Config" as Settings
shortcut:
show(Settings.TITLE)
show(Settings.OPTIONS["labels"][1])
show(Settings.heading())Use the constant by name in its own file, or through the import alias from another file. A parameter or local variable can reuse that name without changing the original. To change a value just for one run, copy it into a local variable.
Use literal text, numbers, true/false, null, lists, or dictionaries. Lists and dictionaries can span lines. A constant can’t call an action, use interpolation, or read another value. Choose a unique name that isn’t already a function, object, import alias, built-in input name, or reserved __shortcut name. Very large values are limited.
Pass values that change during a run as function arguments or results.
Shared values declared in Main
Use shared NAME = literal in the entry file when helpers need the same value. No separate Config file or reverse import is needed.
<!-- docs-project: entry-shared file: Main.shortcuty entry: Main.shortcuty -->
import "Greeting"
shared TITLE = "Daily report"
shared LIMIT = 3
shortcut:
show(Greeting.heading())
show(LIMIT)<!-- docs-project: entry-shared file: Greeting.shortcuty entry: Main.shortcuty -->
def heading(prefix: Text = TITLE) -> Text:
return prefixEvery imported helper can read these values by name, including helpers imported by other helpers. Set them at the top of Main, outside shortcut:. They follow the same rules as const, work in defaults, and don’t depend on declaration order.
Changing a local variable with the same name does not change the shared value. In Main, don’t give a shared value the same name as a const, function, object, or import alias. Pass values calculated during a run through function arguments and returns.
Prebuilt icons: searchable PNG menu pictures
Ask Studio for one of the built-in PNG icons below. Studio makes them locally. Existing PNGs still work; SVGs aren’t supported.
Example: {"icon":"clock","size":128,"foreground":"#FFFFFF","background":"#2563EB"}. Choose a size from 32 to 256 pixels (128 by default). Use hex colors such as #2563EB, with two more digits if you need transparency. Reuse the same image for rows that mean the same thing.
Prebuilt icons for focus, time and media
- clock: time, schedule, duration, focus session.
- timer: countdown, stopwatch, pomodoro, focus duration.
- calendar: date, agenda, appointments.
- play: start, run, resume, launch.
- pause: suspend, pause playback.
- stop: finish, stop session.
- music: song, audio, playlist.
- volume: speaker, sound, loudness.
- bell: reminder, notification, alarm.
Prebuilt icons for navigation and organization
- home: dashboard, main menu, return home.
- grid: apps, overview, launcher, categories.
- search: find, inspect, lookup.
- folder: files, projects, documents.
- book: reading, reference, knowledge, notes.
- settings: preferences, configure, tools.
- info: help, about, details.
- sort: reorder, organize, sorting.
Prebuilt icons for actions and status
- add: create, plus, new item.
- remove: minus, subtract, remove item.
- check: complete, confirm, done, success.
- star: favorite, featured, rating.
- heart: wellbeing, liked, health.
- bolt: quick action, energy, automation.
- target: goal, aim, focus objective.
- trash: delete, discard. Keep destructive confirmation separate.
- share: send, share sheet, export.
- download: receive, save, import.
- upload: upload, publish, transfer.
- refresh: reload, retry, update, synchronize.
Build and attach a prebuilt icon
Use the real project image path in the row’s image: asset("EXACT_RETURNED_PATH") field. Keep the row’s title and stable value even when it has an icon.
If none of the icons fit, Studio can make a simple image using up to 64 rectangles, circles, or lines on a canvas up to 512 pixels across. Choose shapes or a built-in icon, not both. Custom fonts, text, CSS, and vector markup aren’t supported.