DekGenius.com
Team LiB   Previous Section   Next Section
alias

Allowed coercions

list with one item, as in: {alias "Macintosh HD:Desktop Folder:newfile.txt"}
string

Syntax

set theFile to alias "Macintosh HD:Desktop Folder:newfile.txt"

Description

An alias type is a representation of a disk, folder, or volume. An alias is a form of referring to an object such as a file (as in the syntax example), which is very similar to the "alias files" that you can create in the Finder.

An alias file is a Finder object that can be referred to in tell statements that target the Finder. An alias (such as the alias in the syntax example), on the other hand, is a built-in AppleScript class or type.

Nearly everyone who has used a Macintosh is familiar with making alias files (i.e., select the file and type Command-M or choose File Make Alias from the Finder's menu in Mac OS 9). For example, if you have a file called new.txt and you make an alias out of it, then the Finder creates a new file in the same location that looks like Figure 3-1. This file refers to the original file by using a unique identifier. Even if you move the original file around within the volume (which is represented by a disk icon on OS 9's desktop), but not outside of the volume, the alias file will still find it. An alias value type is similar to an alias file. Every time you change and recompile a script that refers to an alias, AppleScript will attempt to find the file or other object that the alias refers to. A lot of commonly used commands take aliases for arguments (such as open for access) or return aliases (e.g., choose file). See Appendix A for more information on these commands.

Figure 3-1. An alias file icon
figs/ascr_0301.gif

One way to create an alias in AppleScript is by preceding a valid file path with the keyword alias:

alias "Macintosh HD:Desktop Folder:newfile.txt"

If the file path, a string, does not point to a valid file, folder, disk, or volume, the script will not compile in Script Editor. For example, if you use the code:

set theFile to alias "Macintosh HD:Desktop Folder:newfile.txt"

and the file newfile.txt does not exist, then Script Editor will not allow a compilation to an applet or compiled script.

Another way to create an alias in AppleScript is to use the keyword as with a string:

set theFile to "Macintosh HD:Desktop Folder:newfile.txt"as alias

The string used with as (e.g., "Macintosh HD:Desktop Folder:newfile.txt"), however, has to be a valid file path or the script will not compile. Other ways to get aliases to files or folders are the choose file, choose folder, and path to scripting additions. See Appendix A for a description of these commands. All three commands return alias types that refer to files, folders, or to special folders such as control panels. The examples elsewhere in this chapter include the use of these scripting additions.

Examples

Aliases are particularly useful in AppleScript for getting the path to files or folders as strings. The first example shows how to use the path to scripting addition to get a string that represents the Desktop Folder ("Macintosh HD:Desktop Folder:"). The path to osax returns an alias type, which is then coerced to a string:

(* this line returns something like "Macintosh HD:Desktop Folder:" *)
set dpath to (path to desktop) as string

(* this returns an alias like alias "Macintosh HD:Desktop 
folder:today:index.html", but only if the "index.html" file exists *)
set theFile to alias (dpath & "today:index.html")
set dt to (path to desktop) as string (* returns (depending on the 
startup disk name) "Macintosh HD:Desktop Folder:" *)
set fileAlias to (choose file with prompt "Choose a file if you please.") 
(* presents a dialog box to the user and returns an alias type that looks 
like alias "myStartupDisk:Web Files:search.html".*)
set folAlias to (choose folder with prompt "Choose a folder to store¬ 
the new file in.") (* this time returns an alias type that points to a 
folder. The return value looks like alias "myStartupDisk:Web Files:". 
Notice that the folder path (i.e., "myStartupDisk:Web Files:") ends with 
a semi-colon (":"), which is the character used to delimit file paths on 
the Macintosh. *)
set theApp to (choose file with prompt "Choose an app to launch") (* You 
can then launch the chosen application with code such as tell application 
(theApp as string) to run. *)
    Team LiB   Previous Section   Next Section