Referring to items in the Finder

    To control Finder items such as files and folders, you need to specify the items in the script. There are several ways to refer to items. In addition, you can use AppleScript terms to specify groups of files.

Types of references

    There are three ways to refer to a file or folder in a script. Each type of reference indicates the name of the item, the type of item, and its location on your computer.

    Finder references:
    A Finder reference starts with the name of the file or folder, then gives the location of the item as a "chain of folders" that contain the item, ending with the disk. Here's an example:

    file "Art.gif" of folder "Images" of disk "Macintosh HD"

    A Finder reference can include these names of special folders and disks:
    system folder
    fonts folder
    desktop
    extensions folder
    preferences folder
    apple menu items folder
    startup disk

    You don't need to use quotation marks with these shortcuts. Here are two examples:

    file "Art.gif" of folder "Images" of startup disk

    file "My Prefs" of preferences folder

    Path references:
    A path reference starts with the name of the disk, gives the names of the folders, and ends with the file or folder name. Each item in the path is separated by a colon. The path reference for a folder ends with a colon. For example:

    file "Macintosh HD:Images:Art.gif"

    folder "Macintosh HD:Images:"

    Alias references:
    An alias reference is in the same form as a path reference, but is preceded by the word "alias" instead of "file" or "folder":

    alias "Macintosh HD:Images:Art.gif"

    alias "Macintosh HD:Images:"

    Alias references in a script work much the same way that aliases work in the Finder. Finder and path references are absolute, so a script will not work if you move the item they refer to. If you use an alias reference, the alias will contain the file's location, even if it's moved, so the script will always work.

    If you reference items on remote disks:
    AppleScript tries to locate an alias reference on a remote disk when it compiles your script. The remote disk must be available and you must provide the appropriate user name and password to access the disk.

Pasting references

    Rather than typing a reference, you can copy and paste it. To paste a Finder reference to a file or folder into a script:
    1 Select a file or folder in the Finder, then open the Edit menu and choose Copy.
    2 In the Script Editor, place the insertion point in the script where you want the reference to appear.
    3 Open the Edit menu and choose Paste Reference.

Identifying items

    AppleScript includes terms that let you specify groups of items in various ways.

    Every:
    Use Every to refer to all the files or folders in a location. For example:

    set my_list to every file of folder "Process Files"

    Whose and Where Its:
    Use Whose and Where Its to refer to files or folders by a condition. For example:

    set my_list to every file of folder "Process Files" whose¬
    name contains "project"

    set my_list to every file of folder "Process Files" where its¬
    name contains "project"

    These two statements would have the same result.

    This example refers to folders by their label property:

    set my_list to every file of the startup disk whose¬
    label index is not 0

    And and Or:
    Use And and Or to refer to items by several conditions. For example:

    set my_list to every file of folder "Process Files" whose¬
    name contains "project" and label index is not 0

    Entire Contents:
    In the Containers and Folders suite, the Finder defines the Container object, which includes an Entire Contents property. Because folders and disks are containers, you can use this property to refer to all the items in a folder. For example:

    set my_list to every file of the entire contents of folder¬
    "Process Files" of startup disk whose name label index is 0

Related topic

 


Table of contents