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.
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
### 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...
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.
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
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.
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 classPOSIX 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:
Get the value of the KM variable Local__Path of this execution instance
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?
Also, I want to understand the thought process in the sequence:
For example when I have a test macro with just the Create Unique action, I get a new file with no extension, like this:
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?
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.
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".