Matt mends my magic variables

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 Matt's SWA fix Step 01 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 Matt's SWA fix Step 02 We both loop through all the entries collected in Step 1
3 Matt's SWA fix Step 03 Here’s where Matt takes my three actions and reduces them to two. Instead of getting the Title from the current entry as a separate initial step, he accesses the Title directly as a property of the Repeat Item and splits it…
4 Matt's SWA fix Step 04 … then takes the last word, which will be the confirmation number.
5 Matt's SWA fix Step 05 He then does the same step-saving procedure to split the Location
6 Matt's SWA fix Step 06 … 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 Matt's SWA fix Step 07 Same idea. He doesn’t first extract the Start Date and then calculate an alert time from it; he accesses the Start Date from within the calculating step.
8 Matt's SWA fix Step 08 This is where it all comes together. The Location, Start Date, and End Date are taken directly from the properties of the calendar entry that is the current Repeat Item. 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 Text step.
9 Matt's SWA fix Step 09
10 Matt's SWA fix 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 the Get Details of Calendar Events action? Let’s look at an example from the shortcut: the splitting of the Location field into words in Step 5.

We start by dragging the Split Text action into the shortcut. If Shortcuts doesn’t automatically set the thing to be split to the Repeat Item, choose it from the list of possibilities. Also change the default separator from New Lines to Spaces. The step should now look like this:

Split Text action with Repeat Item as its target

To get a particular property of the Repeat Item, we tap on it to bring up this selection list:

Selection list for properties of a calendar event

The Title is the default property, but we can change that by scrolling down and selecting Location.

Select Location property from list

Now it’s in the final form of Step 5.

Finalized action step

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.