I'm trying to get an mp3 duration in seconds for my RSS Podcast feed.
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"
Thank you sir, that fixed it!
Does anyone know of a way to do this with WAV files?
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...
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.
June 21, 2021, 2:34am
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>
As I was typing a reply,
@kvanh provided the solution, which was to add the path to the program. Thank you both!
Now, I'm going to figure out how to:
Store the result in a variable
Round to the nearest full second
Append it to the filename of the WAV file
June 21, 2021, 3:07am
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:
mdls is /usr/bin/mdls
Have you changed the path on your system?
You shouldn't for most normal macOS-native executables. (See Post
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:
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.
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?
June 23, 2021, 4:26pm
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.
Run program in path test.kmmacros (2.4 KB)
You mean then macro in post
You've put a
text variable in a calculation field.
Next time please post the actual macro, so it can be tested. Just eyeballing something is PITA and makes real debugging impossible.