Using Variables Prepend and Append

KM 9.0.2 has new Prepend and Append variables, Cool​:+1:t2: I haven't used variables Not Cool :-1:t2:. This looks like a spot to jump in :astonished:!

I've set up a bunch-o-marcos to Prepend/Append the numbers 1-20 that I use on file names to sequence them within folders when they are sorted by name. They are simply UI actions that types return, up or down arrows, numbers, etc. Gets the job done nicely. Seems like these new variables would do the job more eloquently.

I know you have to declare a variable before using it and some characters are reserved and can't be used and that's about it. Not enough to get anything done.

Currently I have around forty macros (~20 to prepend and ~20 to append) which makes for a long list to scan and pick from.

For a first toe in the water I'd like to replace these UI driven macros with variables to do the same thing.

What would be nice but is way too scripty, which if written for me wouldn't likely help me learn so it goes on the not now list, would be for the macro to evaluate the file names in the current folder and add a prepend or append in sequence with the existing files. That way it could be a single macro to get the job done. I suppose a folder action that did that for any file added would be the best way to go but this is just thinking out loud and not something to follow up on anytime soon.

Thanks in advance for any using variables 101 support :smile:.

Good for you for wanting to jump in deeper!

I'm trying to figure out the best way to help you with this. There's so many different things that you could do.

Let me try this, to see where we're at. This example doesn't do what you need, not yet. But it's probably the best way to do what you want (once it's complete), so I thought I'd start with this.

Look at this and see if you can figure out what it does. By the way, I'm using "Append Variable" like you mentioned.

Oh, and if you make this macro yourself (which is a good idea), it probably won't produce any result unless you select some files in a Finder window first.


You are very noble for wanting to learn and for not having answers handed to you on a platter. But we don't expect knighthood-level nobility here, so you can always ask for help. Based on what you described, it sounds very likely that a single macro could do the job.

DT provided you with a tip that might help you get started, but the way I'm interpreting your question I think you're doing something different with your macros. Are you willing to show one of your macros?


Thanks Dan!

I made, tested, and see what it does. Somehow this acquired the POSIX path to the file and the Display Text action displayed that path in a dialog that popped up.

I looked up ( to be clear about what kind of path is being acquired as I’ve seen lots of references for POSIX pathways with a few references to HFS pathways.

“Notice that in HFS path names, a colon ":", is used as a separator, and in POSIX path names, a forward slash "/" is used as a separator. The other major difference is that in HFS path names, the path name begins with the name of the volume and in POSIX path names, the startup volume doesn't need to be named, only other volumes need to be named.”

Slashes w/o volume reference=POSIX :white_check_mark:

The included pic below is my guessing at what’s happening to clearly display what I don’t understand so you can hopefully fill in the needed missing pieces.

1 Like

Excellent homework! And I'm thrilled with your questions, because it shows me we started in the right place. :smile:

[EDIT] I've just finished typing this, and it makes be realize how may topics this covers. And some of these topics will either totally make sense, or they'll blow your mind. And it's OK if they blow your mind - you won't be the first person to have trouble understanding these concepts.

So be patient with yourself, because if you can end up really understand these concepts, then you've actually passed the biggest hurdle you'll have to going forward.


Good work on HFS vs. POSIX paths. Fortunately, as far as I know, you only need to deal with POSIX paths, which are very similar to Windows paths, except the Mac uses slashes, and Windows uses backslashes. So in KM, we just usually talk about them being "paths".

In Finder, if you right-click on a file, then hold down the Option key, the menu changes and it has an item to copy the file as a pathname, and you get a POSIX path. Most people don't even know about HFS paths.


You never have to "define" them first, like you do with lots of programming languages. Just start using one. Of course, if you want it to contain a value, then you need to put something in it. However:

"Normal" KM variables keep their values even when the macro finishes running. If you go to "Keyboard Maestro->Preferences" and click on the "Variables" tab, you'll probably see some "normal" (aka "global") variables there.

You can name variables almost anything you want. Check out the Wiki for the rules.

But: Variables that start with "Local" only exist while the macro is running. At the start of the macro, they're empty. And they automatically get deleted when the macro ends. That's why I used variable names that start with "Local". Well, I actually use "Local_" (with an underscore), but that's just a personal preference.

Variables that start with "Instance" are basically the same as "Local", but for now the difference isn't relevant, until you start working with what we like to call "sub-macros".

So: Use variable names that start with "Local" until you have a reason not to.

Your questions:


Aside from the "Local" as I mentioned above, it's just a name that made sense to me.

As I mentioned earlier, you don't have to define variables beforehand, so this isn't technically a case of me defining the variable. But since it gets a new value each time through the loop, I could understand why you might think of it as "defining a variable", but again, you don't need to define them.


I chose this name because it's where I'm putting all the paths you had selected in Finder.

Append Variable action:

Whatever I put in the text box will be appended to the end of the variable. So you could read that as "Append what's in the text box to the variable Local_SelectedPaths".

Try this: Add this "Prompt for User Input" action:

Every time the loop repeats (make sure you've selected more than one file in Finder), you can see the value changing.

If you click the "Gear" icon in the "Append Variable" action, you can change it to "Prepend" or "Set Variable". The former puts the text in front of anything already in the variable, and "Set Variable" replaces whatever is in the variable with the data in the text box. Try it to see what happens.


This is how you get the value from the variable, when you're in a text box. You ask why the % in the middle, and my answer is, because that's what the Wiki or Help says to do. :smile:

This is a concept that's hard for some people to grasp, so let me know if this ends up making sense or not.

"This suggest that..."


"How did the local path get acquired..."

I used the action "For each Path in Finder Selection".

"For each [variable name] in these collections:

A "collection" is exactly that. A collection of things. This action "iterates" through each item in the collection. Each time it loops, the variable "Local_Path" contains the next item in the collection.

"The Finder's selection"

In this case, the "collection" is a collection of each file selected in Finder. There's a bunch of "collections" you can chose from.

"execute the following actions"

This is what gets executed for each item in the collection. In this case, it's for the path of each selected file in Finder.

I'm sure you have more questions. Mess around with this and see what you can figure out. You can't hurt anything, unless you add a "Delete File" action, or something like that. :open_mouth:

Let me know how you're doing.

1 Like

@BernSh, I think @DanThomas has answered all of your questions, but I want to focus on one question because it is such a fundamental part of KM:

First, there is no such thing as a "special" variable, or variable name.
Anywhere a KM Action requires a KM Variable, you can enter any name you like (even though often KM will provide a default name), as long as it conforms to a simple rule:

Variable Naming Rule

  • Variable names must start with a letter, and then can contain letters, numbers, spaces, or underscores.
  • Variable names are case insensitive, but their case is remembered.
  • Variable names should not include a function or operator name with spaces around it
    *(eg “ MOD ” , so “A MOD B” would not be a valid variable, although “MODULE” would be fine).

See Variables in the KM Wiki for details.

Please read the above article carefully, and take note of the Variable Scopes:
(there is no such thing as a "normal" variable)

  • Global
  • Local
  • Instance

In general, it is best to use Local Variables unless you have a good reason to use one of the other types. I like to use "Local__" (with two underscores) to denote "Local" variable because if you need to display this Variable in a Prompt, then the "Local__" will NOT be shown.