Contents
Double Tell etc.
Applescript Links
Lists & Newgroups
English Dialect
Scripting Eudora
Contents
Storing Prefs
Unicode Greek
|

applescript@eremita
Using a Variable to Send Events
Many of the functions that Applescript performs are internal to Applescript, relying on the 'aeut' resource of the current dialect, eg. the English Dialect in the Dialects folder of the Scripting Additions folder".
This contains the Standard Suites of events and objects, such as the Applescript Suite, theStandard Suite, the Text Suite etc. This serves as a translator between the "human language" of Applescript and the inhuman tokens that make up Apple Events, classes, constants etc. Instead of writing something like this, or worse
get Applescript's «property txdl»
--> {""}
get first «class citm» of "AS"
--> "A"
we write
get Applescript's text item delimiters
--> {""}
get first text item of "AS"
--> "A"
But Applescript is extended by osaxes (Open Scripting Architecture eXtensions) on the one hand, and on the other by resources in scriptable applications. If you look at the 'aete' resource of any osax or scriptable application, you will find similar collections to the 'aeut' resource in the Dialect file.
In order to send events, or instructions, to an application, including the Macintosh Finder, the Applescript compiler needs to have access to the application's Apple Event dictionary and this is achieved by using a "tell block" in our script which allows statements within the block access to all the suites peculiar to the application.
tell application "Eudora Pro"
make message at the end of mailbox "Out"
--> message id 1.72768502E+9 of mailbox "Out" of application "Eudora Pro"
get the bounds of the front window
--> {219, 145, 723, 585}
end tell
In this script we have 'window', 'front', 'bounds','end of' which are not peculiar to Eudora but are events objects or properties shared by many applications; on the other hand we have 'message', 'mailbox' which are objects that do not figure in the standard suites and which have a special meaning in Eudora. Outside the tell block these words would have no meaning and the script would not compile. Now application "Eudora Pro" lookes very much like application "Eudora Pro" and you would reasonably expect both lines in the script below to compile and run...
set varEudora to "Eudora Pro"
tell application varEudora to get the bounds of the front window
--> {219, 145, 723, 585}
tell application varEudora to make message at end of mailbox 2
--> ERROR: Expected end of line, etc. but found number.
...but we discover that as soon as we come to the line containing the special Eudora objects, the script does not know what we are talking about and has not got access to Eudora's dictionary. Thus it is pointless substituting a variable for the name string in the tell statement unless only standard events and objects are going to be used.
Somehow we have to get an application "Eudora Pro" which is not simply a concatenation of 'application' and "Eudora Pro". Besides, application files can have different names. Alas, very few authors give the same name to every version and flavor of their program. We want our script to work on different machines with different versions of the same basic program which may be named anything. One thing that all versions will have in common is the four letter creator type code, so what we need to do is identify the application whose creator type is "????", get access to its dictionary and persuade out script to compile. The Finder can quickly locate an application given its creator type. If the application is a running process, then the file of that process will be returned and if not, then the candidate that is physically nearest will be located.
set varCreatorType to "TBB6"
tell application "Finder"
get application file id varCreatorType
--> file "Tex-Edit Plus" of
-- folder "TE+" of folder "Program Files" of
-- disk "D8" of application "Finder"
class of result
--> application file
end tell
However, once the process is running it is not a simple matter to get a reference to it in the form we need. This is complicated by the fact that different versions of Applescript on different Macintosh operating systems will behave differently. OS 7.6 behaves one way, OS 8.0/1 behave a new way, and OS 8.5 is different again. In the script below, we ask for the creator types and we get a list of classes; we ask for the first process and we get a list of 1 item! This is on OS 8.5. OS 7.1/5/6 will give a list of strings and the first of the list will not be a list but a string.
tell app "Finder"
set varProcessCreators to get the creator type of every process
--> {«class HPKb», «class LAND», «class TBB6», etc.}
set varProcessList to ¬
the first application process whose ¬
creator type is "CSOm"
--> {process "Eudora Pro" of application "Finder"}
set varProcess to item 1 of varProcessList
get the class of varProcess
--> application process
process "Eudora Pro" of application "Finder"
set varEudora to varProcess as «class psn »
--> application "Eudora Pro"
end
Finally in the last line we have got the thing we are needing -- application "Eudora Pro". Let us now reduce this process to the bare minimum.
set creaType to "CSOm"
tell app "Finder" to open application file id creaType
tell app "Finder" to set appEudora to ¬
(the first process whose creator type is ¬
(creaType as «class type»)) as «class psn »
tell appEudora to open window "Address Book"
So far so good. Without mentioning any names, we have found the application file, launched the process and got a window to open. But we have not referred in plain English to anything in the Eudora Suite. If we had done so we should have got an error. And here we come to the two real solutions to this problem. It is hard to say which of them is the uglier but they both work.
First the "Double Tell" procedure
set creaType to "CSOm"
tell app "Finder" to the first process whose ¬
creator type is creaType as «class type»
set appEudora to the result as «class psn »
tell app "Eudora Pro" to ¬
tell appEudora to ¬
get the addresses of the first nickname in nickname file 0
--> "MACSCRPT@LISTSERV.DARTMOUTH.EDU"
The obvious objection to this from anyone first seeing it is that it begs the question. The whole point, surely, is that we don't want to use the name of a particular brand of Eudora. The answer to this is that the current process "Eudora Pro" is used only to gain access to the dictionary and to translate 'addresses', 'nickname' and 'nickname file' into useful entities. Once this is done, the script file has all it needs to compile and once it is compiled, the name "Eudora Pro" is irrelevant and unnecessary. If you take this script to a machine that has no Eudora at all and open it in Script Editor, you will be prompted to locate "Eudora Pro". You can choose any application you want, even SimpleText will do, and the script will open and compile.
This brings us to the second method of telling an application by variable, where we dispense with the double-tell and use raw events. Here is the same script with the double-tell removed. Now the script will run and get the results in all circumstances without the risk of a dialog appearing asking for an application. Of course Eudora must be running for it to work at all, but that is easily guaranteed by launching it by creator type in the first place.
set creaType to "CSOm"
tell app "Finder" to the first process whose ¬
creator type is (creaType as «class type»)
set appEudora to the result as «class psn »
tell appEudora to ¬
get «class euNa» of first «class eNck» in «class eNfl» 0
--> "MACSCRPT@LISTSERV.DARTMOUTH.EDU"
|
Composed by
John Delacour
using Frontier 5.0b20
Last updated Sat, 07 Aug, 1999 at 9:08:43 pm
| |
|