Referring to files in Finder

How can the properties of a file be accessed and changed? Specifically, I want to change the names of a set of files based on fields in an image database. Here is what I have tried in my handler, which I can’t make work:

tell application “Finder”
set theFile to a reference to “Macintosh HD/Users/me/Images/ART/Monika/PSD/CRW_6975.psd”

set theItem to properties of theFile
    set theFname to name of theItem
    set name of the theItem to newName
length of theItem

end tell

I have tried colons in the path and instead of “a reference to” before I have appended an “as reference” at the end. None of the lines above work. I thought scripting Finder items and files involved changing fields in their property records but I can’t seem to access them. Thanks for any insights.

Hi. Try this:

tell application "Finder"
	set myFile to file "Mac OS X:Users:John:Desktop:testFile.pdf"
	set myFile's name to "newName"
--or you can use
--  set name of myFile to "newName"
end tell

or

set myFile to alias "Mac OS X:Users:John:Desktop:testFile.pdf"
tell application "Finder" to set myFile's name to "newName.pdf"

or

set myFile to POSIX file "/Users/John/Desktop/testFile.pdf"
tell application "Finder" to set name of file myFile to "newName.pdf"

Your script reads all properties of theFile into a variable theItem. theItem is not theFile, it is a record of theFile’s properties. Look in the Editor’s events pane after running the script, and you should see this returned as an answer to that specific line (edited for privacy):

{class:document file, name:"Test.rtf", index:3, displayed name:"Test", name extension:"rtf", extension hidden:true, container:...., disk:startup disk of application "Finder", position:{-1, -1}, desktop position:{1598, 566}, bounds:{-33, -33, 31, 31}, kind:"RTF-tekst", label index:0, locked:false, description:missing value, comment:"", size:442, physical size:4096, creation date:date "zaterdag 29 september 2012 13:19:23", modification date:date "zaterdag 29 september 2012 13:19:23", icon:missing value, URL:..., owner:"me", group:"staff", owner privileges:read write, group privileges:read only, everyones privileges:read only, file type:missing value, creator type:missing value, stationery:false, product version:"", version:""}

Briefly, theFile is an object (“owned” by Finder). You can change (some of) its properties by telling it to …, or by telling Finder to do this-or-that to one of its objects. That’s what adayzdone’s examples demonstrate.

Thanks for reply. This clears things up. All of your examples are working with the file directly. I thought that it was necessary to access the file’s property record in order to change a file property. Also, I see I was using some incorrect reference forms like trying to use “/” in a file path without the “POSIX file” prefix.

Thanks. I thought that one needed to change a Finder object’s property (when changeable) by changing the value of the property in the property record itself. Didn’t realize the object itself knew it had a given property with a definite value. In general, can any changeable property of a Finder object be changed by referring to the object itself? Would changing the r-w properties of an object in any other program necessarily be done via direct reference to the object, rather than its property record, as well?

Basically yes. But…

tell application "Finder"
	selection --> {document file "test.rtf" of folder "Desktop" of folder "thijs" of folder "Users" of startup disk of application "Finder"}
end tell

tell application "Finder"
	tell document file "test.rtf" of folder "Desktop" of folder "thijs" of folder "Users" of startup disk
		name
	end tell
end tell

I made Finder give me a reference to an item on the desktop.
Then I used the reference to ask Finder to return the file’s name.
Point is, you cannot use the reference as-is:

tell document file "test.rtf" of folder "Desktop" of folder "thijs" of folder "Users" of startup disk of application "Finder"
		name
	end tell

That won’t even compile… The script has to address the Finder first, so the rest of the message can be sent somewhere where it will be understood.

It should, but some scriptable apps have a limited understanding of AppleScript.
You can change the properties record, but that has no effect. To talk to the properties record you have to put it into a variable - and by that very action the script no longer talks to “properties of the file”:

tell application "Finder"
	set myFile to item 1 of (get selection)
	set itsProps to properties of myFile
	set name of itsProps to "test2.rtf" --> does nothing!
	-- or:
	tell itsProps
		set its name to "test2.rtf" --> nothing again
	end tell
	itsProps-- it changed, but not the actual filename!
	set name of myFile to "test2.rtf" --> yes, that works
end tell

Thanks. Makes sense now.

Hello.

It may be faster to script System Events than Finder when it comes to files, as long as you don’t obtain file referencec through Finder, since it is an back ground application, and doesn’t have to update windows and such. :slight_smile:

Thanks for reply. This opens a new can of worms for me but I’m intrigued. Never even thought of System Events as an application, much less considered scripting it. It’s probably a powerful approach but I’m completely unfamiliar with it. Is it recordable? That might be a way to start. Looking at the dictionary and object model in my demo copy of Script Debugger now and the terms are not familiar. (What actually is the function of System Events?)

Something like this (?):

tell application “System Events”

set myHome to folder “Me” of folder “Users” of folder “Macintosh HD” (as reference?)
set theFile to file “test.txt” of folder “Desktop” of (folder myHome|? myHome) (as reference?)
move file theFile to folder “Documents” of folder myHome

end tell

Hi,

System Events is a faceless scriptable application.
It contains a file management suite (similar to the Finder) as well as a lot of other suites to control Security, Login items, Folder Actions, Appearance, several Preferences, Processes, Audio-, QuickTime-, Movie-files, Network- and Powermanagement and some more.

System Events is not recordable (as far as I know).
For your example it has a few predefined folders to avoid absolute paths


tell application "System Events"
	set myHome to home folder -- myHome is an object reference
	set theFile to file "test.txt" of desktop folder -- theFile and desktop folder are object references
	move theFile to folder "Documents" of myHome
end tell

Thanks for the help. Will need to practice a bit…