Scripting Basics for file manipulation via the Finder in OSX

I am sharing these basic AppleScripts for the Finder under OSX to hopefully save others some of the difficulties and frustrations I experienced in trying to locate them.

Yes, I checked this site and many, many others; read in detail all the Apple materials (the Help files, Finder’s dictionary, etc.) I could find; and finally consulted the recently-published books (by authors such as Kochan; Neuburg; and Rosenthal). I was astonished how sparsely the basics were covered. Of all the sources, I found Rosenthal’s book to be the most helpful, but even it was missing several basics.

In the spirit of exposing the man behind the red curtain, I share the following:

  1. To duplicate* a file:
    tell application “Finder” to duplicate file “Your Disk:Users:Roxanne:Desktop:test.pdf”

*Note that you can not make a copy of a file using a “Copy” command using the Finder under OSX. Instead you must “Duplicate” the file.

  1. To rename a file:
    tell application “Finder” to set name of file “Your Disk:Users:Roxanne:Desktop:foo.pdf” to “test.pdf”

  2. To move a file:
    tell application “Finder” to move file “Your Disk:Users:Roxanne:Desktop:test.pdf” to “BigServer:Clients:test_folder:”

  3. To delete a file:
    tell application “Finder” to delete file “Your Disk:Users:Roxanne:Desktop:test.pdf”

  4. To empty the Trash:
    tell application “Finder” to empty trash

  5. To determine the path to the Desktop:
    path to desktop as string

  6. To determine the path to the Users:Shared folder:
    path to “sdat” as string

Please feel free to offer additions, improvements, suggestions, etc. I just hope the next newby has an easier time of learning how to script some basic file manipulations in the Finder for OSX.

Hi,

There’s a lot more basic stuff and to correct #3:

tell application “Finder” to move file “Your Disk:Users:Roxanne:Desktop:test.pdf” to “BigServer:Clients:test_folder:”

move item_reference to location_reference

The location_reference here is a string and you should use a reference like:

alias “BigServer:Clients:test_folder:”

gl,

Hopefully someone can help me with this…

tell application “Finder” to set name of file “Your Disk:Users:Roxanne:Desktop:foo.pdf” to “test.pdf”

This, and everything like it, never works for me. No matter what I try to do to a file in the Finder, I get an error such as:
“Finder got an error: Can’t get name of document file “test.txt” of folder “Desktop” of item “tonks”.”

I can’t get the name, I can’t set the name, I can 't seem to do anything. I’ve tried this on two computers, both running 10.3.7

What am I missing? I’ve even copied and pasted code from the example scripts, with the same result.

forget file, use alias:

tell application "Finder"
	set name of alias "Macintosh HD:Users:vml:Desktop:before.rtf" to "after.rtf"
end tell

Just tested it and it works.

thanks for the reply.

that worked, but i can’t get any further. (i’m having endless trouble finding up-to-date documentation on the web.)

what i’m trying to do is to get a reference to a file i’ve selected in the Finder so that I can manipulate and then pass that reference to another app.

when i “get selection” it’s fine, but then I can’t get the name, alias, or anything else from the selection. I’ve tried several versions of the code below, with and without the “first item” line, etc… all to no avail…


tell application "Finder"
	set s to selection
	set i to first item of s
	get alias of i
end tell

The selection is a list of file paths, not aliases…

tell application "Finder"
	set fileList to selection
	repeat with theFile in fileList
		set filealias to theFile as alias
		--do stuff with each filealias like renaming deleting labeling etc.
	end repeat
end tell

So I tried this at work and it worked, I was excited. Now I’m home and it doesn’t work.

The code:


tell application "Finder"
	set fileList to selection
	repeat with theFile in fileList
		set filealias to theFile as alias
	end repeat
end tell

The event log:


tell application "Finder"
	get selection
		{document file "test.txt" of folder "Desktop" of item "tonks"}
	get document file "test.txt" of folder "Desktop" of item "tonks"
		"Can't make «class docf» "test.txt" of «class cfol» "Desktop" of item "tonks" of application "Finder" into a alias."

I realize this is very basic stuff, but I can’t seem to get started. I can’t imagine why it would work on my office machine and not here. Same code, same OS, etc.

Thanks again for your help.

Hi,

I can’t remember how a pre OSX reference looked, but I think it looked like this. Is the name of your startup disk “tonks”?

gl,

Hmm, since it worked at work I can’t think of anything except a different version of Applescript or OSX.

Maybe a real guru can tell you what’s going on…

That ‘item “tonks”’ shouldn’t be there. If you’ve selected a single file called “test.txt” on your desktop, the Finder should return the selection as:

{document file "Test.txt" of folder "Desktop" of folder "<yourUserName>" of folder "Users" of startup disk of application "Finder"}

The item in the list is a Finder reference. You can use it in the Finder to rename the file, move it, or do whatever the Finder can do with it. There’s no need to coerce it to alias for that.

tell application "Finder"
  set fileList to selection
  set name of item 1 of fileList to "New name.txt"
end tell

Assuming you’ve accurately reported your results, the problems you’ve been having may be with the hard disk of your home machine. It could be worth rebooting and/or checking the disk with a disk repair utility.

interesting… there seems to be some debate between this ‘alias’ and ‘file’ thing.

i should have explained that “tonks” is my username.

the version of OSX is the same in both places, how do I determine the version of AppleScript?

display dialog version of AppleScript

:slight_smile:

The version of AppleScript is unlikely to affect the behaviour of the Finder itself, but I can’t say straight off if it has any influence during the coercion of Finder references to alias.

Greetings,

I’m working on renaming a file as well and I’ve tried the script posted above:

tell application “Finder”
set name of alias “Macintosh HD:Users:vml:Desktop:before.rtf” to “after.rtf”
end tell

However, I, too, continually receive this error:

File Macintosh HD:Users:vml:Desktop:before.rtf wasn’t found

The file, however, is clearly on my desktop. What gives? (I’ve just cut and pasted the above script although my username and information differs, the concept remains the same).

Thanks.