Schrödinger's Comments

Yes I agree with you 100%. The trick is getting the AppleScript code to integrate with the KM macro/subroutine. That's what I meant by "an AppleScript solution."

Get and set KM global, local, and instance variables from AppleScript.

set kmInstance to system attribute "KMINSTANCE"

tell application "Keyboard Maestro Engine"
   set asVarName to getvariable "local_copiedText" instance kmInstance
   setvariable "local_KM_VarName" instance kmInstance to dataStr
end tell

What do these AppleScript lines do?
Are they needed/useful in KBM Execute AppleScript actions?

I.e., if a pro like Chris always starts his AppleScript this way, should I?

They're declaring required resources for the script. So:

  • it won't run if the AS version is less than 2.4
  • it needs the "Foundation" framework because it contains ASObjC commands
  • and as soon as you use any use statement, Scripting Additions aren't available by default so you have to declare their use too

There can also be some behind-the-scenes optimisation of how Scripting Additions calls are processed when you explicitly use them like this, so the pros (like Chris!) do recommend it and the pros (like Chris!) do do it.

If (like Chris!) you use Script Debugger you'll find they are included in the "New File" templates.

VGTK! (Very good to know!)

So a good workflow might include something like:

  • as soon as I open a new Execute AppleScript action in KBM, I would…
  • switch over to Script Editor and open it’s New File template,
  • write a first draft of the script in Script Debugger, using its syntax checker etc.,
  • then copy and paste that script into KBM for testing.

Do you know of a way inside of Script Debugger to test the script using KBM variables? Or do I, as I outlined above, have to paste the script into KBM to run it?

When I’m building/testing scripts in ScriptDebugger I just manually set the variables to whatever KM would produce until I have a working script. Then I either replace the manually set variables lines with lines to retrieve KM variables, or typically comment them out for future testing and add the KM variables lines in.


Thanks. I think I’ve done that with Script Editor before, but I don’t yet have any “muscle memory” for working with AppleScript, which is why I asked.

1 Like

Script Debugger -- I know you use Debugger elsewhere in your post, But I'd echo @ccstone in that if you're even semi-interested in AppleScript then Debugger is an excellent choice. The free version is good, but if you value your time even slightly then the debugging feature alone will quickly cover the cost of the full version.

There are ways, but I wouldn't recommend it. I'm a great advocate of Local variables in KM whenever possible, which makes what you want a lot more difficult.

What I do is set the variables in the script while I'm testing in the editor, then replace those set lines with KM Engine calls in the action's script:

set userName to "August"

set theText to "Hello " & userName & "!"
tell application "Keyboard Maestro" to display dialog theText
return "Job done"


set inst to system attribute "KMINSTANCE"
tell application "Keyboard Maestro Engine"
	set userName to getvariable "Local_username" instance inst
end tell

set theText to "Hello " & userName & "!"
tell application "Keyboard Maestro" to display dialog theText
set theResult to "Job done"

tell application "Keyboard Maestro Engine"
	setvariable "Local_jobStatus" instance inst to theResult

(Yes, you can return the result directly to a variable in the KM action, but I wanted to show how you can get them out this way too.)

Giving you:

AS Demo.kmmacros (3.0 KB)



Hey Guys,

Actually I recommend never emplacing use statements in AppleScripts – unless you're writing AppleScriptObjC – or you are employing some of the tricks you can do with them.

This practice lets me know at a glance what kind of script I'm dealing with and lowers the cruft factor.

Vanilla AppleScript has no need for them – AppleScriptObjC requires at least the use foundation statement and sometimes one or more others.

As @Nige_S mentions – once you employ a use statement the system scripting additions are dead unless you deliberately turn them back on.

I have a text-abbreviation in Typinator for this:

use AppleScript version "2.4" --» Yosemite or later
use framework "Foundation"
use scripting additions

The first line of this indicates when the first really useful version of AppleScriptObjC came out.