[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:

https://wiki.keyboardmaestro.com/AppleScript

### 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

2 Likes

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)

Image

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.

I'm revisiting this macro you shared, because yesterday I got this error:
Action 13889627 failed: Get File Attributed failed to get path for %FinderSelection%

This is related to the second action in the macro.
My guess is that if I have 2 or more items selected, Keyboard Maestro can't really get the attribute of multiple items, so shouldn't the Get File Type action come after the MULTIPLE ITEMS action, like this?

image

Also, I want to understand the thought process in the sequence:
image

For example when I have a test macro with just the Create Unique action, I get a new file with no extension, like this:
image

But when I use your macro, this file isn't even created. I would guess that's because everything happens so fast that the system deletes the file even before it gets shown in Finder?

So you're basically using Create Unique to just set a unique path to the new folder?
That means that using "Create Unique" will always check if there's a file/folder with the name you specified (in this case "New Folder") in the selected path, and if so adds the "copy 1", "copy 2", etc, right?

Yup! Oversight on my part.

Yeah it's a weird one to get your head around. You have to create a unique file and log the path generated, then delete it and use that path to create your actual file.

Yup.

Exactly.

1 Like

Awesome! Thanks for clarifying everything.
It's funny because by the time you created this macro, I was still very new to KM, even though I have been using it for maybe a year and all those actions were still pretty new to me (and a bit confusing). Now that I look at them, everything is so much easier to understand. That's the beauty of these posts when you look at old ones and think "wow, this used to make my brain melt, yet now this is part of my almost-daily macro creation". :slight_smile:

1 Like