Following the instructions here, I can't seem to make this script work when I select an image in Finder and run the macro (I'm using a FOR EACH action)
tell application "Keyboard Maestro Engine"
set filePath to getvariable "Local__file"
end tell
set filePath to ((current application's NSString's stringWithString:filePath)'s stringByExpandingTildeInPath) as text
set imageList to alias POSIX file filePath
importToAlbum(imageList, "Testing")
on importToAlbum(imagePathList, albumName)
tell application "Photos"
set albumNames to name of albums
if albumName is in albumNames then
import imagePathList into album albumName
else
make new album named albumName
import imagePathList into album albumName
end if
end tell
end importToAlbum
I get this error:
2023-11-05 18:15:13 Action 15299945 failed: Execute an AppleScript failed with script error: text-script:147:172: execution error: NSString doesn’t understand the “stringWithString_” message. (-1708)
2023-11-05 18:15:13 Execute an AppleScript failed with script error: text-script:147:172: execution error: NSString doesn’t understand the “stringWithString_” message. (-1708). Macro “Move images and videos to Photos.app” cancelled (while executing Execute AppleScript).
Thanks for the link. I remember chatting about this same issue a while ago, but I can't seem to find the topic or my notes about it...
That wiki article seems a bit confusing, because on the same script it's using (at least that's what I assume) both options:
Getting the variable from KM into AS
Getting the AS variable back into KM
Is it true? At least that's what I got from it, after struggling with it for a few minutes...
I now have this and it doesn't work:
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
set filePath to getvariable "Local__file" instance kmInst
end tell
set filePath to ((current application's NSString's stringWithString:filePath)'s stringByExpandingTildeInPath) as text
set imageList to alias POSIX file filePath
importToAlbum(imageList, "Testing")
on importToAlbum(imagePathList, albumName)
tell application "Photos"
set albumNames to name of albums
if albumName is in albumNames then
import imagePathList into album albumName
else
make new album named albumName
import imagePathList into album albumName
end if
end tell
end importToAlbum
@tiffle
Just a few questions to see if I understand how to use the variables properly:
1 - If I want to use a local variable (Local_music) in the AppleScript that was set before the Execute AppleScript (for example Set New Variable > Execute AppleScript), I would use this:
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
set kmLocalVar1 to getvariable Local_music instance kmInst
end tell
or
2 - If it's the opposite: I created a script with a variable (myMusic) and I want it to be used on a different action (Execute AppleScript > Search and Replace, for example):
set kmInst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
setvariable Local_someVar instance kmInst to myMusic
end tell
Right? So one is from KM to AS, the other one is AS to KM, correct?
Also 2 questions here:
1 - Do I need to quotes around the KM variables? In the examples they have it, so I'm wondering if I need them like "Local_myMusic" or just Local_myMusic?
2 - Is that last line necessary? "log kmLocalVar1"
I want to make sure I understand this so I can add it to my KM notes
Thanks