Finder count script broken by 10.6

I’m thinking that I’m going to have to learn a different scripting language since it appears that Apple has broken Finder in version 10.6. But before I go there, maybe one of you out there will have a suggestion I can use to salvage this script.

We get in anywhere between 200 - 500 images a day from our photo studio. All of these images come in named with a three digit department number, followed by and alpha code, then the actual file name. This script looks at those first three digits (the department number) and counts all of the images that belong to a certain department so that we can track which areas have the heaviest workload. In 10.5, this script ran in one to three minutes depending on the number of images) accessing a folder on our server (smb://). The script in 10.6 fails most of the time and when it does work, takes upwards around 20 minutes to run start to finish. I tried copying this image folder to my local disk and running the script on it… still took over 6 minutes. We still have access to 10.5, so it’s not the end of the world… but we will be getting upgraded eventually so I would like to cut this one off at the pass.

Here is the code:


--Department lists broken out by division numbers
current date
set AccList to {"031", "032", "033", "035", "122", "131", "132", "134", "135", "222", "234", "235", "322", "331", "422"}
set DomList to {"011", "015", "064", "075", "111", "115", "164", "175", "211", "215", "264", "311", "315", "364", "411", "464", "511", "946", "949"}
set HomeList to {"020", "023", "024", "026", "061", "065", "121", "126", "161", "165", "223", "830", "832"}
set HouseList to {"025", "060", "063", "066", "067", "125", "266", "566"}
set IntList to {"034", "036", "094", "104", "133", "143", "228", "229", "230", "231", "233", "333", "433", "443", "533", "743"}
set JewelList to {"027", "029", "127", "129", "227", "327", "427", "527", "727", "827"}
set JrList to {"044", "084", "144", "242", "244", "344", "404", "405", "406", "444", "544", "644", "744"}
set KidList to {"012", "045", "046", "047", "048", "049", "073", "074", "076", "096", "097", "112", "145", "147", "197", "245", "246", "247", "248", "272", "346", "348", "448", "920", "922"}
set MenList to {"051", "052", "053", "056", "151", "152", "153", "156", "251", "252", "253", "254", "255", "256", "352", "353", "356", "452", "453", "456", "553", "552", "556", "656"}
set MissList to {"113", "213", "413", "513", "543", "571", "913"}
set MissPlusList to {"013", "014", "016", "019", "114", "171", "214", "243", "313", "314", "342", "343", "345", "412", "414", "442", "514", "614", "643", "713", "943"}
set MissUpdatedList to {"040", "041", "043", "071", "095", "212", "324", "371", "424", "471", "524", "613", "624", "836"}
set ShoeList to {"037", "039", "137", "139", "218", "239", "287", "318", "418"}
set YoungList to {"017", "055", "057", "058", "059", "089", "117", "146", "155", "157", "159", "172", "217", "257", "355", "357", "457"}
set Miscellaneouslist to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

--Count Script
set acc to 0
set dom to 0
set homedec to 0
set house to 0
set int to 0
set jewel to 0
set jr to 0
set kid to 0
set men to 0
set miss to 0
set missplus to 0
set missup to 0
set shoe to 0
set young to 0
set misc to 0

set InBox to alias "Scene7:Done:Inbox Archive:082710"
tell application "Finder"
	repeat with i in AccList
		set acc to acc + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in DomList
		set dom to dom + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in HomeList
		set homedec to homedec + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in HouseList
		set house to house + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in IntList
		set int to int + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in JewelList
		set jewel to jewel + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in JrList
		set jr to jr + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in KidList
		set kid to kid + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in MenList
		set men to men + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in MissList
		set miss to miss + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in MissPlusList
		set missplus to missplus + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in MissUpdatedList
		set missup to missup + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in ShoeList
		set shoe to shoe + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in YoungList
		set young to young + (count (files in InBox where name begins with i))
	end repeat
	repeat with i in Miscellaneouslist
		set misc to misc + (count (files in InBox where name begins with i))
	end repeat
	
	--Image Total
	set ImageTotal to (acc + dom + homedec + house + int + jewel + jr + kid + men + miss + missplus + missup + shoe + young + misc)
	--Breakdown by department
	(set the clipboard to "" & ImageTotal & " image(s) total 

Accessories: " & acc & " 
Domestics:  " & dom & " 
Home Decor:  " & homedec & " 
Housewares: " & house & " 
Intimates: " & int & " 
Jewelry: " & jewel & " 
Juniors:  " & jr & " 
Kids:  " & kid & " 
Mens:  " & men & " 
Misses:  " & miss & " 
Misses Special Sizes:  " & missplus & " 
Misses Updated:  " & missup & "
Shoes:  " & shoe & " 
Young Mens:  " & young & " 
Miscellaneous: " & misc & "")
	
end tell

Any suggestions would be greatly appreciated.

Hi.

When scripting the Finder, it’s best to use Finder references rather than aliases or anything else, especially in extended references involving contents and/or filters.

tell application "Finder"
	set InBox to folder "Scene7:Done:Inbox Archive:082710" -- A Finder 'folder', not an 'alias'.
	
	repeat with i in AccList
		set acc to acc + (count (files in InBox where name begins with i))
	end repeat
	
	-- etc.
end tell

I gave it a go, but the script is still very slow and crashed halfway through. Until Apple fixes Finder, I’m starting to think that I may need to find an alternate solution to this problem. This is frustrating only because this exact same script worked flawlessly in 10.5.

I’m certain there are many regulars here who would be able to devise an improved, speedier solution, but, since this is a project with an express commercial application, why not post it as a job to MacFreelancer? This is simple enough that it wouldn’t be very costly, so win-win for your employer and a knowledgeable developer.

I had considered that, but I don’t think I will be able to free up the financial resources. I will run it by them however, cause well, it never hurts to ask! I was just hoping maybe someone else had seen something similar and had a quick fix. Again, I’m just bummed out that it did work, and now it suddenly doesn’t. They are pretty big into PERL here, so I might just chase down that solution as well.

Hi Rolfster,

you could try a solution like this one working with lists and texts:

I guess this should be faster … hopefully :wink:

set tid to AppleScript's text item delimiters
set accList to {"031", "032", "033", "035", "122", "131", "132", "134", "135", "222", "234", "235", "322", "331", "422"}
set AppleScript's text item delimiters to ""
set NameList to (list folder (path to desktop as text) & "Picturefolder" without invisibles) --your folder here

--just catch the first three digits (as string) in a list
set digitList to {}
repeat with i from 1 to count of NameList
	set i to text 1 thru 3 of item i of NameList
	set end of digitList to i
end repeat

--list to string
set digitList to digitList as text

set ThisCount to -(count items of accList)


(*
in 10.6 you may use:
	set AppleScript's text item delimiters to accList
set ThisCount to count every text item of digitList

instead of the following repeat-loop
	*)


repeat with i from 1 to count of accList
	set AppleScript's text item delimiters to item i of accList
	set MyCount to count every text item of digitList
	set ThisCount to ThisCount + MyCount
end repeat

set AppleScript's text item delimiters to return

Since you’re primarily interested in the files’ names rather than the files themselves, it would be better to use a text method to count the names rather than make the Finder keep ploughing through the files. I don’t have your files so haven’t tested this, but it looks in order.


--Department lists broken out by division numbers
set AccList to {"031", "032", "033", "035", "122", "131", "132", "134", "135", "222", "234", "235", "322", "331", "422"}
set DomList to {"011", "015", "064", "075", "111", "115", "164", "175", "211", "215", "264", "311", "315", "364", "411", "464", "511", "946", "949"}
set HomeList to {"020", "023", "024", "026", "061", "065", "121", "126", "161", "165", "223", "830", "832"}
set HouseList to {"025", "060", "063", "066", "067", "125", "266", "566"}
set IntList to {"034", "036", "094", "104", "133", "143", "228", "229", "230", "231", "233", "333", "433", "443", "533", "743"}
set JewelList to {"027", "029", "127", "129", "227", "327", "427", "527", "727", "827"}
set JrList to {"044", "084", "144", "242", "244", "344", "404", "405", "406", "444", "544", "644", "744"}
set KidList to {"012", "045", "046", "047", "048", "049", "073", "074", "076", "096", "097", "112", "145", "147", "197", "245", "246", "247", "248", "272", "346", "348", "448", "920", "922"}
set MenList to {"051", "052", "053", "056", "151", "152", "153", "156", "251", "252", "253", "254", "255", "256", "352", "353", "356", "452", "453", "456", "553", "552", "556", "656"}
set MissList to {"113", "213", "413", "513", "543", "571", "913"}
set MissPlusList to {"013", "014", "016", "019", "114", "171", "214", "243", "313", "314", "342", "343", "345", "412", "414", "442", "514", "614", "643", "713", "943"}
set MissUpdatedList to {"040", "041", "043", "071", "095", "212", "324", "371", "424", "471", "524", "613", "624", "836"}
set ShoeList to {"037", "039", "137", "139", "218", "239", "287", "318", "418"}
set YoungList to {"017", "055", "057", "058", "059", "089", "117", "146", "155", "157", "159", "172", "217", "257", "355", "357", "457"}
set Miscellaneouslist to {"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z"}

--Count Script
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to return
-- Get the names of every file in the folder, each prefixed with 'return', as a single text.
tell application "Finder" to set nameText to return & ((name of files of folder "Scene7:Done:Inbox Archive:082710") as text)

set acc to countDivisionNames(AccList, nameText)
set dom to countDivisionNames(DomList, nameText)
set homedec to countDivisionNames(HomeList, nameText)
set house to countDivisionNames(HouseList, nameText)
set int to countDivisionNames(IntList, nameText)
set jewel to countDivisionNames(JewelList, nameText)
set jr to countDivisionNames(JrList, nameText)
set kid to countDivisionNames(KidList, nameText)
set men to countDivisionNames(MenList, nameText)
set miss to countDivisionNames(MissList, nameText)
set missplus to countDivisionNames(MissPlusList, nameText)
set missup to countDivisionNames(MissUpdatedList, nameText)
set shoe to countDivisionNames(ShoeList, nameText)
set young to countDivisionNames(YoungList, nameText)
set misc to countDivisionNames(Miscellaneouslist, nameText)

set AppleScript's text item delimiters to astid

--Image Total
set ImageTotal to (acc + dom + homedec + house + int + jewel + jr + kid + men + miss + missplus + missup + shoe + young + misc)
--Breakdown by department
(set the clipboard to "" & ImageTotal & " image(s) total 

Accessories: " & acc & " 
Domestics:  " & dom & " 
Home Decor:  " & homedec & " 
Housewares: " & house & " 
Intimates: " & int & " 
Jewelry: " & jewel & " 
Juniors:  " & jr & " 
Kids:  " & kid & " 
Mens:  " & men & " 
Misses:  " & miss & " 
Misses Special Sizes:  " & missplus & " 
Misses Updated:  " & missup & "
Shoes:  " & shoe & " 
Young Mens:  " & young & " 
Miscellaneous: " & misc & "")

on countDivisionNames(divisionList, nameText)
	set cnt to 0
	repeat with i in divisionList
		set AppleScript's text item delimiters to return & i
		set cnt to cnt + (count nameText each text item) - 1
	end repeat
	
	return cnt
end countDivisionNames

Amazing. I knew there was a way to do it like that, but I just couldn’t figure it out myself. I was looking for a hint to lead me in the right direction, didn’t expect a fully functional answer that works 100 times better than my previous solution. I am in your debt sir, thank you very much. I can now stop banging my head up against my desk.

I guess that replacing


tell application "Finder" to set nameText to return & ((name of files of folder "Scene7:Done:Inbox Archive:082710") as text)

by


tell application "System Events" to set nameText to return & ((name of files of folder "Scene7:Done:Inbox Archive:082710") as text)

would fasten a bit.

Since about one year, I replaced calls to Finder by calls to System Events because they are really faster.
I trigger the Finder only when windows and icons are used or when I want to duplicate an item.

Sometimes I may use do shell script "mv .
but it really moves the items and most of the times I need a duplicate.
What a fool, to really duplicate, there is cp !

Yvan KOENIG (VALLAURIS, France) dimanche 29 août 2010 13:34:59

Thanks for the tip Yvan, will keep that in mind. I’m trying to weed out Finder interactions as much as possible, this should help.