Referencing Files and Folders in AppleScript
- Saving Files On Mac To Folders Delete
- Saving Files On Mac To Folders Mac
- Saving Files On Mac To Folders Free
Need a way to merge folders and treat files in the following manners: (1) If the two same-named files are the same, keep a copy of the file. (2) If the two same-names files are NOT the same, keep both copies. (3) If the file does NOT exist in the destination folder, move the file from source folder to destination folder. Is this possible?
- May 06, 2020 Open Finder and press Shift + Cmd + C to view your Computer. Go into Macintosh HD, then Users. Double-click on the user account you want to transfer a file to, then open their Public folder. Drag-and-drop files or folders into the Drop Box folder to transfer them.
- The path in File Explorer should look like this: Mac. If you're playing Stardew Valley on a Mac, you can find your game save files at the following folder: /.config/StardewValley/Saves If that doesn't mean anything to you, simply follow the below: Press COMMAND + SPACE on your computer (or click the magnifying glass in the top right).
In AppleScript, file and folder paths are typically represented using alias
, file
, and POSIX file
objects.
Note
Additional information about working with file and folder paths in AppleScript can be found in Aliases and Files in AppleScript Language Guide.
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
Listing 15-1AppleScript: Structure of an alias objectalias 'VolumeName:FolderName:SubfolderName:FileName'
Listing 15-2 shows an example of an alias
object that references the Desktop folder.
APPLESCRIPT
Listing 15-2AppleScript: Example of an alias reference to a folderListing 15-3 is an example of an alias
object that references an existing file on the Desktop.
APPLESCRIPT
Listing 15-3AppleScript: Example of an alias reference to a filealias '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
Listing 15-4AppleScript: Creating an alias from a colon-delimited path stringset 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
Listing 15-5AppleScript: Example of a command that accepts an alias parameter and returns an alias resultset 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
Listing 15-6AppleScript: Structure of a file objectfile '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
Listing 15-7AppleScript: Example of a file reference to a filefile '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
Listing 15-8AppleScript: Example of incorrect usage of a file object specifierset 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
Listing 15-9AppleScript: Example of correct usage of a file object specifierset theFile to 'Macintosh HD:Users:yourUserName:Desktop:My File.txt'
read file theFile
Note
A file
object can refer to either a file or a folder, despite the file
specifier prefix.
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
Listing 15-10AppleScript: Structure of a POSIX file objectListing 15-11 is an example of a POSIX file
object that references a file that may or may not exist on the Desktop.
APPLESCRIPT
Listing 15-11AppleScript: Example of a POSIX file reference to a filePOSIX file '/Users/yourUserName/Desktop/My File.txt'
Note
A POSIX file
object can refer to either a file or a folder, despite the POSIX file
specifier prefix.
In a POSIX path, the startup disk's name is omitted and represented by a leading slash. Other disks are referenced in relation to the Volumes
directory of the startup disk, for example: /Volumes/DiskName/FolderName/SubFolderName/FileName
.
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
Listing 15-12AppleScript: Example of a reference to a file in the Finderdocument 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
Listing 15-13AppleScript: Example of a reference to a folder in System Eventsfolder '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
Listing 15-14AppleScript: Coercing a string to an aliasset 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
Listing 15-15AppleScript: Coercing an alias to a stringset 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
Listing 15-16AppleScript: Coercing a POSIX file to an aliasset 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
Listing 15-17AppleScript: Coercing a Finder file reference to an aliastell 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
Listing 15-18AppleScript: Converting an alias to a POSIX path stringset 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
Saving Files On Mac To Folders Delete
The handler in Listing 15-19 converts strings, path
objects, POSIX file
objects, Finder paths, and System Events paths to alias
format.
APPLESCRIPT
Listing 15-19AppleScript: Handler that converts a path to an AppleScript aliason 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
Listing 15-20AppleScript: Calling a handler to convert a path to an AppleScript aliasset 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
Listing 15-21AppleScript: Handler that converts a path to an a stringon 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
Listing 15-22AppleScript: Calling a handler to convert an AppleScript alias to a path stringset 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
Listing 15-23AppleScript: Handler that converts a path to an a POSIX path stringon 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
Listing 15-24AppleScript: Calling a handler to convert an AppleScript alias to a POSIX path stringset 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 AppleScript
In AppleScript, file and folder paths are typically represented using alias
, file
, and POSIX file
objects.
Note
Additional information about working with file and folder paths in AppleScript can be found in Aliases and Files in AppleScript Language Guide.
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
Listing 15-1AppleScript: Structure of an alias objectalias 'VolumeName:FolderName:SubfolderName:FileName'
Listing 15-2 shows an example of an alias
object that references the Desktop folder.
APPLESCRIPT
Listing 15-2AppleScript: Example of an alias reference to a folderListing 15-3 is an example of an alias
object that references an existing file on the Desktop.
APPLESCRIPT
Listing 15-3AppleScript: Example of an alias reference to a filealias '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
Listing 15-4AppleScript: Creating an alias from a colon-delimited path stringset 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
Saving Files On Mac To Folders Mac
Listing 15-5AppleScript: Example of a command that accepts an alias parameter and returns an alias resultset 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.
Will minecraft windows 10 work on mac. APPLESCRIPT Wd elements support mac.
Listing 15-6AppleScript: Structure of a file objectSaving Files On Mac To Folders Free
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
Listing 15-7AppleScript: Example of a file reference to a filefile '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
Listing 15-8AppleScript: Example of incorrect usage of a file object specifierset 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
Listing 15-9AppleScript: Example of correct usage of a file object specifierset theFile to 'Macintosh HD:Users:yourUserName:Desktop:My File.txt'
read file theFile
Note
A file
object can refer to either a file or a folder, despite the file
specifier prefix.
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
Listing 15-10AppleScript: Structure of a POSIX file objectListing 15-11 is an example of a POSIX file
object that references a file that may or may not exist on the Desktop.
APPLESCRIPT
Listing 15-11AppleScript: Example of a POSIX file reference to a filePOSIX file '/Users/yourUserName/Desktop/My File.txt'
Note
A POSIX file
object can refer to either a file or a folder, despite the POSIX file
specifier prefix.
In a POSIX path, the startup disk's name is omitted and represented by a leading slash. Other disks are referenced in relation to the Volumes
directory of the startup disk, for example: /Volumes/DiskName/FolderName/SubFolderName/FileName
.
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
Listing 15-12AppleScript: Example of a reference to a file in the Finderdocument 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
Listing 15-13AppleScript: Example of a reference to a folder in System Eventsfolder '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
Listing 15-14AppleScript: Coercing a string to an aliasset 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
Listing 15-15AppleScript: Coercing an alias to a stringset 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
Listing 15-16AppleScript: Coercing a POSIX file to an aliasset 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
Listing 15-17AppleScript: Coercing a Finder file reference to an aliastell 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
Listing 15-18AppleScript: Converting an alias to a POSIX path stringset 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
Listing 15-19AppleScript: Handler that converts a path to an AppleScript aliason 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
Listing 15-20AppleScript: Calling a handler to convert a path to an AppleScript aliasset 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
Listing 15-21AppleScript: Handler that converts a path to an a stringon 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
Listing 15-22AppleScript: Calling a handler to convert an AppleScript alias to a path stringset 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
Listing 15-23AppleScript: Handler that converts a path to an a POSIX path stringon 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
Listing 15-24AppleScript: Calling a handler to convert an AppleScript alias to a POSIX path stringset thePath to alias 'Macintosh HD:Users:yourUserName:Desktop:My File.txt'
set thePath to convertPathToPOSIXString(thePath)
--> Result: '/Users/yourUserName/Desktop/My File.txt'