How to use shell script + regex with macOS Shortcuts (to access iPhone ID)

richard moult
4 min readAug 30, 2022

--

In this post we’ll explore how to use shell scripting and regex inside a macOS Shortcut app.

As an iPhone developer we constantly need to access device identifiers, it can sometimes be cumbersome to explain the process to none technical folk. So to solve this problem I thought a macOS shortcut could make the process much easier.

If regex and scripts sound a little scary check out this post using AI to solve those kind of issues.

For those that want to skip to the end here’s the final script, https://www.icloud.com/shortcuts/f72a413750034a2dab77e3322e85d8a7. In case that link no longer works I’ll post an image of the shortcut below.

So lets get cracking…

On your Mac (Monterey) start by opening the Shortcuts app

  1. Click “+” to create a new Shortcut
  2. Name the Shortcut “Find My iPhone ID”
  3. Add the action “Run Shell Script”, replace the default command echo "hello world" with
system_profiler SPUSBDataType | sed -n ‘/iPhone/,/Serial/p’ | grep “Serial Number:” | awk -F “: “ ‘{print $2}’

If you have no iPhone connected to your Mac, or if you have an old or new iPhone the output from the above script will differ, so we need to introduce a little logic and our friend regex.

For whatever reason, my brain never seems to remember regex, but for our needs here its relatively simple. But if you need a reminder you can go here or need to test some ideas you can go here.

4. The next step is to add the action Match Text, who’s first property is [0-9a-zA-Z] (thats the regex bit), in our simple case we just need to look for a string that is 24 characters long, so switch that default property to ^\w{24}$ .

5. The regex above will work if its a new iPhone or fail if its an old phone or no iPhone is connected to your mac. If its a new phone we need to inject a into the string. So the next job is to add the action “if” and if the regex has any value then we need to add another shell script to inject our character.

A shell command to inject a -character at index 8 looks like…

sed ‘s/.\{8\}/&-/’

Our shortcut should now look a little like this…

Keep a close eye on the Input that needs to be the matching text.

So now we need to handle the case of an old iPhone with a different id length.

6. In our previously added “if” action we now need to fill in the “Otherwise” section. btw there are so many ways to do this this is just one method.

We need to repeat the previous steps with a few tweaks. Add in another new action Match Text but this time use the regex ^\w{24,40}$ in Shell Script Result,which means only accept the output if its between 24 and 40 characters in length. Add in another “if” action Matches has any value and use another action “Show Result” to display Matches .

7. Finally in our second “Otherwise” add the action Show alertwith text something like No id found please make sure iPhone is connected to mac .

That should all looks something like…

8. Final step is to add your Shortcut to the menu bar. In the right planel select Details and tick Pin in menu bar

Congratulations you have now unlocked your shortcut shell script and regex badge 🥳.

Check out the next post to see how you can use AI to make the regex and scripting super simple.

If you enjoyed this blog post, you’ll love the book packed with plenty of real-world examples and AI integration — dive deep into Shortcuts.

For more interesting Shortcut ideas check out this link.

If that post helped and you like the idea of planting trees, all you need to do is follow me. Once I hit the magic number of Medium followers it will allow me to donate to these guys. Many thanks.

--

--