The "If" action is probably my single favorite action in Shortcuts. That's because it lets you build one shortcut that does multiple things, depending on whether certain conditions are met.
For instance, I've got a shortcut that plays my Apple Music Chill Mix most of the year, but once December rolls around, it will play from one of Apple's Christmas playlists. I like the "If" action because it changes what a shortcut does depending on the context, and my Seasonal Tunes shortcut is a prime example.
Here's how you can navigate working with the If action in your own shortcuts, while also getting a look at some other scripting features, like working with number ranges.
Working with "If"
You'll find the "If" action in the Scripting menu, of course, when you go to add an action to a shortcut. You'll find it under the Control Flow subsection.
I find that If works best "If" you use it as the second or third part of a shortcut. You establish some initial conditions, like having the shortcut fetch the status of your HomeKit devices or get the current date, then the "If" action goes to work, executing the remaining parameters of the shortcut.
Let's take a look at how the If action can combine with calendar and music actions to create a contextual music player shortcut.
Tunes for the season
I have several shortcuts centered around music. This includes the one I call 'Seasonal Tunes,' which uses the "If" action to determine what music should be played. While most of the year, this shortcut will play my Chill Mix on Apple Music, when we get to around Thanksgiving, that same shortcut will start playing Apple's Essential Christmas playlist. And it's all thanks to the "If" action.
Here's how you build this shortcut, piece by piece.
First, you'll want to grab the AirPlay, which is in the Scripting action menu under Device. I recommend setting its parameter to "Ask Each Time," which will appear on the action as "Device." This lets you choose whichever speaker or another audio source you want to play music through each time you run the shortcut.
Next, we have the calendar actions, both of which can be found by searching for them when adding actions to your shortcut. First, you want to get the Current Date action. This establishes the current date as a variable that can be used within the shortcut.
Next, you'll want the Get Time Between Dates action. This one requires some modification. The output of this action is ultimately what determines whether the shortcut plays the Chill Mix (or whatever other playlist you designate) or Christmas music.
You'll notice that this action has three blue parameters. By default, they are Current Date, Date, and Minutes. Leave Current Date as it is, but tap Date, delete it, then type in 'December 25 at 11:59 PM.' You could leave it at 'December 25,' but Shortcuts will default to 12:00 PM, and we want to Christmas music to play all day on Christmas, so we need to specify the time. Finally, tap Minutes and change it to Days, because it'll make the next part a lot easier.
Finally, we come to the "If" action, which is really going to make this shortcut sing. When you insert it, you'll notice that it already has Time Between Dates as a parameter, which is the output result of the previous action, so we don't need to add it ourselves.
Tap Condition and choose 'is between' from the bottom of the list. Tap the first 'Number' parameter and enter 0, then tap the second 'Number' parameter and enter 30. This will mean that the shortcut will play Christmas music in the 30 days leading up to Christmas, so you could also pick something like 25 if you want to keep Christmas music strictly in December, or 54 if you want to start celebrating on November 1.
Now it's time for the music actions. They're both the Play Music action found in the Media section when you add an action.
Put the first Play action directly under the "If" action. This is going to cause Time Between Dates to appear in the Play action, so you'll want to delete that. Tap the now-clear Music parameter, then tap through the menus or use the search bar to find the playlist or album you want to play during the holiday season. I'm using the Essential Christmas playlist from Apple Music. Tap the little '+' button to add the whole album or list to the shortcut.
The second Play Music action works in much the same way as the first; you just put it in a different place. Drag the action to sit between the Otherwise and End If pieces of the If action. Clear the Time Between Dates parameter once again, then just like you did for the first Play action, find the album or playlist that you want to play when you're outside the holiday season.
And there you go. The shortcut is built, and when you activate it, it should play either your regular playlist or album or Christmas music, depending on when you're reading this. If you've built it following these rules, Christmas music should play from your specified holiday season until 12:00 a.m. on December 26.
If you want more examples of how you can use the "If" command, I've already offered one shortcut that shows how the "If" command can help you control your HomeKit lights, creating a one-word toggle switch to turn lights on or off. There are many different ways to use the "If" command, and I'm sure we'll explore it more as time goes on.
What do you want to see next?
Is there anything you want to know about scripting in Shortcuts? We've gotten acquainted with the shortcuts menu, and now we've spent some time with the "If" command. Let me know in the comments if there's something you want to see next time.
Get the best of iMore in in your inbox, every day!
Joseph Keller is the former Editor in Chief of iMore. An Apple user for almost 20 years, he spends his time learning the ins and outs of iOS and macOS, always finding ways of getting the most out of his iPhone, iPad, Apple Watch, and Mac.
Thank you for signing up to iMore. You will receive a verification email shortly.
There was a problem. Please refresh the page and try again.