Dumbest question ever: How do you rename a file?

OK, this has got to be my like 100th script and I am so frustrated I can’t get this to work I’m about to go crazy:

Running 10.3.9:
I have a file named “blah” on my Desktop. If I run this script:

tell application "Finder"
	choose file
	set myFile to result
	display dialog myFile as string

end tell

and select this file, as expected I get the result: “Polka:Users:pmac05:Desktop:blah”
With Polka being my startup disk, and pmac05 my username. Makes perfect sense, right?

Now, I want to rename the file! Not too complicated, right? So I add the line:

move myFile to "blah2"

And I get the insane error message: Finder got an error: Can’t get alias “Polka:Users:pmac05:Desktop:blah”

Now, correct me if I’m wrong, but if Applescript made the alias variable itself, and I didn’t move the file in between these 3 lines, how can it suddenly not find it? I ask this because I need to be able to rename a file that Photoshop created because Photoshop refuses to save it with the correct name (it’s Photoshop 7, with just enough applescript support to not work a little)

What’s really getting me about this is I’ve seen code exactly like this all over the web in other people’s scripts that supposedly works, but on my machine for some reason it just won’t!

All I want to be able to do is rename a file from applescript. If someone could just tell me a way to rename a file without resorting to a shell script or a POSIX path, I will love you forever. I can’t use a shell script because most of these files are on the network, and for some reason when applescript creates a posix path it neglects to account for the network volume’s place in the actual filesystem (instead of /Volumes/Server, it just goes to /Server, which is just flat out wrong).

How can a language billed as being so easy to use fail at such a simple request? Someone please tell me there’s a viable way…


The problem is not in the reference returned by the ‘choose file’ command. It’s in where you want to move it to. You used a string when it should be a reference. Here’s the syntax from the Finder dictionary:

move: Move object(s) to a new location
move reference – the object(s) to move
to location reference – the new location for the object(s)
[replacing boolean] – Specifies whether or not to replace items in the destination that have the same name as items being moved
[positioned at list] – Gives a list (in local window coordinates) of positions for the destination items
[routing suppressed boolean] – Specifies whether or not to autoroute items (default is false). Only applies when moving to the system folder.
Result: reference – to the object(s) after they have been moved

You see the ‘to’ parameter. It says to location reference. Yuo used a string “blah2” which is not a reference like the one returned from the ‘choose file’ command. Try this:

choose file
set myFile to result
set move_location to (choose folder)
tell application “Finder”
move myFile to move_location
end tell

remember, a string is not a reference. For more info on AppleScript references, read AppleScirptLanguageGuide.pdf.

Edited: to get volume in there just concatenate it to the posix path:

set v to (choose folder)
set path_with_volume to “/Volume” & (POSIX path of v)


See, the problem is, I can’t use a reference, I need to use a string. It’s hard to believe that apple didn’t think people would want to refer to files by their filename rather than by some reference that can only be obtained through choose file or choose folder… the whole point of scripting is to eliminate the user input!

It appears my problem was that instead of saying “my:path:to:the:file” as alias, I should have been saying alias “my:path:to:the:file” and just set the name of the file, instead of using the move command. I was thinking in shell script mode (where there is no rename command, and files are referred to by filename) but using applescript - generally a bad combination. I’m still lost though as to the semantic difference between ‘“this thing” as alias’ and ‘alias “this thing”’ - I mean, shouldn’t they mean the same thing? Perhaps it’s not to be understood…

And by the way, I saw at least 4 separate scripts on the web, all of which claimed to be functional, which all did the move command exactly as I described it. Perhaps all those people were just horribly misguided, but, you’d think the error message would say something about a bad reference to the destination, and not a bad reference to the source. I mean, telling me there’s a problem with the source file when there’s really a problem with the destination would be rather… counterintuitive, wouldn’t it?

Hi Jove,

That is one of the funny things about AppleScript where it says you have errored here while the problem is somewhere else.

There is no difference between (alias “pathtosomeitem”) and (“pathtosomeitem” as alias) although the later form is the proper form. Note that with the ‘as alias’ form, I usually place it in parenthesis or set a variable to it:

set v to “pathtosomeitem” as alias

This last statement will error if to item does not exist. This can be used as a feature.

BTW, I have a really hard time with unix. :smiley: