Make script perform faster

I have the following script that we use to make monthly folders and subfolders for filing. I would like to expand the script to create folders and subfolders for an entire year. My delma is I think it can be done faster, but I just don’t know how to make that happen. The process of creating the subfolders seems to be what makes it lag. And I am afraid by doing an entire years worth at once it could really bog down.

property theMonths : {"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"}
property theWeekdays : {"SUN", "MON", "TUE", "WED", "THU", "FRI", "SAT"}

set destinationFolder to choose folder with prompt "Choose destination folder"
tell (current date) to set thisMonth to (it - (its day) * days + days - (its time))
set monthList to {}
repeat with i from 0 to 23
	tell (thisMonth + i * 32 * days) to set end of monthList to ((its month as integer) as string) & " / " & its year
end repeat
set chosenMonth to (choose from list monthList with prompt "choose a month") as string
if chosenMonth is "false" then return
tell thisMonth to set {its month, year} to words of chosenMonth
tell thisMonth to tell it + 32 * days to set lastDate to day of (it - (its day) * days + 86399)
set m to item (1st word of chosenMonth as integer) of theMonths

repeat with oneDay from 0 to lastDate - 1
	tell (thisMonth + oneDay * days) to set {wk, dy} to {its weekday as integer, its day}
	if wk > 2 then
		set folderName to text -2 thru -1 of ("0" & dy) & space & m & space & item wk of theWeekdays
		try
			tell application "Finder" to set dateFolder to make new folder at destinationFolder with properties {name:folderName}
			tell application "Finder" to set SubDestination to make new folder at dateFolder with properties {name:"ADS"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"LIVE"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"FINISHED"}
			tell application "Finder" to set SubDestination to make new folder at dateFolder with properties {name:"PAGES"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"LIVE"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"FINISHED"}
			tell application "Finder" to set SubDestination to make new folder at dateFolder with properties {name:"PHOTOS"}
			tell application "Finder" to set SubDestination to make new folder at dateFolder with properties {name:"PLAN"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"LAYOUT PLAN"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"PRESS ORDERS"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"REPORTS"}
		end try
		
	end if
end repeat
display dialog "The script is finished running." buttons {"OK"} default button 1

Model: iMac
Browser: Safari 533.19.4
Operating System: Mac OS X (10.6)

Hi,

I have a Déjà Vu: http://macscripter.net/viewtopic.php?id=34225 :wink:

replace


repeat with oneDay from 0 to lastDate - 1
	tell (thisMonth + oneDay * days) to set {wk, dy} to {its weekday as integer, its day}
	if wk > 2 then
		set folderName to text -2 thru -1 of ("0" & dy) & space & m & space & item wk of theWeekdays
		try
			tell application "Finder" to set dateFolder to make new folder at destinationFolder with properties {name:folderName}
			tell application "Finder" to set SubDestination to make new folder at dateFolder with properties {name:"ADS"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"LIVE"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"FINISHED"}
			tell application "Finder" to set SubDestination to make new folder at dateFolder with properties {name:"PAGES"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"LIVE"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"FINISHED"}
			tell application "Finder" to set SubDestination to make new folder at dateFolder with properties {name:"PHOTOS"}
			tell application "Finder" to set SubDestination to make new folder at dateFolder with properties {name:"PLAN"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"LAYOUT PLAN"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"PRESS ORDERS"}
			tell application "Finder" to make new folder at SubDestination with properties {name:"REPORTS"}
		end try
	end if
end repeat

with


set POSIXdestinationFolder to quoted form of POSIX path of destinationFolder
repeat with oneDay from 0 to lastDate - 1
	tell (thisMonth + oneDay * days) to set {wk, dy} to {its weekday as integer, its day}
	if wk > 2 then
		set folderName to text -2 thru -1 of ("0" & dy) & space & m & space & item wk of theWeekdays
		do shell script "/bin/mkdir -p " & POSIXdestinationFolder & ¬
			quoted form of folderName & "/{ADS/{FINISHED,LIVE},PAGES/{FINISHED,LIVE},PHOTOS,PLAN/{'LAYOUT PLAN','PRESS ORDERS',REPORTS}}"
	end if
end repeat

Thank you! I completely forgot I had looked into this before. Your original script worked great until i mucked it up. I tried to modify it to only create folders for days that we publish (tue-sat) but since i don’t know anything about the shell scripts i mucked it up bad and got frustrated and gave up. I am going to repost a request under that, could you help with that Stefan?

Basically I took Nigel’s version and added the weekday filter and the subfolder creation.
I think, it’s pretty fast


property theMonths : {" JAN", " FEB", " MAR", " APR", " MAY", " JUN", " JUL", " AUG", " SEP", " OCT", " NOV", " DEC"}
property monthLengths : {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
property theWeekdays : {" SAT", " SUN", " MON", " TUE", " WED", " THU", " FRI"}

(* Choose destination folder for script to create folders inside of *)
set destinationFolder to choose folder with prompt "Choose destination folder"

(* Set a reference date to the 1st January this year. *)
tell (current date) to set {its day, its month, referenceDate, thisYear} to {1, January, it, its year}

(* Make a list of the years from 5 before this year to 5 after. *)
set yearList to {}
repeat with i from thisYear - 5 to thisYear + 5
	set end of yearList to i
end repeat

(* set variable choosenYear to year picked from menu and get the weekday of 1st January in that year. *)
set chosenYear to (choose from list yearList with prompt "choose a year")
if (chosenYear is false) then return
set year of referenceDate to chosenYear
set currentWeekDay to referenceDate's weekday as integer

(* Create a hierarchy path structure for 'mkdir'. *)
set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to ","
set monthList to {}
repeat with aMonth from 1 to 12
	set dayList to {}
	repeat with aDay from 1 to (item aMonth of monthLengths) + (((aMonth is 2) and (chosenYear mod 4 is 0) and ((chosenYear mod 100 > 0) or (chosenYear mod 400 is 0))) as integer)
		set currentWeekDayString to item ((currentWeekDay mod 7) + 1) of theWeekdays
		if currentWeekDayString is not in {"SUN", "MON"} then
			set end of dayList to quoted form of (text -2 thru -1 of (100 + aDay as string) & currentWeekDayString)
		end if
		set currentWeekDay to currentWeekDay + 1
	end repeat
	set end of monthList to quoted form of (text -2 thru -1 of (100 + aMonth as string) & item aMonth of theMonths) & "/{" & dayList & "}"
end repeat
set hierarchy to quoted form of (POSIX path of destinationFolder) & chosenYear & "/{" & monthList & "}/{ADS/{FINISHED,LIVE},PAGES/{FINISHED,LIVE},PHOTOS,PLAN/{'LAYOUT PLAN','PRESS ORDERS',REPORTS}}"
set AppleScript's text item delimiters to astid

(* Create the entire hierarchy of folders. *)
do shell script "/bin/mkdir -p " & hierarchy

How would you eliminate sun and mon from day folder portion of the script? that is were i end up making the script go fubar? We only want folders for tue, wed, thur, fri, and sat. And I’m not accomplished enough to figure it out. :frowning:

Sorry, I didn’t notice that the entries in the weekday list have a leading space character

just add those spaces in this line


.
if currentWeekDayString is not in {" SUN", " MON"} then
.