Running an AppleScript saved as an scpt file

I have an AppleScript that creates Calendar events from data in a Numbers document. It works from Script Editor and it works as a Keyboard Maestro macro if I copy the text from the script and paste into the Execute an AppleScript step's "Execute a Text Script" box. But-- if instead of a Text Script I point Keyboard Maestro to the actual script file (saved as .scpt), nothing happens when I run the Keyboard Maestro macro. I am interested in having Keyboard Maestro executing the script file as I may want to change it down the road, and if I do I don't want to have to go to Keyboard Maestro and change it there too. I just can't get it to work when I tell Keyboard Maestro to execute the script file. I must be missing something!

This this page may have what you're looking for:
https://wiki.keyboardmaestro.com/action/Execute_an_AppleScript

Thanks for that. That page is where I started. Unfortunately it does not have an answer to my question.

Have you had a look at the Engine.log?

Or tried setting the action to output errors to display in window?

Display Errors

I did get this error from Keyboard Maestro:

/usr/bin/osascript: couldn't save changes to script

error -1763

So now I have to figure that out.

c

Have you got a property declaration in your script? That would cause KM to try (and fail) to save changes.

1 Like

I didn't know that! :open_mouth:

I get the error all the time but it doesn't cause my scripts to abort, so I 've come to ignore it.

For sanity's sake, I sometimes start by putting this at the top of a problem script:

return "So far so good." 

Save the script.

Make sure the Execute an AppleScript is set to output to "display in window".

Run the macro.

If the message shows up in "display in window" I know, at least, that everything runs up to that point.

May be worth saving it as an .applescript (text) file,
rather than a .scpt (compiled) file.

Compiled .scpt files are trickier, and don't always entirely survive (without recompilation) the transition between different versions of macOS, for example.

1 Like

So that I don't have to keep switching from my script editor to the KM Editor to run the macro and get the output, I sometimes place this block at the top of my script.

As long as it is there, the script editor will run everything up to the block and then trigger the macro and exit. The macro will run anything in the script that is not in the block.

I then comment the block out when I'm ready to run my full script in the script debug environment.

If the "display alert" succeeds, I keep moving the display alert down my script.

Save the script and run again.

use scripting additions
--So that you don't have to keep switching to KM Editor to run the macro, you can also place this block at the top of your script.
--Comment it out when you are ready to run your full script in your script editor environment.
--If the display alert succeds, keep moving the display alert down your script.
--Save the script and run.

if not ("KMINSTANCE" is in (system attribute)) then
	set myMacroID to "copy and paste the id or unique name of your macro here"
	tell application id "com.stairways.keyboardmaestro.engine"
		do script myMacroID -- with parameter 
	end tell
	return "finished running the macro"
end if

return display alert "so far so good" & linefeed & (POSIX path of ((path to me as text)))

Go to the KM Editor and copy your macro's uid to the clipboard.

Screenshot

Paste it as the value of myMacroID.

Save and run.

And I shouldn't have said that...

If it's a text script the property won't be saved -- or, rather, it will be reset every execution because the script is recompiled.

If it is a scpt file it will be saved, which you can prove for yourself by saving

property theNum : 0

set theNum to theNum + 1
display dialog theNum

...as a script file then repeatedly executing it in a macro -- the number in the dialog will increment.

So it isn't generally true -- but it does seem to be true in OP's case since error -1763 is "Unable to store memory pointers in a saved script" (see Error Numbers and Error Messages).

If OP is trying to have a property carry over between executions it'll need to be a scpt file. If they don't then they could either change whatever statement is changing the property's value, or do away with the property altogether and use a variable.

1 Like

I don't have a property declaration in my script. It is saved as a "scpt" and oddly it seems to work via Keyboard Maestro every so often. When it works, it's great. When it doesn't work, nothing happens at all-- no error message, no nothing.

Unlikely to be helpful, but I've never had the issue you describe running AppleScript files from KM. This one is typical:

That’s very interesting. I am using the “Execute script file” like so:

PastedGraphic-1.png

But you’re using “Execute text script.”

Doing the “Execute script file” seems like the more desirable move for me, since I am saving the script as .scpt and I will occasionally want to open that script, edit it, and run it. Or just run it from Script Debugger. When I save, it’s saving as scpt. I guess I could be careful about it and save as text but it would be more convenient to just save normally (as scpt).

I'm just running a scpt file too, in my case usually with a parameter. I'm far from a KM expert, but did some testing to run a simple (no parameters) script file that I normally trigger from a different application (Butler). Surprisingly, it failed to run properly from KM using both my method and your method. However, after adding a delay in the script to allow extra time for a window to open, it worked reliably using both methods. My conclusion is that KM runs the scpt file faster than Butler did, so adding the delay was needed. I suggest you try adding delay(s) in your script to allow more time for actions that take time, such as a window opening or closing.

Intermittent success sounds more like a context issue -- the wrong thing selected, the wrong window frontmost, or similar -- or a resource availability problem. Whether you'd get an error message very much depends on the script and any error handling within it.

It might be a more general KM/osascript problem in your setup, but you could test that with the simple "increment a number" script above.

You should also try with your script file on your system drive rather than in Dropbox, just in case...

And don't forget to try @ComplexPoint's earlier suggestion -- the action will take a little longer to execute since the script will need to be compiled every time, but you say you aren't using propertys so lack of persistence shouldn't be a problem.

I've decided to just put my AppleScripts into the FastScripts menu and run them from there. The scripts work every time from there (and from Script Editor, and from Script Debugger). I liked the idea of triggering an AppleScript from one of Keyboard Maestro's floating palettes but I just can't get it to work. Thanks to you and to everyone who tried to make this work for me.