Finder Commands
The following commands can be used by enclosing them in a
tell
statement that targets the Finder, as in:
tell app "Finder" to sleep
reference
The Apple Menu in the upper left corner of the Mac OS 9 screen has a Favorites menu item that includes folders and programs that are displayed or executed if you select them. You can use this command to add to the Favorites list:
tell application "Finder" to add to favorites (folder "today" of desktop)
This adds a folder called today on the desktop to the Favorites menu.
reference
This command neatly arranges buttons or icons in an open window or on the desktop:
tell application "Finder" to clean up window "HFSA2gig"
(See the Finder’s View menu, which determines how
Finder items like folders are aligned on the desktop.) If you use
clean up all
, this command has the same effect as
clean up desktop by name.
reference
Use the close command followed by a reference to one or more windows. An example is:
tell application "Finder" to close window "HFSA2gig"
You can also close multiple objects:
tell app "Finder" to close every window
This command closes every Finder window on the desktop, such as folder or disk windows.
constant
or string
The computer command provides information about the machine running the script. The following example displays how much memory is available in megabytes, including virtual memory. This command is the AppleScript equivalent of the Gestalt function that is part of the Macintosh Application Programming Interface (API). You can find out more about this function at http://developer.apple.com/techpubs/mac/OSUtilities/OSUtilities-11.html.
You can use the following constants with the computer command:
CPU
, FPU
,
hardware
, memory available
,
memory installed
, MMU
,
operating
system
, and
sound system
. There are also numerous other
selectors that you can use instead of these constants, as long as you
know the four-character string and what its return value means (an
integer
). For example, the command
computer "scr#"
tells you how many active
scripting systems the computer has, and computer "sysa"
indicates whether the computer is a PowerPC (result
value of 2 means yes).
tell application "Finder" set mem to (computer memory available) display dialog (mem / 1024 / 1024) end tell
has integer
The computer command returns a
boolean
value if you use this labeled parameter:
tell app "Finder" to computer "sysa" has 2
This code phrase returns true
if the computer is a
PowerPC.
In Mac OS X, the
computer command has been removed from the
Finder dictionary and placed in the Standard Additions osax as the
command system attribute. You do not have to
enclose system attribute in a Finder
tell
block (as you have to with
computer), because system attribute
is not a Finder command.
You can count the number of objects within another object, such as
count files of folder MyFolder. The command
returns the number of counted objects as an
integer
. You can also use the form:
tell folder "MyFolder" to count files
or:
count each file of folder "MyFolder", or count every file of folder¬ "MyFolder"
data size returns the size in bytes of the object reference that would be returned from a get command. In other words, if you used the phrase:
data size of (file "Boston" of desktop)
the return value would not be the size of the file on disk; it would be the byte size of the actual reference:
file "Boston" of application "Finder"
Yes, I agree, it is difficult to find a purpose for this command. Except that you can get the byte size of an icon family with code such as:
data size of icon of (file "Boston" of desktop)
You can delete more than one object with this command:
delete every item of folder "actions"
This code deletes all folders and files in the “actions” folder on the desktop. Or, you can use syntax such as:
delete {file "test", folder "saved template"} of folder "actions"
This is a handy method when the items that will be trashed are
dynamically assembled in a list
variable:
delete deleteList of folder "actions"
Duplicate an object like a file or folder with code such as:
tell application "Finder" to duplicate folder "today" to folder¬ "actions" of folder "desktop" with replacing
This code will take the “today” folder on the desktop and duplicate it (reproduce it and its contents) to a desktop folder called “actions.” This code will also replace any “today” folders that are contained by the “actions” folder. This is a good command to use when you are backing up files from one volume or disk to another.
to
location reference You can copy or duplicate the objects to another location on your machine by using this labeled parameter. You have to specify a valid location such as:
duplicate folder "today" to folder "2000archive" of disk "BackUp"
The location reference would be to folder "2000archive" of disk "BackUp".
If you do not use this
to labeled parameter, the objects are duplicated
in the same container as the original and given the original name
with “copy” appended to it.
replacing
boolean
If you use replacing true
then any objects with
the same name located in the same container where you are copying an
object are replaced by the new object.
routing suppressed
boolean
This command only applies to objects that are being duplicated to the
System Folder. The Finder automatically routes certain objects that
are dropped on the System Folder like the Calculator accessory (it is
routed to the Apple Menu Items folder). If you set this labeled
parameter to false
then a file or folder that is
duplicated to the System Folder is not automatically routed to a
certain location.
reference
If you just use eject alone, then every ejectable disk is ejected. For example, the following code causes the computer to eject a zip disk from a disk drive and a floppy disk at the same time:
tell app "Finder" to eject
You can specify the disk to eject, as in eject disk "backupZip"
. Using eject with a
non-ejectable disk such as an internal or external hard disk raises a
script error.
This command erases a disk and thereby wipes it clean of all of its data; it is the equivalent of using the Finder’s Special → Erase Disk... menu command. You cannot erase a disk that has File Sharing turned on for it (which means it is being shared over a network). You should use this command with care (in other words, back up any disk data that you want to preserve).
You can find out whether a file or folder exists with code such as:
set itExists to (exists folder "today")
If the “today” folder does not
exist on the desktop then the itExists
variable
will be false
. You have to provide the
exists command with a complete object reference
or the Finder will not be able to verify the
object’s existence. Another example is:
exists (file "Web Sharing Extension" of Extensions Folder)
The parentheses are optional but make the code easier to understand.
You can make a new element of the Finder (like a folder or text or image file) with this powerful command. This is a useful command for such tasks as creating a log file and a folder to contain that log. This example creates a new BBEdit text file called “theLog” on the desktop:
tell app "Finder" to make file at desktop with properties {name:¬ "theLog", creator type: "R*ch", file type: "TEXT"}
If you leave out the at
location part when making
a new file or folder, then the Finder will by default make the new
file or folder on the desktop. The return value of the
make command is the object that you created. The
“Finder” code sample beneath the
"with properties record
" section stores the new folder in a
variable and then makes a new file in that folder in the next line,
using the folder variable as the new file location.
The
Finder has a quirk that requires you to not use the
new
keyword when making a new file, as in
make file...
instead of make
new file...
. You have to use the
new
keyword in most other circumstances when
making a new object. You can, however, use the syntax "make new..."
with the Finder and AppleScript 1.6 in Mac OS 9.1
and Mac OS X.
at
location reference In most cases you have to specify where you are making the new object
(except for the Finder’s default behavior to make
new files or folders on the desktop if you leave the
at
labeled parameter out of your
make statement). Be sure to specify a complete
location reference as in the example under
"with properties record
.”
to
reference
If you are making an alias
file type, refer to the
alias file’s original or parent file with the
to
labeled parameter. This code phrase tells the
Finder to make a new alias file to the Word application on the
desktop:
tell app "Finder" to make alias file to application file¬ ((name of startup disk) & ":Microsoft Office 98:Microsoft Word")
with properties record
You use this labeled parameter to give the new file or folder its
properties. The properties are specified as one or more name/value
pairs enclosed by curly braces (for example, a
record
data type). You can find out the properties
that you can provide values for by examining the object in the
Finder’s dictionary. For example, before I created
the following example, I found out that the Finder’s
folder
object inherits some properties from the
container
object, including the icon size
property. So I included icon size
in the with properties
record
,
with a value of large
(a
constant
). A lot of property values, like the
names of files and folders, are strings. with properties
is not a required parameter when making files or
folders. If you do not use it, then the folder is given the name
“untitled folder” and the file is
named “untitled”:
tell application "Finder" activate set lfol to (make new folder with properties¬ {name:"LogFolder", icon size:large}) make file at lfol with properties¬ {name:"Log file", creator type:"ttxt", file type:"TEXT"} open lfol end tell
You can move files and folders around to new locations using this command. Unlike duplicate, this command does not create a copy of the object and leave one copy in the original place; it moves it to the new location. This script moves a folder from the desktop to inside another desktop folder called “actions.” The script also positions the folder to a spot 10 pixels from the left edge of the parent folder and 10 pixels down from the “actions” folder’s top border. The exception is moving files or folders from one disk or volume to another; this copies the original items to the new locations and leaves the originals intact:
tell application "Finder" activate move folder "LogFolder" to folder "actions" positioned at {10, 10} end tell
to
location reference This is a required parameter specifying where you want to move the
object. Unless the location is on the desktop, you have to make a
complete location reference, in the form of folder
"HFSA2gig:1wordfiles"
or
(folder
"1wordfiles"
of
disk
"HFSA2gig")
.
replacing
boolean
If the replacing parameter is true
then any items
with the same name as the items you moved are replaced in the new
location. In other words, if the
“actions” folder already has a
LogFolder folder, then the folder you moved replaces it if
replacing
is true
.
replacing
is false
by default.
positioned at
list
You can position the item you moved in the new location by passing
the move command a point
object. This is a list
of coordinates specifying
the upper left corner of the item’s icon.
routing suppressed
boolean
This command only applies to objects that are being moved to the
System Folder. The Finder automatically routes certain objects that
are dropped on the System Folder icon, such as the Calculator
accessory (it is routed to the Apple Menu Items folder). If you set
this labeled parameter to false
then a file or
folder that is moved to the System Folder is not automatically routed
to a certain location.
You can open one or more files or folders using the
Finder’s open command. You can
also have the Finder create an object and then instruct another
application (the object’s or file’s
creator) to open it. Open several objects at once by passing the
Finder open command a list
:
open {folder "today",folder "actions"}
This command opens two desktop folders, since the Finder assumes that incomplete folder references are on the desktop. If you refer to files or folders without complete file or folder paths (unless they are located on the desktop) then your code will raise an error. Since the following are hypothetically complete path references, the Finder will open each of the folders without an error:
open {folder "Macintosh HD:Logs:JuneLogs",folder "Macintosh¬ HD:Logs:JulyLogs"}
using
reference to application file
You can specify the application to open the file with this labeled parameter:
open file "Bfile" using¬ application file "Macintosh hd:BBEdit 5.0:BBEdit 5.0:" &¬ "BBEdit 5.1"
This code uses the BBEdit 5.0 text editor to open the file. With most
files, the open command issued from the Finder
results in the file displayed by the proper software program. In
other words, if you use the code open
file "bigpic.gif"
and this is a Photoshop file,
then the file will most likely open up in Photoshop. In this case,
the Finder’s open command is
the equivalent of double-clicking the file.
with properties record
This command is designed to pass some object properties along to the application when you are specifying another program to open the file or folder:
using application file "MyProgram" with properties¬ {name:"Newfile"}
But I assume that few programs support this syntax with the Finder’s open command, since I have had difficulty finding any:
tell application "Finder" activate set bpath to "macintosh hd:BBEdit 5.0:BBEdit 5.0:BBEdit 5.1" set tf to (make file at desktop with properties¬ {name:"sfile2", creator type:"R*ch", file type:"TEXT"}) open tf using application file bpath end tell
This does what you would expect it to do—prints a file with
code such as print file "myfile"
. Selecting a
printer in the Chooser before using this command helps avoid errors
with it.
with properties
record
This command is designed to pass some object properties along to the application that will print the file, but the program must support this extension to the print command (which makes it is difficult to find an effective use for this parameter).
put away serves two main purposes: to eject
disks and to return files and folders from the desktop to the disk or
folder where they came from. The example below puts two files that
were placed on the desktop back into the folders where they were
saved. As you can see, you can pass a list
as a
parameter to the put away command to put away
multiple objects. Or you can just use a single object like a disk as
the parameter. If disk “flop” was a
floppy disk, then using the code
put away disk "Flop"
would eject the disk. The code is the equivalent of selecting the disk icon and typing Command-Y:
tell application "Finder" activate put away {file "finderclasslist", file "findercomlist"} end tell
When used with a running program, reveal makes that program the frontmost one in the Finder. You would normally use the activate command to initiate this behavior. You can also use reveal to open a folder in a disk:
reveal folder "Macintosh HD:MyFolder"
This code opens the “MyFolder” folder and makes it the frontmost item on the desktop. Using reveal with other desktop items like document files selects those items but does not open them into a window (use open to do that).
You can select one or more objects on the desktop with this command. For instance, if you record a Finder operation in which you click on and open various objects, the recorded script usually contains a lot of select commands. Once you have selected the objects, you can use the term “open selection,” as shown in the following example:
tell application "Finder" activate select {disk "HFSB2gig", disk "HFSA2gig"} open selection (* opens two Finder windows showing the contents of each disk *) end tell
list
of references
You
can sort files or folders by various properties such as creation
date, modification date, name, or size. The by
part of the command is required.
sort’s return value is the
sorted list
of objects. If you sort files by date,
the sort will be in the order of newest files first; if you sort by
name, then the sort will be in alphabetical order. The following
example sorts the files in a certain folder by their creation date:
tell application "Finder" activate sort files of folder "HFSA2gig:1wordfiles:fol1" by creation date end tell
by property
This required labeled parameter specifies how to sort the objects. You can use all kinds of properties with the sort by command; it depends which object(s) you are sorting. For example, you can use the following properties, among others, to sort files: creation date, creator type, file type, kind, modification date, name, and size.
This command updates the display of objects like windows and disks to match their representation on disk. This task is usually done automatically by the operating system. The effect of this command is to force the update of, for instance, a disk on which a script has made a lot of changes.