Renaming files in the Finder, help please...

Hi there,

I know this is pretty easy but I’m making a bit of hash of it, one of those days I think.
I’m trying to rename a file named like this Fred Bloggs - CS to this XYZ-Bloggs_Fred-CS using the name that is already there.

This is the code so far:-

tell application "Finder"
	set x to get selection as text
end tell
set AppleScript's text item delimiters to ":"
set fname to x as text
set yy to (text items 1 thru -2) of fname
--display dialog yy as text
set AppleScript's text item delimiters to ":"
set filename to last text item of x
set AppleScript's text item delimiters to " "
set newFilename to "XYZ-" & (text item 2 of filename) & "_" & (text item 1 of filename) & "-" & (text item 4 of filename) as text
display dialog x as text
display dialog newFilename

set name of x to newFilename

When I run this all works fine apart from the last line - the renaming. I get an error 'Can’t set name of “this file” to “XYZ-Bloggs_Fred-CS” '.

Please can someone point me in the right direction.

Thanks in advance,

Nick

Hi Nick,

you forgot first to tell the Finder to do the job and second to coerce your path to an alias or file reference

tell application "Finder"
	set x to get selection as text
end tell
set filename to name of (info for alias x) -- easier way to get the filename
set AppleScript's text item delimiters to " "
set newFilename to "XYZ-" & (text item 2 of filename) & "_" & (text item 1 of filename) & "-" & (text item 4 of filename) as text
set AppleScript's text item delimiters to {""}
display dialog x as text
display dialog newFilename
tell application "Finder" to set name of alias x to newFilename

Hi Stefan,

Thanks for helping me out with this one, it’s spot on. :slight_smile:

Should have seen the Finder one, d’oh!

Regards,

Nick