Extension Question

I have this script which changes the version number of a file name ( example: file_name_v1 to file_name_v2) by dropping the file on the application alias in my finder window. It works great until it comes across a file with an extension on it. The problem is I have show file extension turn off on my computer and I don’t want to show them. These are mostly indesign file but they could be quark files. I’m also trying to add the ability to move a copy of the old name into a history folder within the main folder of the file. It works on my desktop, but as soon as I try it with folders on the server it errors out. Here is where I am so far…
Thanks


on open files_rename
	
	tell application "Finder"
		
		repeat with aFile in files_rename
			copy file aFile to folder "History"
			set new_name to name of aFile
			
			set delim to AppleScript's text item delimiters
			set AppleScript's text item delimiters to "v"
			set version_number to text item -1 of new_name
			set original_name to text item 1 of new_name
			set AppleScript's text item delimiters to delim
			
			try
				if name of file new_name contains ".indd" then
					set delim to AppleScript's text item delimiters
					set AppleScript's text item delimiters to "."
					set version_number to text items 1 through -2 of version_number
					set AppleScript's text item delimiters to delim
				end if
			end try
			
			set build_number to version_number as integer
			set new_version_number to build_number + 1
			
			set name of file aFile to original_name & "v" & new_version_number
		end repeat
	end tell
end open

this cannot work. copy doesn’t move files. To move a file use move or duplicate.
Two additional notes:
¢ The name of a file is always the name plus the extension! Hiding the extension in the Finder affects only the view, the name is always complete
¢ files_rename is a list of aliases. Using file aFile causes an error, because aFile is already an alias

Your script is quite dangerous. If there is any additional “v” character in the file name, you get a cut new file name.
I recommed to use an underline character as in your example.
This script strips the Extension from the file name


property version_separator : "_v"

on open files_rename
	repeat with aFile in files_rename
		tell application "Finder" to move aFile to folder "History"
		set {name:Nm, name extension:Ex} to info for aFile
		if Ex is missing value then set Ex to ""
		if Ex is not "" then set Nm to text 1 thru ((count Nm) - (count Ex) - 1) of Nm
		set {TID, text item delimiters} to {text item delimiters, version_separator}
		set {original_name, version_number} to text items of Nm
		set text item delimiters to TID
		set new_version_number to (version_number as integer) + 1
		tell application "Finder" to set name of aFile to original_name & version_separator & new_version_number & "." & Ex
	end repeat
end open

Thanks StefanK,
As you can see I’m still a newbie…but learning more with each script. You are right about the “_v” never thought about that, would have realized it with my first file name with a “v” in it. :smiley: Thanks

I like how compressed you can get the scripts…

The move still doesn’t work I think it moves the file before it can rename it. Everything else is great though.

Thanks again

Yes, sorry, I commented out the line while running the script, because I have no folder “History” on the desktop.
If you want to move the file after renaming, put the line right before the end repeat.
No worry about the new name, an alias “survives” renaming.
If you want to make a copy of the file and keep the original, use the command duplicate

Thanks again but it is not seeing the folder “History” even though the file and History folder are in the same folder

your code points to a folder “History” on your desktop, which is the “root” folder of the Finder
How should AppleScript know, where the folder is?

This moves the file to a folder “History”, which is in the same folder as aFile


tell application "Finder" to move aFile to folder "History" of container of aFile