Files in sequence order?

Okay, I know its an easy script for the pro scripters out there. And I have tried and tried. Can someone send me an AppleScript for OSX that will number my files from 1 to whatever inside a folder window. I made a script that will do it, but it scrambles the files up before it re-numbers them. I need the script to number the files in the order that I have the names.

Example:

(name)

DCS100a.mov
DCS100b.mov
DCS100c.mov
DCS100d.mov
DCS100e.mov

AppleScript to-

1.mov
2.mov
3.mov
4.mov
5.mov

I know you have to use a counter variable. But my script mixes the files up before it re-numbers them. :

Thanks!

Without seeing your code it’s hard to tell what the problem is, but I’m guessing it’s because you are getting the current file list within a loop. Since the files are changing as part of that loop you’re trying to hit a constantly-moving target.

Instead, first get a list of all the files in the folder at the beginning of your script, then loop through that list and you shouldn’t have a problem.

The following should get you started:

tell application "Finder"
	-- get the current folder listing
	set sourceFiles to every file of folder "Macintosh HD:some folder"
	-- start off with file 1
	set fileCounter to 1
	--loop through the files
	repeat with aFile in sourceFiles
		--renaming each one in turn
		set name of aFile to (fileCounter & ".mov") as string
		-- and incrementing the counter after each file
		set fileCounter to fileCounter + 1
	end repeat
end tell

I like to get much more fancy in my naming by adding a prefix, a suffix, choosing the starting number, and adding leading zeroes (not really necessary in X but still much nicer to look at). I also ensure that the files are named in order through a sort since the Finder sometimes returns a selection in an odd order. Finally, I also add a try handler to catch duplicate file names. To work, just select the files you want to rename in the Finder–make sure you only select the files you want renamed!–then run the following script (it could be saved as a drag and drop applet or as a script to be run from the Script Menu):

property the_prefix : ""
property the_suffix : ".jpg"
property start_num : 1
property file_count : 0

on run
	tell application "Finder" to set the_selection to selection
	if the_selection = {} then
		beep
		display dialog "Error: Nothing is selected in the Finder. Please select the files to rename and try again." buttons {"OK"} default button 1 with icon 0
		return
	end if
	tell application "Finder" to set folder_name to ((name of (container of (item 1 of the_selection as alias))) as string)
	set the_but to button returned of (display dialog ("I'm about to rename the files in the folder:
	
			“" & folder_name & "”

Shall I continue?") buttons {"No", "Yes"} with icon 2)
	if the_but = "Yes" then
		open (the_selection)
	else
		return
	end if
end run

on open (the_files)
	set file_count to count of the_files
	set the_err to ""
	repeat
		set start_num to text returned of ¬
			(display dialog the_err & "Enter the number to start with:" default answer (start_num as string) buttons {"Cancel", "OK"} default button 2 with icon 1)
		if start_num is not "" then
			try
				set start_num to start_num as integer
				exit repeat
			on error
				set the_err to ("“" & start_num & "” is not an integer. Please try again." & return & return)
			end try
		end if
	end repeat
	set the_prefix to text returned of ¬
		(display dialog "Enter the prefix to add to every file (leave blank for none):" default answer the_prefix buttons {"OK"} default button 1 with icon 1)
	set the_suffix to text returned of ¬
		(display dialog "Enter the suffix to add to every file (leave blank for none):" default answer the_suffix buttons {"OK"} default button 1 with icon 1)
	
	set the_but to button returned of (display dialog "Should the files be sorted by name before being renamed? (If you choose “No” they will be renamed in the order selected in the Finder which sometimes causes unexpected results.)" buttons {"No", "Yes"} default button 2 with icon 1)
	if the_but = "Yes" then
		set file_strings to {}
		repeat with i from 1 to file_count
			set file_strings to file_strings & {(item i of the_files as string)}
		end repeat
		set the_files to my list_sort(file_strings)
	end if
	
	repeat with i from 1 to file_count
		try
			process_file(item i of the_files as alias)
		end try
	end repeat
	set finString to ("Finished renaming " & file_count & " files by number (" & ((current date) as string) & ").")
	display dialog finString buttons {"OK"} default button 1 with icon 1 giving up after 20
	return finString
end open

on process_file(the_file)
	set file_name to (the_prefix & ((my add_leading_zeros(start_num, (start_num + file_count)) as string) & the_suffix)) as string
	repeat
		try
			tell application "Finder"
				set name of the_file to file_name
				set extension hidden of the_file to false
			end tell
			exit repeat
		on error
			activate
			set file_name to text returned of ¬
				(display dialog "This file already exists." & return & ¬
					"Please choose another name:" default answer file_name buttons {"Cancel", "OK"} default button 2 with icon 2)
		end try
	end repeat
	set start_num to start_num + 1
end process_file

on add_leading_zeros(the_number, total_number)
	set the_leading_zeros to ""
	if total_number >= 1000 then
		if the_number < 10 then
			set the_leading_zeros to "000"
		else if the_number < 100 then
			set the_leading_zeros to "00"
		else if the_number < 1000 then
			set the_leading_zeros to "0"
		end if
	else if total_number >= 100 then
		if the_number < 10 then
			set the_leading_zeros to "00"
		else if the_number < 100 then
			set the_leading_zeros to "0"
		end if
	else
		if the_number < 10 then set the_leading_zeros to "0"
	end if
	return (the_leading_zeros & the_number as string)
end add_leading_zeros

to list_sort(the_list)
	copy {the_list, (length of the_list)} to {sorted_list, the_length}
	set the_gap to the_length
	repeat while the_gap > 1
		set {the_gap, the_flag} to {(the_gap div 2), true}
		repeat while the_flag is true
			set the_flag to false
			repeat with index_1 from 1 to (the_length - the_gap)
				set index_2 to (index_1 + the_gap)
				if (item index_1 of sorted_list) > (item index_2 of sorted_list) then ¬
					set {(item index_1 of sorted_list), (item index_2 of sorted_list), the_flag} to {(item index_2 of sorted_list), (item index_1 of sorted_list), true}
			end repeat
		end repeat
	end repeat
	return sorted_list
end list_sort

As always, watch the line wraps.

Jon

Camelot Your The Best! :smiley: Your example code helped tremendously! Here is the final script that I made.


try
	tell application "Finder" to set the source_folder to (folder of the front window) as alias
on error -- no open folder windows
	set the source_folder to path to desktop folder as alias
end try

tell application "Finder"
	-- get the current folder listing 
	set sourceFiles to every file of source_folder
	-- start off with file 1 
	display dialog "Start with what number?" default answer "1"
	set fileCounter to text returned of result as number
	--loop through the files 
	repeat with aFile in sourceFiles
		--renaming each one in turn 
		set name of aFile to (fileCounter & ".mov") as string
		-- and incrementing the counter after each file 
		set fileCounter to fileCounter + 1
	end repeat
end tell