Referencing Files and Folders
Referencing Files and Folders in AppleScript
In AppleScript, file and folder paths are typically represented using alias
, file
, and POSIX file
objects.
Alias Objects
An alias
object dynamically points to an existing item in the file system. Since an alias is dynamic, it continues pointing to the item even if the item is renamed or moved, the same way an alias file works when you manually create one in the Finder. With an AppleScript alias, the original item must exist at run time or an error will occur.
An alias
object is displayed as a colon-delimited path preceded by an alias
specifier, in the format shown in Listing 15-1.
APPLESCRIPT
alias "VolumeName:FolderName:SubfolderName:FileName"
Listing 15-2 shows an example of an alias
object that references the Desktop folder.
APPLESCRIPT
alias "Macintosh HD:Users:yourUserName:Desktop:"
Listing 15-3 is an example of an alias
object that references an existing file on the Desktop.
APPLESCRIPT
alias "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
To create an alias, add the alias specifier prefix to a colon-delimited path string, as shown in Listing 15-4.
APPLESCRIPT
set thePath to alias "Macintosh HD:Users:yourUserName:Desktop:"
Many commands accept an alias as a parameter and/or return an alias as a result. In Listing 15-5, the choose file
command accepts a folder alias
object in its default location
parameter. The command then returns an alias
object that points to the chosen file.
APPLESCRIPT
set theDefaultFolder to alias "Macintosh HD:Users:yourUserName:Desktop:"
choose file default location theDefaultFolder
--> Result: alias "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
File Objects
A file
object is a static reference to an item at a specific location in the file system. It’s not dynamic, and can even refer to an item that doesn’t exist yet. For example, a save
command may accept a file reference when saving to a new file.
A file
object is displayed as a colon-delimited path preceded by a file
specifier, in the format shown in Listing 15-6.
APPLESCRIPT
file "VolumeName:FolderName:SubfolderName:FileName"
Listing 15-7 shows an example of a file
object that references a file that may or may not exist on the Desktop.
APPLESCRIPT
file "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
Unlike the way an alias
object works, you can’t create a file
object simply by prefixing a path string with the file
specifier. For example, Listing 15-7 errors when run within a script.
APPLESCRIPT
set theFile to file "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
Instead, you must prefix the path with the file
specifier at the time the file is targeted by a command, as shown in Listing 15-8.
APPLESCRIPT
set theFile to "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
read file theFile
POSIX File Objects
Some scriptable apps are designed to work with POSIX-style paths, rather than AppleScript alias
and file
objects. Like a file
object, a POSIX file
object is not dynamic and can also refer to an item that doesn’t exist yet.
A POSIX file
object is displayed as a slash-delimited path preceded by a POSIX file
specifier, in the format shown in Listing 15-10.
APPLESCRIPT
POSIX file "/FolderName/SubfolderName/FileName"
Listing 15-11 is an example of a POSIX file
object that references a file that may or may not exist on the Desktop.
APPLESCRIPT
POSIX file "/Users/yourUserName/Desktop/My File.txt"
App-Specific References to Files and Folders
Some apps, such as the Finder and System Events, have their own syntax for referring to files and folders. Listing 15-12 shows how a Finder file reference appears.
APPLESCRIPT
document file "My File.txt" of folder "Desktop" of folder "yourUserName" of folder "Users" of startup disk of application "Finder"
Listing 15-13 shows how a System Events folder reference appears.
APPLESCRIPT
folder "Macintosh HD:Users:yourUserName:Desktop:" of application "System Events"
Since this terminology is app-specific, it doesn’t work in other apps. For example, you can’t write a script that tries to import a Finder reference to an audio file into iTunes because iTunes doesn’t understand Finder file references. In this case, you must coerce the Finder file reference to something iTunes can understand, like an alias. See Converting Between Path Formats below. In most cases, apps with their own path syntax also support standard AppleScript path types.
Converting Between Path Formats
Since different situations may result in paths appearing in different formats, you may need to regularly convert one path format to another. Sometimes, this can be done by using the as
coercion operator, as shown in Listing 15-14, Listing 15-15, Listing 15-16, and Listing 15-17.
APPLESCRIPT
set theFilePath to "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
set theFilePath to theFilePath as alias
--> Result: alias "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
APPLESCRIPT
set theFilePath to choose file
--> Result: alias "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
set theFilePath to theFilePath as string
--> Result: "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
APPLESCRIPT
set theFilePath to POSIX file "/Users/yourUserName/Desktop/My File.txt"
set theFilePath to theFilePath as alias
--> Result: alias "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
APPLESCRIPT
tell application "Finder"
set theFilePath to file "My File.txt" of desktop
end tell
--> Result: document file "My File.txt" of folder "Desktop" of folder "yourUserName" of folder "Users" of startup disk of application "Finder"
set theFilePath to theFilePath as alias
--> Result: alias "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
Converting from a string or alias to a POSIX path can’t be done through coercion. Instead, you must access the POSIX path
property of the path to convert, as shown in Listing 15-18.
APPLESCRIPT
set theFilePath to choose file
--> Result: alias "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
set theFilePath to POSIX path of theFilePath
--> Result: "/Users/yourUserName/Desktop/My File.txt"
Using Conversion Handlers
Running paths through a conversion handler is a good way to ensure the format you expect.
Converting a Path to an Aliases
The handler in Listing 15-19 converts strings, path
objects, POSIX file
objects, Finder paths, and System Events paths to alias
format.
APPLESCRIPT
on convertPathToAlias(thePath)
tell application "System Events"
try
return (path of disk item (thePath as string)) as alias
on error
return (path of disk item (path of thePath) as string) as alias
end try
end tell
end convertPathToAlias
Listing 15-19 shows how to call the handler in Listing 15-19 to convert a POSIX-style path string to an alias.
APPLESCRIPT
set thePath to "/Users/yourUserName/Desktop/My File.txt"
set thePath to convertPathToAlias(thePath)
--> Result: alias "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
Converting a Path to a String
The handler in Listing 15-21 converts a path to string format.
APPLESCRIPT
on convertPathToString(thePath)
tell application "System Events"
try
return path of disk item (thePath as string)
on error
return path of thePath
end try
end tell
end convertPathToString
Listing 15-22 shows how to call the handler in Listing 15-21 to convert an alias to a path string.
APPLESCRIPT
set thePath to alias "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
set thePath to convertPathToString(thePath)
--> Result: "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
Converting a Path to a POSIX Path String
The handler in Listing 15-23 converts a path to POSIX path string format.
APPLESCRIPT
on convertPathToPOSIXString(thePath)
tell application "System Events"
try
set thePath to path of disk item (thePath as string)
on error
set thePath to path of thePath
end try
end tell
return POSIX path of thePath
end convertPathToPOSIXString
Listing 15-24 shows how to call the handler in Listing 15-23 to convert an alias to a path string.
APPLESCRIPT
set thePath to alias "Macintosh HD:Users:yourUserName:Desktop:My File.txt"
set thePath to convertPathToPOSIXString(thePath)
--> Result: "/Users/yourUserName/Desktop/My File.txt"
Referencing Files and Folders in JavaScript
In JavaScript, file and folder paths are represented using Path
objects.
To create a path, pass a POSIX-style string to the Path
object, as shown in Listing 15-25.
JAVASCRIPT
Path("/FolderName/SubfolderName/FileName")
To convert a path to a string, call the toString()
method on the path, as shown in Listing 15-26.
JAVASCRIPT
var path = Path("/Users/yourUserName/Desktop/My File.txt")
var string = path.toString()
string
// Result: "/Users/yourUserName/Desktop/My File.txt"
Copyright © 2018 Apple Inc. All rights reserved. Terms of Use | Privacy Policy | Updated: 2016-06-13