Random misevaluated if statements?

I’ve written a block of code that is supposed to go through a folder and rename some files. For some reason, although there are 202 files to rename, it only renames 126. If I do repeated passes, it eventually gets them all. I put x and y variables into the code to see how many times each section is run, and it appears that the if statement isn’t being evaluated correctly. That is: x comes out to 126 at the end, and y comes out to 202 (what it should be). Here is the code:


tell application "Finder" to set itemcount to count files of folder chosen_folder
		set x to 0
		set y to 0
		repeat with i from 1 to itemcount
			tell application "Finder"
				set currentfile to file index i of folder chosen_folder as string
				set the_name to the name of file currentfile
				set the_date to the creation date of file currentfile
				set the_extension to the name extension of file currentfile
				set y to y + 1
				if the_name does not contain " on " then
					set x to x + 1
					set AppleScript's text item delimiters to {" #"}
					set the_name to the first item in the_name's text items
					set AppleScript's text item delimiters to {""}
					if the_name contains "." then
						set AppleScript's text item delimiters to {"."}
						set the_name to the first item in the_name's text items
						set AppleScript's text item delimiters to {""}
					end if
					set the_month to the month of the_date as number
					set the_day to the day of the_date as number
					set the_year to the year of the_date as number
					set the_hours to the hours of the_date as number
					set the_minutes to the minutes of the_date as number
					if the number of characters in (the_month as string) is not 2 then set the_month to "0" & the_month
					if the number of characters in (the_day as string) is not 2 then set the_day to "0" & the_day
					if the number of characters in (the_hours as string) is not 2 then set the_hours to "0" & the_hours
					if the number of characters in (the_minutes as string) is not 2 then set the_minutes to "0" & the_minutes
					set the_full_date to " on " & the_year & "-" & the_month & "-" & the_day & " at " & the_hours & "." & the_minutes
					set the name of file currentfile to the_name & the_full_date & "." & the_extension
				end if
			end tell
		end repeat

Anybody have any suggestions as to what’s going on? Thanks a bunch.

Model: 17" PowerBook G4
AppleScript: 1.10 (?)
Browser: Safari 412.5
Operating System: Mac OS X (10.4)

Hi Andy,

It’s because you are getting the file index every iteration of the loop, and the indexes changes when you change other files in the folder. This means when you get roughly half way through the correct indexes no longer exist.

This script references every file of the folder whose name does not contain " on " before looping through them, so the references remain constant through the looping process.

set chosen_folder to choose folder
tell application "Finder"
	set myFiles to files of folder chosen_folder whose name does not contain " on "
	repeat with currentfile in myFiles
		set the_name to the name of currentfile
		set the_date to the creation date of currentfile
		set the_extension to the name extension of currentfile
		set AppleScript's text item delimiters to {" #"}
		set the_name to the first item in the_name's text items
		set AppleScript's text item delimiters to {""}
		if the_name contains "." then
			set AppleScript's text item delimiters to {"."}
			set the_name to the first item in the_name's text items
			set AppleScript's text item delimiters to {""}
		end if
		set the_month to the month of the_date as number
		set the_day to the day of the_date as number
		set the_year to the year of the_date as number
		set the_hours to the hours of the_date as number
		set the_minutes to the minutes of the_date as number
		if the number of characters in (the_month as string) is not 2 then set the_month to "0" & the_month
		if the number of characters in (the_day as string) is not 2 then set the_day to "0" & the_day
		if the number of characters in (the_hours as string) is not 2 then set the_hours to "0" & the_hours
		if the number of characters in (the_minutes as string) is not 2 then set the_minutes to "0" & the_minutes
		set the_full_date to " on " & the_year & "-" & the_month & "-" & the_day & " at " & the_hours & "." & the_minutes
		set the name of currentfile to the_name & the_full_date & "." & the_extension
	end repeat
end tell

Best wishes

John M

Ahhh great. I hate it when things happen that just seem so random and I’m like “what on EARTH could cause THAT?”… that explanation makes perfect sense. Thanks.

One other question: is it possible to do something like

set the_person to (every person whose (value of AIM handles) contains the_name1)

but ignoring the case of and spaces in the AIM handle? I tried

ignoring case and white space

but it didn’t have any effect. I assume it only applies to stuff outside of applications?

Hi Andy,

I don’t think you can. Are all the AIM handles lower case, with no spaces? If that’s the case you could change the variable the_name1 to be in this form before searching.

Or, if not, you could build every combination of capitalization and spaces and check every one in turn (yuck).

Best wishes

John M

I’m currently fooling with the idea of having a mock database, that is, maybe reading the names of all the address book people into one list and all their screennames into another. Then change all screennames in the list to lowercase, along with the_name1 to lowercase. Then look for matches in the screenname list, record which item it was, and find the corresponding person. That’s an awful lot of stuff I’m not clear on how to do though. It also might take too long?