Making New Files and Folders

As in Mac OS 9, you can make new files and folders with Mac OS X and AppleScript by using the Finder’s make command. Example 32-3 creates a new folder called “NewFolder” in the Desktop folder of the currently logged in user.

Example 32-3. Making a New Folder in OS X
tell application "Finder"
   make new folder at desktop with properties{name:"NewFolder"}
end tell

You can also create new files like aliases with the new Mac OS X Finder. One Finder quirk that has been corrected in OS X is the necessity to use the syntax make file at... as opposed to make new file at... when code is making a new file. Under OS 9 and its predecessors, you generally have to use the make new... syntax when making everything but file objects. Example 32-4 asks the user for a file reference, using the choose file osax, and then creates an alias to that file with the default name of “[file name] 2.” In other words, if the original file is named “newfile,” then the alias is named by default “newfile 2.”

Example 32-4. Making a New Alias File
tell application "Finder"
   set f to (choose file with prompt "Choose the alias's original file")
   make new alias file to f
end tell