Copy mp3 File Duration in Seconds to the Clipboard

Hi All,

I'm trying to get an mp3 duration in seconds for my RSS Podcast feed.

I've installed mp3info via homebrew. I've changed the ENV_PATH to include homebrew.

Here is my KM setup:

I select an mp3 file in Finder and hit my keyboard shortcut.

I get the following error:

2020-08-31 16:11:11 Execute a Shell Script failed with script error: No MP3 files specified!. Macro “Copy File Duration in seconds” cancelled (while executing Execute Shell Script).

The file is definitely an mp3.

What am I doing wrong?

Many thanks for your help!

Copy File Duration in seconds.kmmacros (2.6 KB)

In the Execute Shell Script action, “input” refers to standard input, as it would be when using the Terminal. But mp3info doesn't take standard input; it needs the file name as an argument on the command line. So change the “With input from” to “Nothing” and the command to

mp3info -p "%S\n" "$KMVAR_FileName"

Screen Shot 2020-08-31 at 1.52.26 PM

1 Like

Thank you sir, that fixed it!

Does anyone know of a way to do this with WAV files?

Hey Pariah,

Just use an Execute a Shell Script action:

mdls -raw -name kMDItemDurationSeconds <PathToFile>


Thanks, Chris, but I'm getting "mdls: Command not found" errors.

You're not showing your work...  :sunglasses:

mdls is a built-in shell command for macOS. I just checked on a Big Sur system, and found it without issue.

So – let's see what you're doing.


When I run shell scripts from KM I usually have to specify the path to the program so:

/usr/bin/mdls -raw -name kMDItemDurationSeconds <PathToFile>

Right. Sorry.

As I was typing a reply, @kvanh provided the solution, which was to add the path to the program. Thank you both! :smiley:

Now, I'm going to figure out how to:

  1. Store the result in a variable
  2. Round to the nearest full second
  3. Append it to the filename of the WAV file

in the Execute Shell Script action is a drop-down with an option to save the result in a variable.

Use that variable in a calculation action and use the round() function

I'm glad it's working for you.

However – it should have worked out-of-the-box, because the command path is in Keyboard Maestro's native shell path:

echo $PATH

type mdls

mdls is /usr/bin/mdls

Have you changed the path on your system?


Hey Kevin,

You shouldn't for most normal macOS-native executables. (See Post #11)

It's not a good practice to use a full path with shell commands.

  • You may use a shell command once or many times in a given script.
  • Scripts are much easier to read and maintain without paths everywhere.

It's much more efficient to establish a wider path using Keyboard Maestro's environment variable system.

Create a variable named: ENV_PATH

Place your path in it – mine is as follows:


Most folks won't require one so lengthy.

Path in Shell Scripts

If for some reason you must manage paths on a per action basis it's a better practice to export/alter the path once at the beginning of your script.

export PATH=/opt/local/bin:/opt/local/sbin:/usr/local/bin:$PATH;

The above incorporates the change into the existing path.


No. Terminal still reports it as /usr/bin/mdls . How can I check/correct the native shell path of KM?

Thank you. I figured there would be, and a round function.

Can you tell me what I'm doing wrong here? When I try running this I get "Task failed with status 1...while executing Execute Shell Script."

NOTE: I did fix the Display Text action to show Path, not KMVAR_Path after taking the screenshot.

Huh? Given what input?

That is the correct path to the shell command. The thing is that you should not need to supply the full path.

Run this and post the result:

Display Keyboard Maestro $PATH v1.00.kmmacros (5.3 KB)


Here's what it came back with:

Okay, you've clearly changed your path.

Do you have an ENV_PATH variable in Keyboard Maestro?


Yes. Somehow it was changed to what I posted, probably when I was learning to execute Python scripts from KM. Thanks. I just set it to the example you posted above (/opt/local/bin:/opt/local/sbin:/Users/chris/perl5/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/Library/TeX/texbin:).

(updated to match my Mac profile, of course)

Now I can call mdls without the full path, but I still get the errors noted above in my macro. Any advice or insight on that?

Well I'm confused as all heck. Last night I did some testing and no matter what I tried I couldn't get mdls to run unless I used /usr/bin/mdls to execute it. both whereis mdls and which mdls reported mdls as being in /usr/bin/mdls and both those commands executed fine from /usr/bin without a path specification.

Then I decided to test again this morning and damn if it doesn't work for me now. I've no idea. Didn't reboot, didn't even restart KM or terminal since last night.

If anyone else wants to test, my test macro is below.


Using /usr/bin:

without /usr/bin:


Macro Download

Run program in path test.kmmacros (2.4 KB)

You mean then macro in post #14?

You've put a text variable in a calculation field.

Remove the %Variable%...%.

Next time please post the actual macro, so it can be tested. Just eyeballing something is PITA and makes real debugging impossible.