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
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
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:
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.
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:
I don't understand what you are referring to.
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.
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.
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
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!