
A picture above is a snapshot of Apple's Script Editor: at the input text view there is a definition of the handler choose_folders and below it is a "just evaluated" expression. The result is shown below, in the result text view, as a list of three chosen directories. All of this is of course written in Applescript.
This code is a wrapper around the choose folder handler, defined in User Interaction Suite of the Standard Additions OSAX, which enriches standard Applescript with several suites of commonly used handlers.
The formal description of the choose folder is shown below:
choose folder v : Choose a folder on a disk or server
choose folder
[with prompt string] :
the prompt to be displayed in the dialog box
[default location alias] :
the default folder location
[invisibles boolean] :
Show invisible files and folders? (default is false)
[multiple selections allowed boolean] :
Allow multiple items to be selected? (default is false)
[showing package contents boolean] :
Show the contents of packages?
(Packages will be treated as folders.
Default is false.)
→ alias : the chosen folder
The items in square brackets are optional. The one which says [default location alias], means that user can specify a default location, on which a browser of folders will be open. The world alias is a fancy name for existing file or directory, immutable with respect to its repositioning into other location of your directory structure. Once you make an alias and move it somewhere else the Applescript will find it for you anyway. But what is really important here is the format of this alias; it must be something like this: alias "ibook:Users:jans".
But what if you wish to use the standard POSIX directory paths instead? Well you would have to specify it as: alias POSIX file "/Users/jans" - with or without the terminating forward slash. The wrapper handler choose_folders customizes the original handler: it will accept "root folder" in POSIX format, it will allow to select more than one folder, and it will represent the result as a list of chosen folders in POSIX format. Other options are simply ignored.

This window, in turn, is Scheme-OSA window. There is an equivalent definition of the procedure choose-folders, this time of course written in legal Scheme.
Below it is "just evaluated" Scheme expression (choose-folders "/Users/jans"). The result is displayed in output text view as a Scheme list of three directories in POSIX format.
Notice that the reddish-brownish text on the left is almost the exact replica of the handler in top window. Well, not exactly. We need to preserve the formal syntax of Applescript, which does not like if a single command line is broken in half. It also likes each command on a separate line. And it prefers return rather than newline line terminators. Accordingly we assemble all of this from a list of strings, and then combine everything together with a subroutine invocation. At the end of this procedure is a little magic, which will announce it to Scheme-OSA bridge as a callback, rather than normal Scheme procedure.
Conclusions
So this is how we build new Scheme procedures, which are destined to talk to Applescript world.
1. We first read appropriate documentation.
2. We then start experimenting with Applescript using Script Editor, or other such tools, writing some Applescript code.
3. Once we are happy with the result we port such code to Scheme.
Those who know nothing about Applescript do not need to despair. For starters, I am providing a small library of useful procedures from User Interaction Suite of Standard Additions. You can go quite far with just this library.
Secondly, Applescript is not that difficult to learn, so you can start writing your own Scheme wrappers in few days.
No comments:
Post a Comment