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