I would like to create a macro where I select 2 or more MP3 files in the Finder, press a hot key, and a merged MP3 file is created in the same directory as the files.
You can use this command in Terminal to combine MP3 files.
My current macro almost does this, but something is going wrong with the file paths. I either need to put quotes somewhere, or somehow escape each file path.
You didn't indicate what the error was, but if the quotes are being passed to the shell, as I think they are, then the problem is that you don't have the final <.mp3> inside the quotes.
Those quotes are not delimiters for KM to know where your variable names are. They are for the shell to know where strings start and stop.
There's a 20% chance I'm completely wrong, but try that advice. And I apologize in advance if I'm wrong.
Thanks for the advice. That is most likely part of the problem. However, it did not fix the macro entirely. The macro still creates a properly named MP3 file that is 0 bytes in size. My issue has something to do with the FilestoMerge variable.
Then my second idea is to not use "Quote for AppleScript" and instead use "quote for Shell Script" since you are passing the name to the latter, not the former.
Quotes in shell script are the bane of my existence. I do everything there by trial and error.
Also, there's a flag on the Execute Shell script action that says "include errors", so you m might want to try that. Also, it might help if you give us the text of the error you are getting, especially after you make the aforementioned change.
Also, merging MP3s using concatenation is not really the most reliable way to do that. It can cause problems doing this. Getting a utility is wiser.
Okay I was able to fix your macro. I don't really understand the precise cause at this point (because I'm watching the World Chess Championship Game 3) and I simply removed the quotes from your source files and it worked fine.
I just greyed out your Filter action. And it worked for me. I can't explain the reason why this fixed it. I'll leave that to you.
But I think what was happening is that you had nested quotes, and the internal quotes were being interpreted as part of the filename, which isn't what you intended.
I didn't know your file names contained spaces. Let me think about it (while I'm watching the Chess game.)
As I said, quotes are the bane of my existence. So you could try a different way: instead of quotes, you can escape your spaces using backslashes. KM and the shell have fairly easy commands for doing that.
Another different approach you could adopt is, instead of combining them all at once, do them consecutively by putting this action into your first loop:
This should also fix your problem with spaces in the source file names, because the KM action probably handles that internally if you do it using the above approach.
Great work; good testing. I was busy watching Magnus Carlsen today so I didn't dig deep like you did.
This whole thread made me wonder if KM could use a binary file concatenation action. (After all, KM does support concatenation of text files with a built-in action.) However since 90% of my ideas end up in the kitty litter box, I expect this one to go there too.
Clearly I don't fully understand quoting in the shell as yet... <sigh>
From the thread you link to:
“To summarize that obvious fact I was not aware, quotes in environment variables are treated literally on the variables substitution rather than delimiters as it takes place in command line parsing.”
He says “environment variables”, but that's not correct is it – it should be variables in general.
If you're aware of a really good technical explanation of this somewhere please point it out. Googling gave me a too many hits to parse at the moment.
This is specifically talking about the shell and how it processes variables, so it is only talking about environment variables as there are no other kinds of variables.
In pretty much any other case I can think of, quotes in a variable would have no special meaning, so from that point of view, you are correct that the statement applies to all variables, but it’s not really sensible to be talking about, for example, Keyboard Maestro variables, or C++ variables, when the discussion is about the shell and how it expands variables and arguments.
If KM's pop-up error messages in the upper right corner of the screen were larger, I think I could have spotted this. So just now I googled if that's possible and I found a reasonable answer here. I'll try to remember to do this in the future.
Yes, I was talking about the shell – and I believe you're quite mistaken about that.
Environment vs. Shell variables
• Environment variables are ‘global’ i.e., shared by all shells started AFTER variable is defined.
• Shell variables are only present in the shell in which they were defined.
• Environment variables are inherited by child shells but shell variables are not.
• Shell variable can be made an environment variable by using export command.
The Keyboard Maestro variables you push into the shell are environment variables.
Normal shell variables in scripts are NOT.
Yet they share the same characteristics when handling quotes.
We probably shouldn't be too hard on ourselves – when we can can't see the bug in our approach, we naturally look for it elsewhere : - )
No mammal finds it easy to see the back of their own head very clearly.
Isn't there something about it in the Sermon on the Mount ? Matthew 7 ?
"Beams" and "Motes" in the KJV, but perhaps Aramaic, or Demotic Greek, didn't yet have a word like bug ?
I really like the idea of this Macro and have used it successfully. My question is, because cat is made to combine not just mp3 files, it isn't the best solution for combining mp3 files due to header issues etc. (e.g. after combining two mp3's using cat and using Quick Look, the length shown is only of the first mp3 file of the two combined).
I have installed mp3cat inside /usr/local/bin/ and have given it appropriate permissions and have set my Mac to allow executing it. I'm running MacOS 15.1.