Renaming numerical files to months of the year

Hi guys and Gals,

Can any one help me here? Im totally new at this and am trying to modifie a Script to fit my purposes but currently i’ve hit a wall!!

What I want to do
Select a folder which contains numbered .jpegs. Rename the jpegs to the months of the year. I.e so that 1=Jan, 2=Feb, 3=Mar, 12=Dec, etc etc, and then loops around when it reaches 13, i.e 13=Jan 14=Feb 24=Dec etc.

What I have got so far

try
	tell application "Finder" to set the inputFolder to (choose folder) 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 inputFolder
	-- 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 (month & ".jpg") as string
		-- and incrementing the counter after each file 
		set fileCounter to fileCounter + 1
	end repeat
end tell

Any ideas?? I’m a bit simple could any suggestions be explained in laymens terms!

Cheers for taking the time to read this post.

TimP

Model: G4 733 Quicksilver
AppleScript: 1.9.3
Browser: Safari 312
Operating System: Mac OS X (10.3.9)

Can you tell us what you had in mind for fileCounter? It doesn’t seem to be used for anything.

Are you saying that the files are named 1.jpg, 2.jpg, etc.? What if there are more than 12 files in the folder. Won’t you end up trying to create two jan.jpg files?

You can set theMonthList to {“Jan”, “Feb”, etc…} and then set file names to item X of theMonthList as you loop.

This is what he meant by “Counter”
****Do not use this script except on TEST FILES. It is an example of why that approach ‘almost works’, for the newbie :lol:


property TheMonth : {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}
choose folder without invisibles
set theFolder to result
tell application "Finder"
	set filelist to items of theFolder
end tell
set counter to 1
set counter2 to 1
repeat with JPEG in filelist
	
	set Monthname to item counter of TheMonth as text -- Sets Monthname to increment of counter
	
	if counter2 ≤ 9 then
		set name of JPEG to "0" & counter2 & "_" & Monthname & ".jpg" as string
	else
		if counter2 ≥ 10 then set name of JPEG to counter2 & "_" & Monthname & ".jpg" as string
	end if
	
	set counter to counter + 1
	if counter > 12 then set counter to 1
	set counter2 to counter2 + 1
end repeat

There is no sort command in X. You can’t say “sort filelist by name”. That was os9. If you had the sort, you could line the files up correctly. There is a “custom sort” option, but this is really the wrong route.

Here are the limiting factors:

If the jpgs are named to the months of the year, you will either have to-

-send each year to a new folder to avoid a “name already exists” error
*can’t have Jan.jpg and Jan.jpg in the same folder
or
-add another value to the filename as you come to each new year
*ie A_ Jan.jpg, B_ Jan.jpg or 1_ Jan.jpg, 2_ Jan.jpg; you want the added value in front of the Month, otherwise your months will be messy in the folder.

As pointed out above, you don’t want to simply take the files in a loop and start associating them to the next month in the list. If you do that, you won’t be sure that 24.jpg is Dec.jpg.
You need to associate the filename with a month in the list.
SC

The question is how to associate the files to the date in a list once the number gets past 12…

property TheMonth : {“Jan”, “Feb”, “Mar”, “Apr”, “May”, “June”, “July”, “Aug”, “Sep”, “Oct”, “Nov”, “Dec”}

I’m sure there’s more than one way, but I used this approach: Divide the file name number by 12.

49.jpg → 49/ 12 = 4.08

Take the resulting number without the decimal value and multiply it by 12.

4 X 12 = 48

Subtract 48 from 49 and you get a remainder of 1.

Now you have your month for 49, which is 1 (Jan) in the list. You can use this to get all your months except numbers divisible by 12. Then you get no remainder, or “0” for that value. Set that case for “Dec”, of course.

This relies on the filename format “01.jpg” or “1.jpg” to work:


set AppleScript's text item delimiters to ""--You may not need this line, I was using it to clear mem in testing
property TheMonth : {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}

choose folder without invisibles
set theFolder to result

tell application "Finder"
	set filelist to items of theFolder
end tell
set counter to 1
repeat with JPEG in filelist
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	set JPEGID to (text item 1 of (name of JPEG as string)) 
	set AppleScript's text item delimiters to ASTID
	
	try
		if JPEGID is less than or equal to 12 then
			set (name of JPEG) to counter & " " & ((item JPEGID) of TheMonth as string) & ".jpg" as text
		end if
		
		if JPEGID is greater than or equal to 13 then
			set Divisor to (JPEGID / 12) as number
			set RoundDiv to round Divisor rounding toward zero
			set Subtractor to 12 * RoundDiv
			set MonthID to JPEGID - Subtractor as number
			set NewPicName to (item MonthID) of TheMonth as string
			set (name of JPEG) to counter & " " & NewPicName & ".jpg" as string
		end if
	end try
	
	try
		if MonthID is 0 then
			set (name of JPEG) to counter & " Dec.jpg" as string
		end if
	end try
	
	set counter to counter + 1
	
end repeat

You may want to change the value of the counter with your dialog, but I set it up to number the files sequentially. Again, that’s only to deal with the redundant file names
SC

Sitcom, does applescript have a mod operator or function?

If so, could you get the month ID by just taking the filenumber mod 12. (plus accounting for decembers like 24, 36, etc., as you pointed out.)

digest4d wrote:

With the mod operator and a little more math, the entire number-to-month conversion can be written much more efficiently:

set AppleScript's text item delimiters to "" --You may not need this line, I was using it to clear mem in testing
property TheMonth : {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"}

choose folder without invisibles
set theFolder to result

tell application "Finder"
	set filelist to items of theFolder
end tell
set counter to 1
repeat with JPEG in filelist
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to "."
	set JPEGID to (text item 1 of (name of JPEG as string))
	set AppleScript's text item delimiters to ASTID
	
	try
		set MonthID to ((JPEGID + 11) mod 12) + 1
		set (name of JPEG) to counter & " " & ((item MonthID) of TheMonth as string) & ".jpg" as text
	end try
	
	set counter to counter + 1
	
end repeat


Browser: Safari 125.12
Operating System: Mac OS X (10.3.8)

My hope in posting was that someone would have the more effiecient form and post it. Thanks for the lesson back at me!
SC