Satimage Previous
Scripting gotchas
Home Documentation Smile Scripting gotchas  
Table of contents
To get or not to get Back to top
In most cases the get verb is implicit. For instance the two following lines are equivalent.
set x to bounds of window 1
set x to (get bounds of window 1)
Here x is not set to a reference (but rather to a list of integers), Applescript implicitly evaluates bounds of window 1. But in many circumstances, Applescript will not evaluate the reference as you might expect, and you have to force the evaluation with the get verb.

Rule of the thumb: if you can apply set to an expression, most probably the expression is not a value, it is a reference. Apply get to get its value.
  • References as parameters of commands
    uppercase name of window 1 -- error
    Here uppercase receives a reference, not a string. Like any command in a Scripting Addition, uppercase cannot resolve a reference which refers to an object of Smile. You have to call explicitly the get command in order to force evaluation before the string is sent to uppercase:
    uppercase (get name of window 1)
    Smile will not evaluate nested references as in:
    set properties of window 1 to {name:name of window 2} -- does nothing :-(
    You muse use get:
    set properties of window 1 to {name:(get name of window 2)} -- :-)
  • Awkward references
    set x to item 1 of bounds of window 1 -- error :-(
    This line will fail because the line contains an implicit inconsistency. Indeed, you are passing to Smile the complex reference item 1 of bounds of window 1, while what you really want is having Smile return bounds of window 1, and have AppleScript handle the item 1 part of the line.
    set x to item 1 of (get bounds of window 1) -- :-)
    Sometimes Applescript itself does not accept references.
    repeat with w in (every window)
    end repeat
    -- error

    repeat with w in (get every window)
    end repeat
    -- all right
Name conflicts Back to top
As in any programming language, you must avoid name conflicts. In most languages, the problems arise when linking, not at run time. In AppleScript within Smile, you can augment the context at any moment with a new variable or a new handler. This brings many advantages and also some possible conflicts.

To view the list of the variables currently defined in Smile's context, run in an AppleScript terminal the following line:
name of every variable of context
The first variables in the list are those that Smile has installed when launching, the rest of the list is those variables that you defined since in the AppleScript interpreter.

Similarly:
name of every handler of context
returns a list. In the beginning of the list you can find two handlers with short names: dd and msg. dd is a shortcut for one standard setting of display dialog [...]. msg prints its parameter to the Console. If you define a variable or a handler with those names (particularly msg) Smile will not work properly.
set dd to 1 -- :-(
set msg to "hello world" -- :-(
Inside a handler, dd and msg would only be broken locally. In an AppleScript terminal, they would be broken for the whole Smile until you quit.
Referring to windows by index Back to top
  • When you create successively objects, you expect that they assume indexes with increasing values.

    This is indeed the case. For instance the AppleScript terminal which is created first is (usually) the Console, so the Console is text window 1.
    name of text window 1
      --  "Console"
    You can use negative indexes, to specify reverse counting. For instance, create a new text window, then run the line below in that window.
    text of text window -1
      --  "text of text window -1"
    If a given window is text window 5 and you delete one of the text windows which had been created earlier (text window 1 to 4), your window becomes text window 4.

  • On the other hand it is a common and a convenient practice that window 1 refer to the front window.

    This is indeed the case: window 1 refers to the front window, window 2 refers to the window just behind and so on. To refer to a window using its front to back rank, use the (virtual) class window. For instance you can run the following in a new text window.
    window 1 is text window -1
      --  true
  • The index property of a window refers to the front to back order. Thus, you can bring a window to front, or send it to back, using the index property.
    set index of window 1 to -1
    -- send the front window to back

    set index of window 2 to 1
    -- swap two frontmost windows
    On the other hand, be aware that if a dialog (for example) has a given index, say 5, this does not imply that it is dialog 5. It is window 5. You cannot get the index of a window in the creation order.
Copyright ©2008 Paris, Satimage