Sub topic 2
Making Decisions and Repeating Steps
Assume you know how to create actions and connect their output in the Shortcuts app. This chapter uses macOS files and basic Finder operations.
Making decisions and repeating steps
Control flow determines which actions run and how often. An If action tests a condition, such as whether a file is a PDF. Its Otherwise branch handles every other case.
A Repeat action runs the same actions for each item or for a chosen number of times. For example, you can repeat over every file received by a shortcut, rename each file, and then combine the results.
Use Choose from Menu when the user should select a path, such as “Archive” or “Share.” Each menu option has its own action sequence.
Working with text, files, and other content types
Shortcuts passes typed content between actions. Text, files, images, URLs, dates, and numbers are different content types. Some actions convert content automatically, but explicit conversion is safer when a shortcut behaves unexpectedly.
For example, Get Text from Input extracts readable text from a file or other input. Get Contents of Folder produces a list of files. A Repeat action can process each file in that list.
Use variables when a value must be reused later. A variable can hold the original file, transformed text, or a result from an earlier branch.
Receiving files and text with Shortcut Input
Shortcut Input is the value supplied when a shortcut starts. In the shortcut’s details, set the accepted input types, such as Files and Text. Finder, another app, or a share sheet can then pass matching content into the shortcut.
Place Shortcut Input near the start of the action sequence. Add Repeat with Each when multiple files may arrive. Test both a single item and several items because list handling can change the result.
Running shortcuts from macOS
Enable Pin in Menu Bar to run a shortcut from the menu bar. Add it to the Dock for a regular application-style launch. Enable Use as Quick Action to make it available from Finder’s context menu or the Services menu. You can also assign a keyboard shortcut in the shortcut’s details.
Choose the trigger that matches the input source. Finder Quick Actions are useful for selected files, while a keyboard shortcut is better for a repeatable personal command.
Exercise
A shortcut should process every selected Finder file. Which actions should you use?
Show answer
Enable Quick Action input for Files, use Shortcut Input, and place the processing actions inside Repeat with Each.