Simple Finder Script To Add Suffix To A Filename

Hi Guys and Girls

I am pulling my hair out here with something so simple and I thought I would tap into this great resource.

The aim of this script is to enable a user to drag and drop a selection of files onto the script, saved as an App, and for the script to add @TMF to the end of thhe filename of each file.

I would really appreciate any help.

Here it is

on open theList
	tell application "Finder"
		set theList to selection
		set thecount to the count of items in theList
		repeat with i from 1 to thecount
			set thename to (item i of theList) as string
			set name of item thename to thename & "@TMF"
		end repeat
	end tell
end open

Many Thanks

Ignore me I was having one of those friday idiot moments, for those interested and those who were about to ridicule me here is how the script should have read

on open theList
	tell application "Finder"
		set theList to selection
		set thecount to the count of items in theList
		repeat with i from 1 to thecount
			set thename to (name of item i of theList) as string
			set name of item thename to thename & "@TMF"
		end repeat
	end tell
end open

Something like this works if you drop files on the droplet (I didn’t worry about folders)

on open someFiles
	tell application "Finder"
		set theList to someFiles
		repeat with aFile in someFiles
			set fName to (name of (info for aFile)) & "@TMF"
			set name of aFile to fName
		end repeat
	end tell
end open

[I see you fixed yours while I was writing mine; not sure why you want the adder to follow the extension of the file]

Hi Adam

Your script was really helpful I am now trying to move the renamed files into another location and if a duplicate is found then have a dialog box ask the user to replace the item.

Here it is once again any help would be fantastic, cheers

Something like this works if you drop files on the droplet (I didn’t worry about folders)

on open theList
	tell application "Finder"
		set theList to selection
		if not (exists disk "Location 1") then
			display dialog "You need to mount Location 1"
			return
		end if
		if not (exists disk "Location2") then
			display dialog "You need to mount the Location 2"
			return
		end if
		set dailyLegal to folder "Legal IN" of folder "LEGAL" of disk "Location 1"
		set thecount to the count of items in theList
		repeat with i from 1 to thecount
			set thename to (name of item i of theList) as string
			set name of item thename to thename & "@TMF"
			try
				duplicate item to dailyLegal
			on error
				display dialog "An item with the same name already exists" buttons {"Cancel", "Overwrite"} default button 1
				set buttonResult to button returned of result
				if buttonResult is "Overwrite" then
					duplicate item to dailyLegal with replacing
				end if
			end try
		end repeat
		delete theList
	end tell
end open

For openers, have you just tried to mount the servers instead of quitting? See the Scripting Additions “mount volume”. Something like this might work:

mount volume "afp://ProBono/CurrentCases" as user name "Sherlock" with password "Watson"
-- OR
mount volume "afp://"Sherlock":"Watson"@ProBono/CurrentCases"

Next, a more efficient way to refer to a particular folder on a mounted disk is:

-- Instead of this:
set dailyLegal to folder "Legal IN" of folder "LEGAL" of disk "Location 1"
-- Write
set dailyLegal to "Location 1:Legal:Legal IN:" as alias 

Don’t set theList to selection. Note how I did it in the script above. “on open theList” is a list of references to the files that were dropped. Just set them to your variable or use “theList”.

Something like this structure:

on open someFiles
	tell application "Finder"
		-- mount your servers here using real names, etc.
		-- set dailyLegal-1 to the target folder on one server
		-- set dailyLegal-2 to the target folder on the other.
		set theList to someFiles
		repeat with aFile in theList
			set fName to (name of (info for aFile)) & "@TMF"
			set name of aFile to fName -- at this point they are renamed where they were when they were dropped. aFile is still the path to a particular file just dropped.
			try
				duplicate aFile to dailyLegal-1
			on error theMsg -- which will say the file already exists
				set doThis to button returned of (display dialog theMsg buttons {"Cancel", "Overwrite"})
				if doThis is "Overwrite" then duplicate aFile to dailyLegal-1 with replacing
			end try
		end repeat
	end tell
end open

Hi Adam

I really appreciate this help I have re-written the script and it looks like this but I receive the following error

Can’t make <> “Legal IN” of <> “LEGAL” of <> “DM News & Feats Desks” of application “Finder” into type number.

when I run this

on open someFiles
	tell application "Finder"
		set theList to someFiles
		if not (exists disk "DM News & Feats Pages/Pix") then
			display dialog "You need to mount the DM News & Feats Pages/Pix Server"
			return
		end if
		if not (exists disk "DM News & Feats Desks") then
			display dialog "You need to mount the DM News & Feats Desks Server"
			return
		end if
		set dailyLegal to folder "Legal IN" of folder "LEGAL" of disk "DM News & Feats Desks"
		set thecount to the count of items in theList
		repeat with aFile in someFiles
			set thename to (name of (info for aFile)) & "@TMF"
			set name of aFile to thename
			try
				duplicate aFile to dailyLegal
			on error theMsg -- which will say the file already exists
				set doThis to button returned of (display dialog theMsg buttons {"Cancel", "Overwrite"})
				if doThis is "Overwrite" then duplicate aFile to dailyLegal - 1 with replacing
			end try
		end repeat
	end tell
end open
delete theList

You don’t say, but I assume your problem is with this line:

set dailyLegal to folder "Legal IN" of folder "LEGAL" of disk "DM News & Feats Desks"

.

That’s a Finder file specification. The same sort of thing you get with this:

tell application "Finder" to set f to selection -- and you have selected a file
--> document file "yourFile" of folder "itsFolder" of folder "ContainerOfThat" of folder "UserName" of folder "Users" of startup disk

If you add “as alias” to your set dailyLegal string, it will look like this:

alias "yourHD:Users:yourUname:containerOfThat:itsFolder:yourFile"

That’s what appleScript wants to see to set a folder to a variable. It’s the form I used in my example above.

Hi

I have changed the location to an alias however when I run the script I now receive the following error

Can’t make alias “DM News & Feats Desks:LEGAL:Legal IN:” into type number.

Any ideas?

on open someFiles
	tell application "Finder"
		set theList to someFiles
		if not (exists disk "DM News & Feats Desks") then
			display dialog "You need to mount the DM News & Feats Desks Server"
			return
		end if
		set dailyLegal to "DM News & Feats Desks:LEGAL:Legal IN:" as alias
		set thecount to the count of items in theList
		repeat with aFile in someFiles
			set thename to (name of (info for aFile)) & "@TMF"
			set name of aFile to thename
			try
				duplicate aFile to dailyLegal
			on error theMsg -- which will say the file already exists
				set doThis to button returned of (display dialog theMsg buttons {"Cancel", "Overwrite"})
				if doThis is "Overwrite" then duplicate aFile to dailyLegal - 1 with replacing
			end try
		end repeat
		delete theList
	end tell
end open

You’re trying to subtract one from an alias. I think you could just get rid of the " - 1" part.

P.S.: Please put your AppleScript code inside applescript tags (use the button on the “Post a reply” page, or just type them yourself).

Hi there

I’m afraid removing the “-1” only brings up the following error. The operation could not be completed because some items had to be skipped. “This is a test please ignore@TMF”

Any ideas?

Works fine for me.