Script help—iteration

Hey, so I’ve got the following script:


tell application "Finder" to set ItemList to selection
repeat with thisItem in ItemList
	tell application "Finder"
		set diag_1 to display dialog "Please Add Version Comments:
(Automatically Replaces)" default answer "" buttons {"Add", "Cancel", "Replace"} default button 3 with title "Add Keywords"
		set a_com to text returned of result
		
	end tell
	
	-- automatic appends comment; replace on command v1.5
	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
		if button returned of diag_1 is "Add" then
			
			set ItemList to selection
			
			
			if not ItemList is {} then
				repeat with thisItem in ItemList
					set p_path to (POSIX path of (thisItem as alias))
					set e_com to comment of thisItem
					
					
					if e_com is "" then
						set comment of thisItem to a_com
						
					else if button returned of diag_1 is "Replace" then
						
						set n_com to e_com & ", " & a_com
						set comment of thisItem to n_com
						set n_com2 to e_com & ", " & a_com
						
					end if
				end repeat
			end if
			
		else
			
			set ItemList to selection
			if not ItemList is {} then
				repeat with thisItem in ItemList
					set comment of thisItem to a_com
					
					
				end repeat
			end if
			
		end if
		
		
	end tell
end repeat
-- v 1.5

Basically this script prompts you for comments, duplicates the item and adds a revision number to it (“r1”,“r2”,“r3”)

I’ve got two things I haven’t quite been able to hash out”

  1. Once we get to “r10”, the next revision is “r10 r1”. How do I get an r11 here?

  2. If I try to run this on a folder, i get a folder with a revision number and a trailing “.”

Can anyone help me debug this?

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

Hi,

try this, I also simplified the code to add the comments, your additional repeat loops aren’t needed:


tell application "Finder" to set ItemList to selection
repeat with thisItem in ItemList
	tell application "Finder"
		set diag_1 to display dialog "Please Add Version Comments:
(Automatically Replaces)" default answer "" buttons {"Add", "Cancel", "Replace"} default button 3 with title "Add Keywords"
		set {text returned:a_com, button returned:a_button} to diag_1
		
	end tell
	
	-- automatic appends comment; replace on command v1.5
	set {name:Old_Name, name extension:Ext} to info for thisItem as alias
	if Ext is missing value then
		set Ext to ""
	else
		set Ext to "." & Ext
	end if
	if Ext is not "" then set Old_Name to text 1 thru ((count Old_Name) - (count Ext)) of Old_Name
	set {TID, text item delimiters} to {text item delimiters, " r"}
	set nameItems to text items of Old_Name
	if (count nameItems) > 1 then
		set last item of nameItems to ((get last item of nameItems as integer) + 1) as text
		set New_Name to (nameItems as text) & Ext
	else
		set New_Name to Old_Name & " r1" & Ext
	end if
	set text item delimiters to TID
	
	tell application "Finder"
		set dupItem to duplicate thisItem
		set name of dupItem to New_Name
		-- set p_path to (POSIX path of (dupItem as alias))
		set e_com to comment of dupItem
		if e_com is "" or a_button is "Replace" then
			set comment of dupItem to a_com
		else
			set comment of dupItem to e_com & ", " & a_com
		end if
	end tell
end repeat
-- v 1.5

Thanks, Mate. That makes sense.

I dabble too much and should really sit down and learn this stuff for real one day.

D