[Solved] AppleScript not working

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).

Your first 3 lines only work for global variables so you need to modify it slightly to work with your local variable.

See the KM wiki here https://wiki.keyboardmaestro.com/AppleScript#Local_Instance_Variables for guidance.

1 Like

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

What error are you getting now?

Oh - and please post the macro.

Error:

2023-11-05 19:58:55 Action 15299945 failed: Execute an AppleScript failed with script error: text-script:207:232: execution error: NSString doesn’t understand the “stringWithString_” message. (-1708)
2023-11-05 19:58:55 Execute an AppleScript failed with script error: text-script:207:232: execution error: NSString doesn’t understand the “stringWithString_” message. (-1708). Macro “Move images and videos to Photos.app” cancelled (while executing Execute AppleScript).

Move images and videos to Photos.app.kmmacros (22 KB)

Keyboard Maestro Export

@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

Correct. You need to use the first one - as you already have in the macro image.

Next: you can delete the line starting “set filepath to ((“ since you don’t require tilde expansion.

Next: you need the quotes.

Last: you can delete the log line.

(Apologies for not being more verbose/demonstrative but I’m on my iPad now and I’m minimising the amount if typing…)

1 Like

Thank you for clarifying. I took notes about all of this now.

Deleted and now the macro is working. Great!

Thanks!

No worries. Your tips really helped me understand how this works and the macro is now working. Appreciate it! :raised_hands:

1 Like