![]() |
![]() ![]() |
Allowed coercions
Syntaxset theFile to alias "Macintosh HD:Desktop Folder:newfile.txt" DescriptionAn 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.
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 Figure 3-1. An alias file icon![]() 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. ExamplesAliases 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. *) |
![]() |
![]() ![]() |