file renaming script... here I go-

Hi everyone,

I was hoping to get a few suggestions as to how to approach a script I need to set-up. I’m a novice applescripter, so this likely will be much easier for someone with more experience than myself.

The scenario:
Files (.eps) get dropped into a folder on a server. In order for these files to be used during production, they can’t be more than say, 17 characters (not including the file extension)
-Each file begins with a number (2-digit)… these need to remain
-Some files end with a specific naming convention (it is always the same- “BobPS” for example)…this needs to remain

The script has to be able to truncate a file’s name to within 17 characters, and keep the first 2 characters, and possibly the last 5 characters where applicable.

Does anyone have any ideas how to start? (I’ll try to post anything I come up with shortly)

Thanks everyone

See Nigel Garvey’s solution in this thread for starters. It’s a gem.

thanks for the link Adam. I have begun, and here it what I’ve got so far. This is not complete, as it is not working. When I run the initial script you linked (Nigel’s) it errors too. Here is my script:

on adding folder items to this_folder after receiving these_items
	checkNames(these_items)
end adding folder items to

on checkNames(these_items)
	set maxLen to 17
	tell application "Finder"
		set fileNames to name of every file of these_items
		-- Rename any files with more than 17 characters in their names.
		repeat with thisName in fileNames
			if (thisName's length > maxLen) then
				set name of file thisName of these_items to my truncateName(thisName, maxLen)
			end if
		end repeat
		-- Recurse through the subfolders, renaming files if necessary.
		set subfolders to this_folder's folders
		repeat with thisSubfolder in subfolders
			my checkNames(thisSubfolder)
		end repeat
	end tell
end checkNames

on truncateName(thisName, maxLen)
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	if ((count thisName's text item 1) = (count thisName)) then
		set newName to text 1 thru maxLen of thisName
	else
		set nameExtn to "." & thisName's text item -1
		set newName to text 1 thru (maxLen - (count nameExtn)) of thisName & nameExtn
	end if
	set AppleScript's text item delimiters to ASTID
	return newName
end truncateName

The original script errors at the line “then
set name of file thisName of theFolder to my truncateName(thisName, maxLen)
” citing that

“APPLESCRIPT ERROR
Finder got an error: The operation could not be completed because there is already an item with that name.”

I presume my script error for the same reason, it just doesn’t tell me.
Thanks again!

Nigel’s script is set up to check the names of the files in a folder. You are feeding it the name of a file. For that, you only need the "truncateName portion of this. The checkNames part just identifies files that need fixing.

Ok… I think I understand. However, I will need some sort of name-checking to occur, as any file within the 17-character contraints would not need appending anyway.

I’ve taken out the checknames clause, but there is more twewaking still necessary. This is where I’m at so far:

on adding folder items to this_folder after receiving these_items
	repeat with i from 1 to number of items in these_items
		truncateName(these_items)
	end repeat
end adding folder items to


on truncateName(these_items, maxLen)
	set maxLen to 17
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	if ((count i's text item 1) = (count i)) then
		set newName to text 1 thru maxLen of i
	else
		set nameExtn to "." & i's text item -1
		set newName to text 1 thru (maxLen - (count nameExtn)) of i & nameExtn
	end if
	set AppleScript's text item delimiters to ASTID
	return newName
end truncateName

Thanks mucho, once again :slight_smile:

What tweaking, awacht? Does it work for you? If not, what goes wrong? It does compile, so I assume it’s missing something but don’t know what.

Are you saying that you don’t know how to keep the first two characters?
What is the condition you want to apply for keeping the last few?

Not enough info…

Well, at this point - nothing is really happening at all. At least with the folder action version, so I don’t know where exactly it is erroring. I keep working with a version of the folder action one, only it is not a folder action - I’ve kept it like the original, in which the script asks you to select a folder. This version gives me error messages, so I use that to help debug. I figure once that one runs through ok - setting up the folder action version should take just a few clicks here and there. In any case, I’ll link a pic of the current behavior.

Now to elaborate the specifics I wish to employ:

  1. Some files dropped into the folder I wish to affect are ok, anything below 17-characters (not including the file extension) is fine - the script should ONLY act upon files with names longer than 17-characters
  2. Every file will have a 1, or 2-digit prefix. This prefix needs to remain after the file is truncated.
  3. SOME files will have a suffix, It is always going to be “CarlaPS”. This suffix will be right before the file extension, and if it exists - this suffix needs to remain within the filename as well.

So basically, the script needs to check filename length first…

  • If it’s over 17-characters, the name needs to be truncated - always leaving the 2-character “numbered” prefix
  • If the aforementioned suffix is present, that needs to remain too - so only the portion of the filename between the prefix and suffix need to be truncated.

Thanks again for you input - I appreciate the attention.
I’ll get this scripting stuff yet!
Here’s the pic of the script in action:

http://mechamaniac.com/andy/scripting/errorpic.png

Your pic indicates that you are now feeding a folder to the truncation handler, which wants a file, so lets go back to square 1. The truncation reference I gave you was an example of how such things are done - not a solution to your specific problem. It is set up to do its thing to a chosen folder, first getting all the files in the folder and in nested folders and checking each file name for length. For those that are too long, it passes them to the trucateName handler which grabs the extension and then bites off all but the first “n” characters of the base name. Finally, it adds the extension back and passes the result back to the checkFiles part. It is the checkNames handler that actually changes the name of the file. The ideas here are adaptable to your task, but they aren’t meant to do it for several reasons.

First, you want it to be in a Folder Action and that’s a no-no. You can’t change the name of a file in a folder with a Folder Action attached to the containing folder because the Folder Action will regard the file with the changed name as “new” and trigger again. The only way to change names is first to move each file to a new folder and change it’s name after it’s moved. The approach taken in the example is simpler - you load in a bunch of files, run the script, and you’ve got them all fixed. It even looks after the folder name so you can pass the whole thing to a machine that won’t take long names.

Second, the example just truncates from the front - it doesn’t understand keeping some numbers at the front and possibly but not always keeping endings like “BobPS” or “CarlaPS”. With 2 characters in front (and if you set it for 2 it must be 2) and possibly 7 at the end to be kept in a 17 character name, there are only 8 left after the cut, so we have to do something about checking that the truncated name is not the same as one already in the folder or this will fail. I can readily imagine a series of images that all start with the same project numbers, end with a person’s name, and are very similar in the middle so you have to think about what characters to remove, or to put it another way, what should be left in the middle? To boil that down, how should the middle of the name be truncated if the middle of the name makes the total more than 17 characters?

So what am I suggesting here? Well for openers, that you study what the script I referred to does and if you don’t understand some part of it, ask. Then we’ll have a basis for dealing with the rest in this “conversation”.

Try something like this, awacht:

on adding folder items to this_folder after receiving these_items
	set keepEnds to {"CarlaPS", "BobPS"}
	tell application "Finder" to repeat with this_item in these_items
		set maxLen to 17
		set currNum to 0
		set currName to this_item's name
		set nameExt to this_item's name extension
		tell (count nameExt) to if it is 0 then
			set newName to currName
		else
			set newName to currName's text 1 thru -(it + 2)
			set nameExt to "." & nameExt
		end if
		if (count newName) > maxLen then
			set newEnd to ""
			repeat with keepEnd in keepEnds
				if newName ends with keepEnd then
					set newEnd to keepEnd
					set maxLen to maxLen - (count newEnd)
					exit repeat
				end if
			end repeat
			set newName to newName's text 1 thru maxLen
			set fullName to newName & newEnd & nameExt
			tell folder this_folder to if exists file fullName then
				set numPad to "0"
				set newName to newName's text 1 thru (maxLen - 2)
				repeat while (exists file fullName) and currNum < 100
					set currNum to currNum + 1
					set fullName to newName & (numPad & currNum)'s ¬
						text -2 thru -1 & newEnd & nameExt
				end repeat
			end if
			if currNum < 100 then
				set this_item's name to fullName
			else
				display dialog "Could not change name of file \"" & ¬
					currName & "\"." buttons {"Cancel", "Continue"} default button 2
			end if
		end if
	end repeat
end adding folder items to

Great script, Kai. It even ate (properly, mind you) this file name: “123George IsHere” & return & “1234567890.bobps.txt”. Very robust. Whacked it down to size, kept 123, kept bobps, eliminated the return. I should add - thank you for doing it. I felt a bit guilty just leaving it as a commentary, but had to run.

Thanks Kai. I checked the script out and it works great.
Thanks adam also, for the guidance - even though Kai beat me to the punch (not much of a race though)

thanks again guys

…Edit: I couldn’t leave well-enough alone. I figured it might also be convenient to do this with a script under manual control. I’ve started something that will allow the user to select a folder, the script will look at file name’s lengths, then when applicable, present them with a warning dialog, and ensuing dialog in which they can rename the file as they wish. Here is what I’ve got so far… it works to the point where an “offending” file will trigger the warning and renaming dialogs - but I am running into trouble applying the new name to the file -This is where it errors.

here’s the script so far:

set myfolder to (choose folder)
checkNames(myfolder)

on checkNames(theFolder)
	set maxLen to 21
	tell application "Finder"
		set fileNames to name of every file of theFolder
		repeat with thisName in fileNames
			set ASTID to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "."
			set nameExtn to "." & thisName's text item -1
			if (thisName's length > maxLen) then
				display dialog "This file is too long:" & return & return & thisName & return & return & "--Please rename the file so that the length is shorter than 17 characters" & return & "--DON'T include the .EPS/.PDF file extension!!" buttons {"Cancel", "Let's do this..."} default button 2
				if the button returned of the result is "" then
				else
					display dialog "Enter the new file name" default answer "" buttons {"OK"} default button 1
					set newName to text returned of the result
					set name of file thisName to newName & nameExtn
				end if
			end if
		end repeat
	end tell
end checkNames
end

:cool: