How I wrote a shortcut to calculate the length of playlists


This weekend I made a shortcut that takes a list of songs, adds up the total duration, and shows it in a notification.

The first part was easy. The Shortcuts app has a great action that can tell you anything about an iTunes Media file (or any other media file), including its duration. I whipped up a shortcut to cycle through a list of music tracks, adding up the durations along the way. It took five minutes, tops.

Then things started to go wrong. The shortcut returned the total duration in seconds. I don’t know about you, but for me, a number like 4,166.867 isn’t that useful. I prefer something like 01:09:26, or 1 hour 9 minutes and 36 seconds. The problem was, I couldn’t get from one to the other.


For humans, converting from seconds to hours, minutes ands seconds is straightforward (at least for smallish numbers). But how do you tell a computer to do it? You can tell it to divide by 60 to give minutes, but as minutes aren’t decimal, something like 4:30 becomes 4.5 minutes. That’s fine, I guess, but hardly elegant.

I tried all kinds of crazy tricks before turning to the internet for help. Shortcuts-specific help is tricky to find, because the term “shortcuts” is too common to narrow a Google search.

In the end, though, I found what seems like a rather elegant, and yet also rather cumbersome, way to do it.

Shortcut to calculate the total play time of a list of songs

This is the shortcut. It takes any number of selected songs, totals their durations, and returns the result in an hh:mm:ss format.

Why? Because I make tapes (yes, olde worlde cassette tapes) of my own music, and sometimes of other people’s music. This music always starts out in a folder full of WAV files. I needed a quick way to tell if a playlist would fit on one 45-minute side of a cassette.

A shortcut long enough to use as a winter scarf.
A shortcut long enough to use as a winter scarf.
Photo: Cult of Mac

As you can see, the shortcut starts out fine. The first section — everything until End Repeat — is straightforward. That part is what does the actual work of the shortcut: summing the durations.

The rest is there to turn seconds into human-readable text.

How to change seconds into something useful

You can look at the shortcut screenshot to see how it works (and download the shortcut to use it). The gist is that the shortcut takes the total running time in seconds, then adds those seconds to an arbitrary time. I set the time to 00:00:00. Why do this? Because it gives a result that’s in a proper date format. It also takes care of adding seconds (and not getting confused).

Then, I tell shortcuts to format the result as HH, mm, ss (for hours, minutes and seconds). If I don’t do this, the result will show up as an actual date and time. If the total play length was 33 minutes and 4 seconds, for example, it would show up as 12:33AM.

Then I use a regular expression to replace the date with nicely formatted text, which — using the previous example — will look like this: 00:33:04. I don’t know much about regular expression, but that doesn’t matter, because lots of people do, and they talk about it on the internet. For my shortcut, I adapted this regex from a post on Reddit by Mvpetri. Thanks a lot for that. It really simplifies the entire workflow.

My shortcut then shows a notification.

Does it work?

Photo: Cult of Mac

Yes! It works like a charm. The screenshot shows the test version, which lets me pick the music tracks from iCloud Drive right inside the Shortcuts app. But the final version, which you can download here, omits this step. It is designed to be used from the share menu inside the Files app (or the Dropbox app or wherever). And because it only accepts media files, you can drop a whole folder of files on it, and it will only process the music files. It should also work for video, but I haven’t tested that.

In the end, this article isn’t so much about a niche shortcut like the one I made. It’s more about how powerful Shortcuts can be — and also about how frustrating it is when you stray away from the app’s built-in actions.

Still, this is very useful shortcut for me. And if you’re looking for a way to convert seconds to a human-readable format, you can just copy and paste the relevant section.

Let’s block ads! (Why?)

Source: How-To – Cult of Mac

Get real time updates directly on you device, subscribe now.

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time
You might also like

Leave A Reply

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More