###Primary Changes
* The primary change to both articles is to provide the more compact methods of using AppleScript as of KM 7.1, and to put these methods at the top of the list of script examples.
* Added a real-world example AppleScript
* Added Section Headers to the Scripting article to provide a better TOC.
* Changed all of the scripts on the AppleScript article to use more clear and consistent syntax
* Please revivew these scripts carefully to make sure I didn't introduce any errors.
set calcResult to getvariable "Calculation Result"
…
setvariable "Calculation Result" to (calcResult + 10)
I think this is a good idea since KM variables can include Spaces.
‌
For more consistency I would do the same in the snippets @ AppleScript → Get and Set Variables, where currently you are using the unquoted forms.
‌
Sorry for nagging, but – as mentioned in my post above – I think you really should quote the KM variables in the snippets:
set myASVar to getvariable myKMVar
gives an error:
: execution error: The variable myKMVar is not defined. (-2753)
→ set myASVar to getvariable "myKMVar"is fine.
This is because unquoted strings (other than keywords) are seen as variables by AppleScript. And, at that stage, the KM variable is not yet in the AppleScript space.
@Tom, of course that gives an error, but you are taking one line out of context of the complete script shown on the wiki, and thus not accounting for the prior line:
set myKMVar to "My KM Var Name"
###The Complete Script on the Wiki
### Requires Keyboard Maestro 7.1+ ###
--- Set AppleScript Variables to KM Variable Name and Value ---
set myKMVar to "My KM Var Name"
set myASVar to "TBD" -- default value of KM variable, will be updated
tell application "Keyboard Maestro Engine"
### GET ###
-- IF KM Variable does NOT exist, the AS Variable will be set to empty string --
set myASVar to getvariable myKMVar
### SET ###
-- IF KM Variable does NOT exist, it will be created --
setvariable myKMVar to myASVar
end tell
Also, right below that, I show an example of using the KM Variable name explicitly:
tell application "Keyboard Maestro Engine"
set mainStr to getvariable "SCPT__MainString"
set subStr to getvariable "SCPT__StringToFind"
end tell
Perhaps this is a matter of style and/or preference, but I think the Wiki shows both methods, and the user can choose which he/she prefers.
Finally, any user who has previously used AppleScript to a reasonable degree will understand the concept of using either a variable or quoted text when an AppleScript command requires text data.
Let's see if we get comments from any other KM Forum users about this. I certainly want the Wiki to be clear and useful.
Michael, the only thing I wanted to do is helping you getting rid of inaccurate information (=bugs) in the wiki. I think it is obvious that this was a bug, no? That’s it.
I visited the Wiki and, sorry to say it, you have introduced a new not-so-correct thing. I’m feeling very bad telling you this, sincerely, because I know you love the Wiki and it’s your baby (kinda). But I’m a friend of the Wiki, too, and that’s why I’m telling you it:
What you have written is not strictly false. Of course you can set a KM variable to any string. But I think that’s not the most common use-case. The more common use-case (IMHO) is to transfer the value of a AppleScript variable to a KM variable. I hope you know what I mean. If not, please let me know.
No, There was no bug.
Did you read my response in my prior post?
I just re-ran the exact script from above, and NO errors.
Sorry, @Tom, but you are totally wrong here.
This statement is 100% valid AppleScript and Keyboard Maestro.
setvariable "My KM Var Name" to "A new value"
It may not be your style or preference or use case, but it works, and serves the purpose of showing that the parameters to the KME commands are text.
Tom, you need to keep this in mind: There is only one purpose to this wiki article, and that is to clearly show users how to get/set KM variables in AppleScript. Period. It does that.
This is NOT about optimizing code, or picking use cases. The examples shown are clear for all use cases. The more experienced AppleScript users will undoubtedly code things in the way they prefer.
Tom, I'm done with this. Keep posting if you want, but I'm not spending any more time on this Wiki article.