Apple Script basics

Discussion in 'General Mac Discussion' started by continuum, Feb 19, 2005.

  1. continuum macrumors 6502

    continuum

    Joined:
    Aug 22, 2004
    Location:
    Beautiful Lake Tahoe
    #1
    Can anyone assist me in finding a list of commands that are used in Apple Script? I want to create some basic stuff but really don't know where to begin. I've looked at the built in dictionary and just want to make some sense of it.
     
  2. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #2
    AppleScript dictionaries can be confusing to people without much programming experience, but I'll attempt to give you an overview on them.

    Each scriptable application has a dictionary, which is split into different suites. Each suite is then split into commands and classes. Commands allow you to do actions, while classes represent objects (such as a document) and have properties that you can get and set.

    Some commands that you can use with almost any application are found under the "Standard Suite" section of a dictionary, and are close, count, delete, duplicate, exists, get, make, move, open, print, quit, save and set. Some application's dictionaries also have a "Text Suite", which is used for manipulating text.

    Beyond that, the dictionaries are application dependent. Often they have a suite containing the application name (eg "iCal suite"), otherwise it can be split into multiple suites (eg Finder, which has "Finder Basics", "Finder items", "Containers and folders" etc). Each of these suites contains commands and classes that are specific to the application, and must be enclosed in a tell block in your script.

    As an example, the following script tells iTunes to play the next track of the current playlist. It uses the "play" and "next track" commands from the iTunes Suite in iTunes' dictionary, which must be enclosed in a tell block.

    Code:
    tell application "iTunes"
    	play (next track)
    end tell
    The dictionary for "Standard Additions" lists the standard commands and classes that can be used in a script without a tell block, including display dialog, beep, choose file etc. This is probably what you're looking for.

    Here's a simple example using display dialog:

    Code:
    display dialog "Hello world"
    If you're unsure as to how to do something specific, or have any other questions, feel free to ask.
     
  3. continuum thread starter macrumors 6502

    continuum

    Joined:
    Aug 22, 2004
    Location:
    Beautiful Lake Tahoe
    #3
    This is good. I like the overview you gave. Thank you! What about syntax? You show "(next track)" in parentheses. When I look in the dictionary it doesn't reveal that parentheses are needed.
     
  4. HexMonkey Administrator

    HexMonkey

    Staff Member

    Joined:
    Feb 5, 2004
    Location:
    New Zealand
    #4
    Actually when I typed in the code I left out the parentheses, and it put them in when I compiled the script. In general, parentheses are just used to show the order in which things are done, much like in maths, so they are often optional.

    The best way to learn syntax is to read and (most importantly) write scripts. There's heaps of resources on the internet, for example this tutorial.

    You can also get examples of some scripting structures from within Script Editor by right clicking/control clicking in a script window and choosing an item from one of the submenus at the bottom of the menu. For example, if you choose "if-then <selection> end" from the "Conditionals" submenu, it will insert a basic if statement into the script.
     
  5. Santaduck macrumors 6502a

    Santaduck

    Joined:
    Oct 21, 2003
    Location:
    Honolulu
    #5
    Also, remember that in addition to using Applescript to control other applications, you can also use Applescript to make a standalone utility, for example one that does calcluations, or calls Unix Shell scripts (in other words does things that might be cumbersome or impossible for Finder to do).

    e.g.:
    set filename to "my test file.txt"
    do shell script "echo blah blah >> " & quoted form of filename
    (look in your userhome for the created file)
    (if you know your unix well or can learn, then this can be pretty powerful).

    Also dialog buttons (up to 3) are easy & nice:
    set answer to button returned of (display dialog "Here are three buttons" buttons {"uno", "dos", "tree"} default button 2)
    display dialog answer

    Other links:

    http://www.macdevcenter.com/pub/ct/47
    http://maccentral.macworld.com/features/applescriptprimer00/
    http://justapplescript.buzzword.com/discuss/msgReader$48
    book review
     
  6. continuum thread starter macrumors 6502

    continuum

    Joined:
    Aug 22, 2004
    Location:
    Beautiful Lake Tahoe
    #6
    Thanks to both of you. This should get me started!
     

Share This Page