DEV Community

Cover image for Customising 'focus till morning' in Shortcuts app
djchadderton
djchadderton

Posted on

Customising 'focus till morning' in Shortcuts app

I wanted to trigger the Night focus mode on my iPhone using an NFC tag, which should have been straightforward, but Apple has left a few things out of Shortcuts for Focus triggers and dates and times to make it more of a challenge.

The first part is easy. Go to the Shortcuts app, switch to automation from the icon at the bottom and click the plus button, then scroll down to NFC tag and select this (this same process applies whatever you want to use to trigger itβ€”it doesn't have to be an NFC tag).

Add the action you want to use to trigger the Focus mode

Make sure you select "Run Immediately" instead of the default "Run After Confirmation" and scan the NFC tag, if you are using this as a trigger.

You can then add an action to turn the correct focus mode on, but there is no option for "until morning" (or "until this evening" during the day), only until turned off, an event ends, you leave a location or a specified time. But it should, in theory, be possible to emulate the "until morning" option by specifying a time as 7AM, like so:

Add a shortcut action to trigger Focus mode until 7AM

This works fine if you trigger it after midnight, but if before, it throws an error, telling you to choose a time that isn't in the past as it always assumes, oddly, that you mean 7AM on the current day.

You could change this to specify tomorrow at 7AM like this:

Trigger Focus mode specifying tomorrow at 7AM

This will work if you trigger it before midnight, but if after, it will leave you in sleep mode for more than 24 hours unless you turn it off manually.

The answer I found was to combine these in an if statement with some date formatting tricks I had to look up to change the date to a number, as comparing dates directly doesn't seem to be straightforward in Shortcuts. I did this in a new Shortcut to be triggered by the automation. Here's the sequence:

  • Add a Date action and set it to current date.
  • Add a Format Date action to use the output of the previous action and click Show More.
  • Change Date Format to Custom and use HH as the Format String to pick just the hour (in 24-hour clock format, so 0 to 23)
  • Add an If action and make sure Formatted Date is selected in the condition. Click on it and choose Number as the Type. You can then choose is greater than as the condition and add 7 for 7AM (or whatever you like) as the comparison.
  • Under the If, add an action to turn on the Focus mode tomorrow at 7am.
  • Under Otherwise, add an action to turn it on at 7am, i.e. on the current day.

Here's the final shortcut:

Shortcut to trigger Night Focus at the next 7AM, whatever the current time

Go back to your automation and add as the only action Run Shortcut...

Add Run Shortcut action to automation

...then search for this shortcut under whatever name you gave to it.

Select shortcut to run

And that's it! Not as straightforward as I thought it should be, but it works, whatever time of day or night I decide to go to bed.

Top comments (0)