[SOLVED] Create New Folder and Increment

One thing I always thought Finder could have is the option to create a folder inside the selected folder. But in list view, even if I select a folder and the folder is expanded, when I hit Shift CMD N, it always creates the new folder on the root, not inside the selected folder.

I found this AppleScript and I was able to modify it to match what I need, but only if I have a fixed path. When I try to use a KM variable, it doesn't work. I'm sure it's super simple and obvious to someone who understands AS, but I can't seem to figure it out.

Create Folder inside Selected Folder.kmmacros (3.6 KB)

If I remove the first section with the variable (the first 4 lines) and make the commented out section "active" again, it works. I just can't seem to make it work with a variable.

Btw, the section is just a copy from another macro I have, it's not that I fully understand what it's doing, at least not the first line, if it's even necessary...
Either way, with to with it, it doesn't work

Looking at this page, it seems that there's more info on this, but I can't seem to understand something there:


### Requires Keyboard Maestro 8.0.3+ ###
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set kmLocalVar1 to getvariable "Local__SomeLocalVariable" instance kmInst
	setvariable "Local__FromAS" instance kmInst to "Variable set in AppleScript."
end tell
log kmLocalVar1

I don't understand the line
setvariable "Local__FromAS" instance kmInst to "Variable set in AppleScript."

What is it for? What is it doing? What is Local__FromAA and Variable set in Applescript?

I understand the first line. It's getting the local variable set in the macro and it's setting a new variable for AS, called "kmLocalVar1". So far so good. The second line doesn't seem to make sense...

manual:Variables [Using_Variables_in_Scripts]

Local variables (i.e. those with names prefixed with local) can't be used directly in scripts without specifying a run-time instance identifier.

If you dropped the Local_ prefix from each of those names, then you wouldn't need:

  1. To get a specific run-time instance identifier (with set kmInst to system attribute "KMINSTANCE")
  2. to follow getvariable SomeName with the "instance kmInst" specifier
  3. to insert "instance kmInst" in the setvariable assignment of the name FromAS to the string value "instance kmInst"

1 Like

It's going the other way -- setting the KM variable Local__FromAS to the value of the AppleScript variable "Variable set in AppleScript".

It's not supposed to make sense as a script, it's just a demonstration of passing values in either direction when using KM Local or Instance variables.

I also think it's incorrect -- while you can use spaces in AppleScript variable names, you do so within pipes and not double-quotes. So for the variable Hello World:

set |Hello World| to 2
display dialog |Hello World|

The Finder creates the new folder at the current "insertion location", which is generally the target of the frontmost Finder window -- so inside the directory shown in the title bar of the window. Selecting a folder in that window, in either icon or list view, doesn't change that.

You don't need to pass that variable in anyway, you can do it all in AS using the Finder selection:

tell application "Finder"
	make new folder at item 1 of (get selection) with properties {name:"Untitled"}
end tell

...though you'll want to check that only one item is selected, that it is a folder, and so on.

1 Like

To cover the increment part of your question, how about....

New Folder in Selected Folder.kmmacros (23 KB)

Macro screenshot


So let me see if I understand it:
The first line grabs the variable set outside AS, for example from Variable to Text so it can be used in the script.
The second line does the opposite. Sets a variable with a value inside AS to then "output" it to be used in the macro.
Is that is?

In that case I could do all that verification prior to going to the AS action, so when it runs the script it already checked all of that (or showed me an error message), then runs the script you provided

Thanks. That works too.
Great workaround!

Can you see why the AS isn't working?
I would like to at least understand (and learn) why that is when the path is not fixed and I'm using a variable instead. If possible at all, of course

Ok I was checking it again and I want it to be triggered by the same shortcut used now to create a new folder (Shift+CMD+N), so I will need to go and check your macro and mine and how I will be able to achieve this once I go back home tonight.

Not quite -- it isn't setting a variable in AS, that would have been done elsewhere.

You can read the line quite literally, as long as you remember that it is happening within the tell application "Keyboard Maestro Engine":

"Set the KM variable Local__FromAS of the execution instance kmInst to the value of the AppleScript variable |Variable set in AppleScript|".

Splitting things up may make it easier to follow. Does this make sense?

Var Passing Demo.kmmacros (3.7 KB)


Order of precedence in line 3, where you getvariable. The error kind of tells you that -- AS can't make the UUID into an alias. But there's a more subtle problem -- the path to the folder is a string in the format of a POSIX path ("/Users/danny/Desktop/") and AS can't directly make an alias from that, it needs to be of class POSIX file first. So:

set folderPath to (((getvariable "Local__Path" instance inst) as POSIX file) as alias)

I've done extra parentheses so you can see the order of operations. Working from innermost outwards:

  1. Get the value of the KM variable Local__Path of this execution instance
  2. Cast that to class POSIX file
  3. Cast the result of that to class alias
  4. Put the result into the AS variable folderPath

This macro performs three different actions for ⇧⌘N, depending on the current Finder selection:

  • Multiple Items: Selected items are added to a new folder.

  • No Selection or Single File: Creates a new folder in the current directory. This is the default behaviour of ⇧⌘N.

  • Single Folder: Adds a new folder within it (and increments).

New Folder Actions .kmmacros (25 KB)

Macro screenshot

1 Like

Yes this works too. Thanks!

I just removed the action to emulate the CTRL+CMD+N, because I like having those 2 separate so I know exactly what I'm doing.