Please help with AppleScript to rename files - even and odd numbers

Hi,

first of all I wanted to say this forum is amazing, full of really talented people who are generous in sharing their skills and knowledge. I’ve searched high and low here to find a solution to my problem, but couldn’t find anything, so I thought I’d create a new post, hoping that somebody smart can help me out :slight_smile:

I have different folders with images and need an AppleScript to change the names of those files. The original filenames of the images in the folders are always the same, they are in sequential order, 01.PSD, 02.PSD, 03.PSD etc. However, the amount of images are different from folder to folder, some only have a few images, others may have up to 50 files. Each folder is a photo-book design, and I need to rename the files to make clear how many sides are in the design. The first and last image only show one side of the design, all other images show two sides of the design. So I need a script that renames the files as follows:

01.PSD → 01.PSD
02.PSD → 02-03.PSD
03.PSD → 04-05.PSD
04.PSD → 06-07.PSD
etc…

the last image will be renamed like this:

05.PSD → 08.PSD
or
20.PSD → 38.PSD

Ideally I would like to run this script as a droplet, so I can drop a folder of images onto it, and it renames the files in it. This should work in OS 10.4 and upwards

Anyone’s help would be greatly greatly appreciated!
:slight_smile: :slight_smile: :slight_smile: :slight_smile: :slight_smile:

Maybe this script may help :


set source to (path to desktop as text) & "PSDs"
tell application "System Events" to tell folder source
	set counter to 2
	set nbFiles to count (get every file whose visible is true)
	repeat with i from 2 to nbFiles - 1
		set name of file ((text -2 thru -1 of ("0" & i)) & ".PSD") to (text -2 thru -1 of ("0" & counter)) & "-" & (text -2 thru -1 of ("0" & (counter + 1))) & ".PSD"
		set counter to counter + 2
	end repeat
	set name of file ((text -2 thru -1 of ("0" & nbFiles)) & ".PSD") to (text -2 thru -1 of ("0" & counter)) & ".PSD"
end tell

alternate version :


set source to (path to desktop as text) & "PSDs"
tell application "System Events" to tell folder source
	set counter to 2
	set nbFiles to count (get every file whose visible is true)
	repeat with i from 2 to nbFiles - 1
		set name of file (text -6 thru -1 of ("0" & i & ".PSD")) to text -3 thru -1 of ("0" & counter & "-") & text -6 thru -1 of ("0" & (counter + 1) & ".PSD")
		set counter to counter + 2
	end repeat
	set name of file (text -6 thru -1 of ("0" & nbFiles & ".PSD")) to (text -6 thru -1 of ("0" & counter & ".PSD"))
end tell

KOENIG Yvan (VALLAURIS, France) dimanche 21 juillet 2013 15:30:47

Thank you so much, this is great!!! But is there a way to save this as a droplet so I can just drag and drop a folder with images on it, instead of typing the path into the script by hand? :slight_smile:

I have found this shell scrip, it may help to facilitate the drag and drop action, but I’m not quite sure how to combine it with the previous script?



Drag 'n' Drop Applet Shell

Here is the skeleton of a script designed to function as a 'droplet' (a drag 'n' drop AppleScript application). When double-clicked the script begins executing from the first line. When files/folders are dropped onto the application icon the "on open" handler is called. This example simply passes on the object to "DisplayName", which displays the path name of each selected file or folder (and its entire contents). You can download this script as an AppleScript application here.

displayName(choose file with prompt "Select a file:") --if double-clicked
return -- not needed, but shows that the script stops here when "run"

on open of finderObjects -- "open" handler triggered by drag'n'drop launches
  repeat with i in (finderObjects) -- in case multiple objects dropped on applet
    displayName(i) -- show file/folder's info
    if folder of (info for i) is true then -- process folder's contents too
      tell application "Finder" to set temp to (entire contents of i)
      repeat with j in (temp)
        display dialog j as string -- example of doing something with each item
      end repeat
    end if
  end repeat
end open

source: http://www.nobleswan.com/applescript/AS_snippets.html

Save this script as an application.
You will be allowed to use it as an applet (on run) entry point
or
as a droplet (on open) entry point.


on run
	set theSource to choose folder
	my mainCode(theSource)
end run

on open (sel)
	my mainCode(item 1 of sel)
end open

on mainCode(source)
	tell application "System Events" to tell folder (source as text)
		set counter to 2
		set nbFiles to count (get every file whose visible is true)
		repeat with i from 2 to nbFiles - 1
			set name of file (text -6 thru -1 of ("0" & i & ".PSD")) to text -3 thru -1 of ("0" & counter & "-") & text -6 thru -1 of ("0" & (counter + 1) & ".PSD")
			set counter to counter + 2
		end repeat
		set name of file (text -6 thru -1 of ("0" & nbFiles & ".PSD")) to (text -6 thru -1 of ("0" & counter & ".PSD"))
	end tell
