Combine MP3 Files Macro (v10.0.1)

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.

cat ~/Downloads/File\ 1.mp3 ~/Downloads/File\ 2.mp3 ~/Downloads/File\ 3.mp3 > merged.mp3

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.

Combine MP3 Files Macro (v10.0.1)

Combine MP3 Files.kmmacros (5.9 KB)

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.

1 Like

I see what you are talking about. However, the macro still fails when the file path has spaces in it.

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:

image

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.

Hey Guys,

I'm strongly suspecting a problem with the Execute a Shell Script action in Keyboard Maestro 10.0.1.

I've run this up, down, and sideways, and it always says files don't exist – even though the file paths are quite obviously quoted in the command.

Echoing them out, copying them, and testing the quoted paths in the Terminal works fine.

If I compose the complete command in a variable in the shell action and then use eval to run it it works...

I've got a test-case out to @peternlewis, and we'll see what he comes up with.

-Chris

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.

You guys and your bug claims.

Rule 1: If you are quoting things, you are doing it wrong.
Rule 2: You have to quote things.
Rule 3: Refer to rule 1 & 2.

A shell environment variable cannot contain a list of quoted strings (well, ok, sure, it can, but it won't work).

In your script, with your three files selected, this line:

cat $KMVAR_pathList

Gets the following parameters:

  • '/Users/peter/t/Archive/Test
  • File
  • 1.txt'
  • '/Users/peter/t/Archive/Test
  • File
  • 2.txt'
  • '/Users/peter/t/Archive/Test
  • File
  • 3.txt'

The quotes are meaningless characters in this context.

See this similar discussion:

1 Like

Hmm...

Okay, thanks Peter.

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.

-Chris

No. Well, maybe, but not really relevant.

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.

-Chris

Fair enough.

Yes, both Shell variables and Environment variables behave the same.

Or, more generally, quote inside variables never mean anything to anyone unless you actively write code to process them.

1 Like

:slight_smile:

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 think I finally got it working based on the link Peter shared.

Combine MP3 Files Macro (v10.0.1)

Combine MP3 Files.kmmacros (24 KB)
image

3 Likes