The “key” keyword? (A curiosity)

AppleScript Editor colours the word “key” purple (like "name’, “parent” or “AppleScript”). It is not an AppleScript keyword, however, and it is not defined in standard additions. Apparently, I can use it as a variable name. Does anyone know why AS Editor treats it specially?

I don’t know if it’s always been like that, but it’s not related to ASE – the same thing happens if you compile elsewhere. Which means it is a keyword. I’m not sure how you can use it as a variable name.

It’s defined in both the Satimage and XMLLib OSAXen. Do you have either of those installed?

I can’t use it as a variable name.

Ah, I have Satimage installed. But if include a use statement like this:

use application "Finder"
key
host

The lack of a “use scripting additions” line means host compiles as a variable, but key still does not, so it must be coming from somewhere else.

Also, the styling for key is that of property, not an addition-related style.

Same here.

Ok, I was wrong: using “key” as a variable is not possible (in fact, in my code I was using it as a handler’s parameter). I do not have XMLLib or Satimage installed and, still, “key” is coloured as a property. I’d like to know where it comes from.

Isn’t it the first word of “key code” ?

key code‚v : cause the target process to behave as if key codes were entered
key code integer or list of integer : The key code(s) to be sent. May be a list.
[using command down/Œcontrol down/Œoption down/Œshift down or list of command down/Œcontrol down/Œoption down/Œshift down] : modifiers with which the key codes are to be entered

If you really want to use it as a variable you may use this official scheme :

set |key| to 5
|key|

As, some years ago I got sad behavior with the “official” syntax ” I don’t know why but the vertical bars disappeared ” now I use this kind of syntax:

set key_ to 6
key_

or, to have varnames undertantable by French users as wel as English ones

set clef_key to 6
clef_key

Yvan KOENIG (VALLAURIS, France) mercredi 29 janvier 2014 09:32:51

It is, but that’s not a problem in itself. For example,

set display to 1

It doesn’t matter that display dialog is a term.

The AppleScript Suite of AppleScript.rsrc defines a keystroke class, with properties key, key kind and modifiers.

It’s legacy Mac OS GUI Scripting property code. You still can use the legacy class linked list as well, but later created commands can’t work with it, but it’s in there so AppleScripts created on a Mac OS 7 machine should be run properly on a Mavericks machine.

Edit: You can use key as properties and keys in records though

set x to {key:"hello", value:"Hello World!"}

if key of x = "hello" then
	--do nothing
end if

script dummy
	property key : "object"
	property value : "I'm an object"
end script

return dummy's key

However, Apple is clear about this is in AppleScript Language Guide; don’t do it.

Wow, there are interesting things in that file. Beside keystroke and linked list, I see commands such as negate, start log, and stop log; classes such as C string, encoded string, event, international text, Pascal string, picture, preposition, reference form, scrap styles, styled (Unicode) text, type class, vector, writing code info; and an anything type. (Well, many other things.)

AS Editor colors all of them. Most of them do not seem functional. I guess that they are all deprecated since they are not documented in the AppleScript Language Guide. It’s good to know they exist, however, when stumbling upon unexpected syntax coloring.

Hello.

Syntax coloring gives a good clue for whether the (variable) name means something or not for AppleScript.

I prefer to use an underscore in front of such a name, because I like that better than typing the “fences” (|). Today I was thinking through this, and I found that using camelCasing could improve the readability even more, at the expense of somewhat longer variable names.

No – start log and stop log certainly aren’t. In some cases terms were reserved but never implemented. (AS doesn’t implement start log and stop log, but it reserves them so that script editors can implement them.) The term anything is still used all the time, to denote what a command returns.

It seems that AppleScript editor still works with start log and stop log. After reading the old AppleScript documentation it says:

Because log level is already set to 1 when we start a script we only can turn it off and back on again. So when we start a script with “start log” it means that the log level is set to 2 so we need “stop log” command twice to set it back to 0.

Some example:


stop log --turn logging of, it's on by default
display dialog 1
log 1
start log --turn logging back on
log 2

before running make sure you have event logging turned on

The first version of System 7 was written in pascal and halfway during development (when the operating system was re-named to Mac OS n the system was written in C. This means that some strings comes from C and other could come from pascal written application. Therefore there is still the old pascal string in AppleScript, It was more or less a class for writing scripting additions. I still have some original AppleScript books from Apple (the time before PDF) who are from 1993 - 1996.

Another funny thing is that in those books you had two different things. AppleScript, which are events written in C or Pascal, and you have “AppleScript English” which was the AppleScript code we know as today you’ll write in an AppleScript editor. They named it back then because AppleScript’s syntax is something that you can change and they probably expected in the beginning of AppleScript that the future would bring many different syntaxes for AppleScript. I think Javascript was the only “serious” attempt to be an alternative syntax of the AppleScript English syntax.

I guess that AppleScript English was one of the dialects available.
I’m sufficiently old to remember the AppleScript’s French dialect which was an horror.
For a French user, coding with the English dialect was easier than coding with the French one.

Yvan KOENIG (VALLAURIS, France) jeudi 30 janvier 2014 14:57:26

Yes, it mostly has – even ASObjC Explorer does these days :wink:

But although the commands are defined in AppleScript itself, the implementation is left to the editor.

Similarly, the Database Suite and Mail Suites just defined terms and expected behavior. A bit like Objective-C protocols.

As far as I can tell, that’s not actually part of the specification, but rather how AppleScript Editor has chosen to implement it. I’m not sure that it’s particularly sensible to implement a command whose name implies binary operation like that (or what advantage it offers, really).

French and Japanese versions were released. A “programmer” syntax was half-developed, then abandoned. It was all well before javascript.

Yes, but the weird thing is that even on latenightsw they say that start log and stop log system is broken in ScriptEditor as many more websites do.

Having a Japanese or French syntax will only narrow your public while the attempt to use an syntax who has globally millions of coders addresses a much larger public than AppleScript itself. I can make a Dutch dialect which no flaws, but my public would be small and therefore not be taken seriously. While Japanese and French narrows the public, Javascript could extend the AppleScript community. Also the encoding issues were solved pretty soon in AppleScript (I think it was in System 7) and the Japanese syntax became not a required dialect anymore.

But I think you have to put it in the context of the time, and the original aim. AppleScript was not designed for coders, but for “the rest of us”, and one of the ambitions was to make code read like natural language. With that aim, localized syntax made sense, at least theoretically – if you treat scripting as an alternative interface, the idea of localizing it is not so far fetched. (Well, in those days things like consistency of user interface still had plenty of detractors.)

The resulting dialects were by all accounts dreadful, and the plans for AppleScript were reined in sharply very early on anyway. Several former members of the AS team have expressed regret that the “programmer” dialect was never completed. We will never know what might have been.

The French dialect was such a mess than it was common practice to have scripts written and compiled with the English dialog unable to run in a French system.

Sometimes I read comments about the fact that AppleScript doesn’t match exactly English syntax but with the French dialect we were forced to think to the English syntax and try to translate it ” something like Google translator ” and cross our fingers in hope that the translation was the one used by the developers.

Sometimes, localized applications remind me this old time.

The last example is the set of applications which I named iPlay '13
In its Shortcuts listing the shortcut supposed to bring the cursor at the very beginning of a document is named : « Domicile ».
Knowing the short knowledge of Frenchies about English, I’m not sure that many of them will understand that they are supposed to trigger the « Home » key.

Even Spielberg’s E.T. knew that « Home » is meaning « Maison ».

This push me to ask a question.
Am’ I tired or is the Home character really missing in the Unicode Set available in OS X10.9.1 ?

I found ↗︎ ↙︎ ↸ ↖︎ ↘︎. The third one would be OK without the horizontal segment. The fourth one would be OK if its top left was a V, not a black filled triangle.

Yvan KOENIG (VALLAURIS, France) vendredi 31 janvier 2014 14:43:00