Pass Variable to Apple script

Hello all;

So for my first post here I am wondering how to pass a KM variable into an apple script. I am trying to get my camera to pan to my front door for a quick peek then return to its previous position. The script works fine if i use static values, but not dynamic. I have tried
$KMVAR_LastPTZ as well as %LastPTZ%, neither work. The following script at least plays out, but it doesn't return to its last position.

Here's the code.

    tell application "Keyboard Maestro Engine"
	set LastPos to getvariable "LastPTZ"
end tell

tell application "SecuritySpy"
	ptz preset1 camera number 2 speed 100
	delay 10
	ptz LastPos camera number 2 speed 100
end tell

The script you used should work fine:

tell application "Keyboard Maestro Engine"
  set LastPos to getvariable "LastPTZ"
end tell

For details, see Using AppleScript to Get and Set Keyboard Maestro Variables.

Since I don't have the app "SecuritySpy", I can't test or comment on the remainder of your script.
It would be best for you to upload your macro.

Thanks for your reply. I’ve been all over that link today, nothing i tried works - including the code i posted. Very strange.

So, if you want more help, then:

@JMichaelTX

Done!

Hello all;

So for my first post here; (now second as I was asked to share the macro)... I am wondering how to pass a KM variable into an apple script. I am trying to get my camera to pan to my front door for a quick peek then return to its previous position. The script works fine if i use static values, but not dynamic. I have tried
$KMVAR_LastPTZ as well as %LastPTZ% , neither work. The following script at least plays out, but it doesn't return to its last position.

Here's the code.

    tell application "Keyboard Maestro Engine"
	set LastPos to getvariable "LastPTZ"
end tell

tell application "SecuritySpy"
	ptz preset1 camera number 2 speed 100
	delay 10
	ptz LastPos camera number 2 speed 100
end tell

SecuritySpy: PTZ to preset1 then return to last.kmmacros (7.2 KB)

SecuritySpy%3A%20PTZ%20to%20preset1%20then%20return%20to%20last

Well, the first problem I see is that you do NOT define the KM Variable "LastPTZ" anywhere in your KM Macro. So when the AppleScript is run, it will just return the empty string "".

Also, why are you using Shell Scripts with osascript to run AppleScripts?
The best way to run an AppleScript in KM is using the Execute an AppleScript action.

And what is the purpose of this KM Action at the top of your Macro:
image

I don't see any need for it.

How do I define that value? I thought it could be invoked using the getvariable "LastPTZ" as it is stored as a global variable.

Is that not an applescript? I did select "execute applescript".

The purpose of the "Cancel all other macros" is to stop other active macros - like the one that pans my camera back and forth repeatedly.

Where did you set this its value?
Have you confirmed it is properly set using the KM Preferences>Variables panel?

I don't understand what you are referring to.

That is one way to stop your camera pan macro, but it's a bit of overkill. :wink:
If it were me, I would use a global variable, say "Run_Pan", and in the macro that does the panning check that Variable for a value of "YES" in the loop that does the panning.

Then in your "SecuritySpy: PTZ to preset1 then return to last" Macro. set "Run_Pan" to "NO" at the top, and then set to "YES" when you want the panning to continue.

Where did you set this its value?
Have you confirmed it is properly set using the KM Preferences>Variables panel?

Yup, here's a screen grab:

28%20PM

I don't understand what you are referring to.

03%20PM

So how do I fix it? How do I set that value in my script?

If it were me, I would use a global variable, say "Run_Pan", and in the macro that does the panning check that Variable for a value of "YES" in the loop that does the panning.

Then in your "SecuritySpy: PTZ to preset1 then return to last" Macro. set "Run_Pan" to "NO" at the top, and then set to "YES" when you want the panning to continue.

Yeah, it probably is overkill. The fact is I don't use that macro a whole lot anyway, and its fine if some other macros stop it.

See link from above:

Hello again JMichaelTX;

I have been playing with that script to no avail, there is obviously something I am missing. I have tried playing with a few different iterations in the examples in the link and none of them seem to work. How hard is it to get a set KM variable and use it in my apple script?

Can you help further? I'm sorry if the solution is painfully obvious to you, I can't get it to work.

SecuritySpy: PTZ to preset1 then return to last copy.kmmacros (7.7 KB)

OK, I think I finally understand what you are trying to do, and why it is not working.
You original script uses the proper method to get a KM Variable.
The problem is that the SecuritySpy scripting model does NOT accept an AppleScript variable in the ptz command. So, we have to build a dynamic script object, and then run that script.

I believe this is what you need. Please test and let us know how it goes:

AppleScript to Control SecuritySpy Camera


tell application "Keyboard Maestro Engine"
  set cameraPosition to getvariable "LastPTZ"
end tell

if (cameraPosition = "") then set cameraPosition to "preset5"

--- Build Script to Command SecuritySpy Camera ---

set oScript to "
tell application \"SecuritySpy\"
  
  ptz preset1 camera number 2 speed 100
  delay 10
  ptz " & cameraPosition & " camera number 2 speed 100

end tell"

log oScript

--- Execute the Script ---
run script oScript


1 Like

@JMichaelTX:

That works perfectly! Thank you so much!

I tell you , these scripts are strange, I can spend hours trying out one modification after another, asking myself; "Why doesn't this work?!" Then I look at your solution and "Of course!". I thought we could just plus that variable into the script. It turns out we had to build the script afterward. Who knew - well, you did. Thanks!

1 Like