end mainCode

KOENIG Yvan (VALLAURIS, France) dimanche 21 juillet 2013 17:26:12

Hi,

this is a slightly different approach with a bit error handling.
Dropped items which are not folders and folders containing an odd number of files will be skipped


on run
	open (choose folder with multiple selections allowed)
end run

on open theFolders
	repeat with aFolder in theFolders
		-- check for class folder
		tell application "System Events" to set isFolder to class of item (aFolder as text) is folder
		if isFolder then
			tell application "System Events" to set psdFiles to (files of aFolder whose name extension is "PSD")
			set numberOfFiles to count psdFiles
			-- check for an even number of files
			if (numberOfFiles mod 2) = 0 then
				repeat with i from 2 to numberOfFiles - 1
					set fileIndex to (i - 1) * 2
					tell application "System Events" to set name of item i of psdFiles to my pad(fileIndex) & "-" & my pad(fileIndex + 1) & ".PSD"
				end repeat
				tell application "System Events" to set name of item -1 of psdFiles to my pad(i * 2) & ".PSD"
			end if
		end if
	end repeat
end open

on pad(v)
	return text -2 thru -1 of ((100 + v) as text)
end pad


The script must be saved as application (make sure that “Show Startup Screen” and “Stay Open” is deselected)

Thank you so much!!! You guys are awesome :smiley:

Yvan, you’re the king, your script is now exactly what I needed! :slight_smile:
Stefan, you’re the king too, thank you for the variation of the script :slight_smile:

Hello Stefan

I don’t understand why are you treating only the case of even count of files ?

The OP gave two examples, one with an odd count and one with an even count :

05.PSD → 08.PSD
or
20.PSD → 38.PSD

KOENIG Yvan (VALLAURIS, France) lundi 22 juillet 2013 10:28:25

Right, my fault, cut the two lines

I thought this version might be mathematically interesting. :slight_smile:

on run
	mainCode(choose folder)
end run

on open theseItems
	repeat with thisItem in theseItems
		mainCode(thisItem)
	end repeat
end open

on mainCode(thisItem)
	tell application "System Events"
		set thisFolder to disk item (thisItem as text)
		if (class of thisFolder is folder) then
			set c to (count (get files of thisFolder whose name ends with ".PSD")) + 100
		else
			set c to 0
		end if
	end tell
	
	if (c > 102) then
		repeat with i from 102 to (c - 1)
			tell ((i * 202 - 401) as text) to set newName to text 2 thru 3 & "-" & text 4 thru 5 & ".PSD"
			tell application "System Events" to set name of file (text 2 thru 3 of (i as text) & ".PSD") of thisFolder to newName
		end repeat
		tell application "System Events" to set name of file (text 2 thru 3 of (c as text) & ".PSD") of thisFolder to text 2 thru 3 of ((c + c - 2) as text) & ".PSD"
	end if
end mainCode

Pretty code Nigel.

But, the OP wrote : some only have a few images, others may have up to 50 files

which implied that we may have a file named :
98-99.PSD
100.PSD

It seems that everybody missed that :frowning:
All given answers would name the last one as 00.PSD

Edited version :


on run
	set theSource to choose folder
	my mainCode(theSource)
end run

on open (sel)
	my mainCode(item 1 of sel)
end open

on mainCode(source)
	tell application "System Events" to tell folder (source as text)
		set counter to 2
		set nbFiles to count (get every file whose visible is true)
		if nbFiles > 50 then error "Oops, you wrote that the greater number of files was 50 !"
		repeat with i from 2 to nbFiles - 1
			set name of file (text -6 thru -1 of ("0" & i & ".PSD")) to text -3 thru -1 of ("0" & counter & "-") & text -6 thru -1 of ("0" & (counter + 1) & ".PSD")
			set counter to counter + 2
		end repeat
		if nbFiles < 50 then
			set name of file (text -6 thru -1 of ("0" & nbFiles & ".PSD")) to (text -6 thru -1 of ("0" & counter & ".PSD"))
		else
			set name of file "50.PSD" to "100.PSD"
		end if
	end tell
end mainCode

KOENIG Yvan (VALLAURIS, France) mardi 23 juillet 2013 15:52:40

I didn’t miss it. :slight_smile:

I even tested with a folder of 50 files. The name of the last file, using the described naming system, becomes “98.PSD”. 51 files would break things.

Oops

It seems that I am no longer able to count from 1 to 50.

KOENIG Yvan (VALLAURIS, France) mardi 23 juillet 2013 19:27:45