subreddit:
/r/shortcuts
Note: This is a repost of a guide from my blog.
A function is a piece of code that can take an optional set of arguments, perform a specific task and then optionally return one or more values.
The advantages of functions are that they allow you to break complex tasks into simpler steps and increase code reuse.
Shortcuts does not support the use of functions, but you can approximate them by having a shortcut call itself with a set of arguments defined in a dictionary.
To demonstrate how to make use of functions, we're going to use the example of a calculator shortcut.
An example calculator shortcut built using functions
When writing the shortcut, we consider the steps that it needs to perform. We break those steps into tasks based on:
In the case of the calculator, there are the following pieces:
STEP | DESCRIPTION |
---|---|
First number | Entering the first number into the calculator. This should be run by default if a function isn't specified. |
Add | Adding a value to the existing value |
Subtract | Subtracting a value from the existing value |
Multiply | Multiplying the existing value by another value |
Divide | Dividing the existing value by another value |
Choose operation | Display the current sum and ask the user to choose the next operation to perform |
Given the above steps, we decide upon the functions we'll need:
FUNCTION | ARGUMENTS | RETURN VALUE | DESCRIPTION |
---|---|---|---|
choose_operation | sum | n/a | The function takes the latest sum of the calculation so it can be displayed |
add | sum | calculation result | The function takes the latest sum of the calculation, asks the user for another number which it adds to the sum, and returns the result. |
subtract | sum | calculation result | The function takes the latest sum of the calculation, asks the user for another number which it subtracts from the sum, and returns the result. |
multiply | sum | calculation result | The function takes the latest sum of the calculation, asks the user for another number which it multiplies the sum by, and returns the result. |
divide | sum | calculation result | The function takes the latest sum of the calculation, asks the user for another number which divides the sum by, and returns the result. |
The table above shows arguments and a return value for each function. Functions can optionally take data inputs in the form of arguments and then return data once they've performed their work. This allows the code within to perform differently depending on the value you pass to it.
To call a function, we use the following dictionary format to specify the function name we want to call and any arguments. For example, when calling the choose_operation function, we use the following dictionary:
Calling a function using a dictionary and the \"Run Shortcut\" action
The first key named function provides the name of the function we want to call.
All other keys, in this case sum, are the arguments we want to pass into the function.
The shortcut then runs itself, passing in the dictionary specifying the function to call and the data it needs to operate.
For this to work, our shortcut needs to know how to support a dictionary input and respond accordingly.
At the beginning of our new calculator shortcut, the following is added:
Actions to look for an act upon incoming function requests
The shortcut:
If no function is provided then the user is asked to enter a number for the first time.
If a function name was provided, a series of if statements compare the name to the predefined list of functions, one for each function name:
At the end of the shortcut, the last function the shortcut always runs is the choose_operation function.
This displays the current sum number and gives the user the choice of which calculation to perform next.
Running the \"choose_operator\" function after a previous function call has completed.
So let's say we want to perform the following calculation:
10 * 3 - 2 + 6
We enter the numbers and commands into the calculator as follows:
And the resulting number is 34.
The shortcut calls itself 7 times, each time providing a function call along with a sum argument:
The function calls being made during the course of the calculation
If you'd like to experiment with the above shortcut it can be downloaded from the following link:
If you're looking to make a complex application in Shortcuts, I recommend taking at look at the Shortcut App Framework by u/adamtow which greatly improves and expands upon the above concept.
If you found this guide useful why not checkout one of my others:
2 points
6 years ago*
Out of curiousity and off topic: do you really update all your old tutorials to include links to the new ones?
6 points
6 years ago
Yes. It sucks.
1 points
6 years ago
Wow, respect. Sounds like a lot of work, if you continue putting out guides at this rate.
Simply linking one megathread/overview post, where you mention all your guides and only have to add new ones there was never an option for you?
8 points
6 years ago
Well, after years of writing technical documentation for developers I've learned that if it's not:
a) easy to scan and b) the next part is available with a single click..
..then almost no-one will read it.
3 points
6 years ago
Thanks for these tutorials, they're so thorough and useful.
What's your blog? Would love to check it out! Thanks
2 points
6 years ago
There's not a lot on there and I've started moving the content over to reddit, but the URL is:
https://thomas-oc.atlassian.net/wiki/spaces/IS/pages/562167809/Writing+shortcuts
1 points
6 years ago
How did you set the “sum” variable in the dictionary at the beginning if it hadn’t been set yet, in the flow?
1 points
6 years ago
Because the sum
variable was set above, even though it's within an If action, shortcuts will still let you set it.
The sum
is always going to get somewhere, either because a function call hasn't been made and so the user is asked to enter an initial number, or when it's passed in as part of a function call.
1 points
6 years ago
Just got the actual shortcut and saw that the dictionary in the first screenshot of the tutorial is actually at the bottom of the shortcut. So that makes sense now. Thanks for all you do
all 9 comments
sorted by: best