Help with File Rename Clean-Up

So, I want to version my files in a very simple manner (mostly indd)

So I start with filename.ext file name.

I want for this to be duplicated, and renamed filename r.ext (r for revised).

If I make another version, I want this to copy and be filename r2.ext (r2 for revised twice)

I’ve come up with the following code.

It works, but

  1. I KNOW there’s a better way to do it, as I’m a junior-grade scriptor

  2. I couldn’t figure out a sub-routine to handle the variations from no r, to r, to r2 and beyond, so i dropped the " r" and made the base " r1"

Hopefully that makes sense and you can help me clean this code up.

set OrigTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."

tell application "Finder"
	set ItemList to selection
	repeat with i from 1 to number of items in ItemList
		set thisItem to item i of ItemList
		
		set sel_name to name of thisItem -- captures whole name name
		display dialog sel_name
		set Old_Name to first text item of sel_name --captures everything up to suffix
		display dialog Old_Name
		set Old_Type to second text item of sel_name
		display dialog Old_Type
		set AppleScript's text item delimiters to "" -- resets TIDs
		set w_count to count text items of Old_Name -- counts number of letters in name
		display dialog w_count
		if w_count > 4 then
			set LChar to text item w_count of Old_Name -- captures last character
			display dialog LChar
			set SLChar to text item (w_count - 1) of Old_Name -- captures 2nd to last letter
			display dialog SLChar
			set TLChar to text item (w_count - 2) of Old_Name -- captures 3nd to last letter
			display dialog TLChar
			set FLChar to text item (w_count - 3) of Old_Name -- captures 4nd to last letter
			display dialog FLChar
			set TLChar_set to TLChar & SLChar & LChar
			display dialog TLChar_set
			set Name_Thru to text items 1 thru (w_count - 3) of Old_Name as string
			display dialog Name_Thru
			set Poss_Suffix to {" r1", " r2", " r3", " r4", " r5", " r6", " r7", " r8", " r9"}
		end if
		if TLChar_set is not in Poss_Suffix then
			set New_Name to Old_Name & " r1." & Old_Type
			display dialog New_Name
		else if TLChar_set is in Poss_Suffix then
			set New_Suffix to " r" & (LChar + 1)
			display dialog New_Suffix
			set New_Name to Name_Thru & New_Suffix & "." & Old_Type
			display dialog New_Name
		end if
		duplicate thisItem
		set Copy_Item to selection as string
		set name of file Copy_Item to New_Name
	end repeat
end tell

Browser: Safari 525.13
Operating System: Mac OS X (10.5)

Hi,

try this


tell application "Finder" to set ItemList to selection
repeat with thisItem in ItemList
	set {name:Old_Name, name extension:Ext} to info for thisItem as alias
	if Ext is missing value then set Ext to ""
	if Ext is not "" then set Old_Name to text 1 thru ((count Old_Name) - (count Ext) - 1) of Old_Name
	
	tell Old_Name to set {suffixText, suffixNumber} to {text -3 thru -2, text -1}
	considering case
		set isRevised to suffixText is " r" and suffixNumber is in "123456789"
	end considering
	if isRevised then
		set New_Name to text 1 thru -4 of Old_Name & suffixText & ((suffixNumber as integer) + 1) & "." & Ext
	else
		set New_Name to Old_Name & " r1." & Ext
	end if
	
	tell application "Finder"
		set dupItem to duplicate thisItem
		set name of dupItem to New_Name
	end tell
end repeat

Thanks;

That helps and I learned a lot looking over this.

I need to study up on the “considering case” call, but otherwise this makes a lot of sense!

Is there a way to force the first iteration to xxx r.ext and then the next xxx r2.ext?

dt

Yes, of course


tell application "Finder" to set ItemList to selection
repeat with thisItem in ItemList
	set {name:Old_Name, name extension:Ext} to info for thisItem as alias
	if Ext is missing value then set Ext to ""
	if Ext is not "" then set Old_Name to text 1 thru ((count Old_Name) - (count Ext) - 1) of Old_Name
	
	tell Old_Name to set {suffixText, suffixNumber} to {text -3 thru -2, text -1}
	considering case
		set isRevised to (suffixText is " r" and suffixNumber is in "123456789") or (text -2 thru -1 of Old_Name is " r")
	end considering
	if isRevised then
		if text -2 thru -1 of Old_Name is " r" then
			set New_Name to text 1 thru -3 of Old_Name & suffixText & "r2." & Ext
		else
			set New_Name to text 1 thru -4 of Old_Name & suffixText & ((suffixNumber as integer) + 1) & "." & Ext
		end if
	else
		set New_Name to Old_Name & " r." & Ext
	end if
	
	tell application "Finder"
		set dupItem to duplicate thisItem
		set name of dupItem to New_Name
	end tell
end repeat

Cool, thanks!

It works perfect and gives me room to learn.

I love this site!

Thanks again

dt

Actually. the 2nd revision of the script adds characters in an odd way.

If your file is daniel.jpg, the next one is correct, (daniel r.jpg) but then the next iteration is screwy. comes out as daniell r2.jpg (with the last character of the original name doubled.)

I’m going to look into it myself. but if you have a second to debug it, that would be great too.

replace this, the “suffixText” has to be removed


.
if text -2 thru -1 of Old_Name is " r" then
	set New_Name to text 1 thru -3 of Old_Name & " r2." & Ext
else
.