Sub topic 6
Working with text, files and other content types
This chapter assumes you can build a multi-step shortcut, pass output from one action to the next, and use variables and magic variables. You will learn how Shortcuts types its data, how to convert between types, and how to avoid the errors that come from a type mismatch.
Everything has a content type
Each action's output has a content type. Common types are text, rich text, number, date, URL, file, image, and list. The type decides which later actions can use the data and how they treat it.
For example, a Get File action produces a file. If you pass that file to Show Result, you see the file's name and preview, not its contents. To work with the words inside a text file, you first convert the file to text.
Tap a variable in the editor and choose its type, or click the magic variable's pill to see options such as Name, File Extension, or Creation Date. These options are properties of the type. A file has a name and size. A plain text value does not.
Text
Text is the most flexible type, and several actions help you shape it:
- Text holds fixed words and can embed variables inline.
- Replace Text swaps one string for another. Turn on its regular expression option for pattern matching.
- Split Text breaks text into a list by new lines, spaces, or a custom separator.
- Combine Text joins a list back into one text value.
- Change Case switches between uppercase, lowercase, and title case.
- Get Text from Input pulls plain text out of richer content.
Rich text keeps formatting such as bold. Use Make Rich Text from Markdown or Get Text from Input when you need to move between rich and plain forms. Plain text is safer when a later step, such as a shell script, expects raw characters.
Files
Files carry properties you can read with Get Details of Files, including name, size, file extension, and dates. Useful file actions include:
- Get File and Get Contents of Folder to bring files in.
- Rename File and Set Name to change names.
- Save File to write output to a chosen location.
- Get File from Folder to find a file by path.
Because the output of Get Contents of Folder is a list of files, you will often feed it into a Repeat with Each loop. Inside the loop, the Repeat Item variable is one file at a time.
Converting between types
Shortcuts often converts types automatically, but you should convert explicitly when the result matters. The following actions help:
| Action | Converts | Result |
|---|---|---|
| Get Text from Input | Rich text, file, or other content | Plain text |
| Make Text from File (via Get Text from Input) | Text file | Its contents as text |
| Convert Image | Image | Another image format |
| Make PDF | Text, images, or files | A PDF file |
| Get Numbers from Input | Text | Numbers |
| Format Date | Date | Text in a chosen style |
A quick test of what you have: add Show Result after the step. If you see a file icon where you expected words, the data is still a file.
Lists and the single-value trap
Many actions return a list, not one item. Split Text, Get Contents of Folder, and Find Photos all do. If a list reaches an action that expects one value, Shortcuts may use only the first item, or join the items with new lines. Check the count with the Count action. To control the result, use Repeat with Each, or Get Item from List to pick one.
Common errors
- "Shortcut can't continue because it didn't receive any input." An earlier action returned nothing, such as a folder with no matching files. Add an If action to test for "has any value".
- Unexpected file icon instead of text. The file was never converted. Add Get Text from Input.
- Wrong extension after Save File. The saved name comes from the file's own name. Use Set Name first, including the extension.
- A number treated as text. Comparisons like "10 is less than 9" can behave as text comparisons. Convert with Get Numbers from Input first.
Exercise
A shortcut uses Get File on notes.txt, then Replace Text, then Save File. The result is not changed. What is the likely cause, and what action fixes it?
Show answer
Replace Text works on text, but the Get File output is a file, so there was nothing to replace. Insert Get Text from Input between Get File and Replace Text to convert the file to text.
Exercise
Get Contents of Folder returns 12 files. You want only each file's size. Which two actions do you use?
Show answer
Use Repeat with Each over the file list, then Get Details of Files inside the loop set to File Size, using Repeat Item as the input.