Wiki Update: AppleScript and Scripting Pages

The following Wiki articles have been revised, and are submitted for your review, comment, and/or revision:

2017-01-06 16:57 CT

###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.

Looks good!

Please revivew these scripts carefully to make sure I didn't introduce any errors.

Not errors, but some observations:

Variable quoting

In the examples @ Scripting → GET/SET Using Ver 7.1+ you have used the quoted form for the KM variables:

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.

Variable naming

Also in the snippets @ AppleScript → Get and Set Variables, I find it a bit confusing that every variable has a “kmVar” in its name:

set kmVarValue to getvariable kmVarName
setvariable kmVarName to kmVarValue

To make it clearer which variable belongs to AppleScript and which variable belongs to KM, I would do something like this:

set myASVar to getvariable myKMVar
setvariable myKMVar to myASVar

that is, in conjunction with the aforementioned consistent use of quoted forms:

set myASVar to getvariable "my KM Var"
setvariable "my KM Var" to myASVar

Just my 2¢

@Tom, thanks for the quick and thorough review, and the great suggestions.
I agree with them, and have made the corresponding changes to:

AppleScript [Keyboard Maestro Wiki]

Please let me know if you have any other suggestions.

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.

The same for

setvariable myKMVar to myASVar

should be → setvariable "myKMVar" to myASVar

This concerns the first snippet on “Using AppleScript to Get and Set Keyboard Maestro Variables”. (I have not checked if the error is also present on other articles.)

Sorry for not nagging earlier (:wink:), but I became aware of this only as I pointed another user to the WiKi article.

– Tom

@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.

Thanks for your feedback and suggestions.

You didn’t get what I tried to say. Probably my fault!

I’m speaking of the most common use case: You want to get a KM variable value into an AS variable (or viceversa).

Copy this macro; this is with not quoted KM variables:

___AS variables-not working.kmmacros (1.7 KB)

You will notice that it won’t work. (If it works for for you, please let me know.)

Now copy this macro:

___AS variables-working.kmmacros (1.7 KB)

It will work, i.e it will output the value.

OK, @Tom, I hope you were clear this time, because I have just made my last update on this Wiki article. :smiling_imp:

I suppose it is marginally better. I'm just not sure it was worth the effort since the use of quoted text was already there in the second example.

See AppleScript [Keyboard Maestro Wiki], version 1001. LOL

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:

In the 2nd snippet on “Using AppleScript to Get and Set Keyboard Maestro Variables” you have written:

setvariable "My KM Var Name" to "A new value"

IMHO this should be

setvariable "My KM Var Name" to anyASvariable

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.

Well, fine then. I guess this was the last time I tried to help you with a WiKi article.