March 11, 2020 at 10:08 PM by Dr. Drang
During my slow public journey to becoming a decent Shortcuts programmer, Matt Cassinelli has often been my kindly guide, steering me away from the sharp rocks and back onto the smooth path. He did that again today with an improved version of the SWA Calendar Fix shortcut I wrote about yesterday.
Understanding magic variables is the key to writing idiomatic Shortcuts. While I’m much better at using them than I used to be, I still have some blind spots, and I often don’t use them as efficiently as I should. Let’s go through Matt’s improved version of my shortcut and see how a master makes it both shorter and more readable.
Here Matt’s shortcut. You might want to look at it side by side with my original.
|1||This is the same as my first step. It returns a list of all the calendar entries for the next 30 days that came from Southwest Airlines|
|2||We both loop through all the entries collected in Step 1|
|3||Here’s where Matt takes my three actions and reduces them to two. Instead of getting thefrom the current entry as a separate initial step, he accesses the directly as a property of the and splits it…|
|4||… then takes the last word, which will be the confirmation number.|
|5||He then does the same step-saving procedure to split the…|
|6||… and take the last word, which is the flight number. Notice how the property name appears in Steps 3 and 5, making the code more self-documenting.|
|7||Same idea. He doesn’t first extract theand then calculate an alert time from it; he accesses the from within the calculating step.|
|8||This is where it all comes together. The, , and are taken directly from the properties of the calendar entry that is the current . No need to refer to separate magic variables, just different properties of the same magic variable. Even the new title can be composed on the fly instead of making it in a separate step.|
|10||The deletion of the original entries is the same as in my shortcut.|
Matt’s shortcut isn’t better just because it’s shorter, it’s better because it’s clearer. The extra steps mine has get in the way of understanding what it’s doing.
So how do you access the properties of a calendar entry without using theaction? Let’s look at an example from the shortcut: the splitting of the field into words in Step 5.
We start by dragging theaction into the shortcut. If Shortcuts doesn’t automatically set the thing to be split to the , choose it from the list of possibilities. Also change the default separator from to . The step should now look like this:
To get a particular property of the, we tap on it to bring up this selection list:
Theis the default property, but we can change that by scrolling down and selecting .
Now it’s in the final form of Step 5.
I’ve done this selection of properties several times in the past, but for some reason I find it easy to forget and end up back using my old, inefficient ways. Maybe writing this post will help it stick in my head. If not, there’s always Matt.