Generating Folders

We back up our work to Dated CD’s with specific volume names based on the folder names in the Finder.

What I’m trying to do it to get a script which can generate the folder with todays date and a letter (as we always do more than one back up per day).

This is what I’ve got and what works so far (ie it generates ‘a’ and ‘b’ folders) - what I’m trying to learn is how to avoid having to put my if statements in 26 times.

tell application “Finder”
set DayVar to day of (current date)
set MonthVar to month of (current date) as number
set FullYearVar to year of (current date) as text
set YearVar to text -2 thru -1 of (FullYearVar)
set FolderStartVar to (“Work Back Up - " & DayVar & “-” & MonthVar & “-” & YearVar)
set FoldervarA to (FolderStartVar & " (a)”)
if folder FoldervarA exists then
make new folder with properties {name:FolderStartVar & " (b)"}
else
make new folder at desktop with properties {name:FoldervarA}
end if
end tell

If anyone can point me in the right direction or offer and advise I’d be grateful as I not that sure where to start!

Try this: Edit: Removed a couple extra spaces


property alphabet : {"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"}
property counter : 1
property lastDay : 0

set today to (day of (current date))

if today ≠ lastDay then
	set counter to 1
	set lastDay to today
end if

set theDate to today & "-" & (month of (current date) as number) & ¬
	"-" & (text -2 thru -1 of (year of (current date) as text))
set folderName to ("Work Back Up - " & theDate & ¬
	" (" & item counter of alphabet & ")")

try
	tell application "Finder" to make new folder at desktop ¬
		with properties {name:folderName}
	if counter < 26 then
		set counter to counter + 1
	else
		display dialog "All the letters have been used." buttons ¬
			"OK" default button 1 with title "Backup Script"
	end if
on error errorMsg
	display dialog errorMsg buttons ¬
		"Cancel" default button 1 with title "Backup Script"
end try

Edit: As a side note, you can also get “theDate” like this:


set theDate to (do shell script "date +%d-%m-%y")

Model: Mac mini
AppleScript: 1.10
Browser: Safari 412
Operating System: Mac OS X (10.4)

This is a trick trying to stay close to the “use as few lines as possible” you were trying to achieve; IF you are not adverse to using numbers instead of letters (which overcomes your 26 folder limitation). Save the following as an application, and it will be a “droplet”. Double clicking the app will produce folder after folder numbered from 1 to whatever. To reset the app back to (1), drop a folder on the icon. This won’t do anything to the folder, just reset the number of the next created folder back to (1).


property NextFold : {"1"}

on open droppeditems
	set NextFold to {"1"}
end open

on run
	tell application "Finder"
		set DayVar to day of (current date)
		set MonthVar to month of (current date) -- I took out "as number"; gave me errors
		set FullYearVar to year of (current date) as text
		set YearVar to text -2 thru -1 of (FullYearVar)
		set FolderStartID to ("Work Back Up - " & DayVar & "-" & MonthVar & "-" & YearVar)
		make new folder at desktop with properties {name:FolderStartID & " (" & NextFold & ")"}
		set NextFold to NextFold + 1
	end tell
end run

SC

If you’re certain it won’t go beyond 27 backups then this will work:

set cd to (current date)
set m to month of cd as integer
set d to day of cd
set y to text -2 through -1 of ((year of cd) as text)
set fldrStart to "Work Back Up - " & m & "-" & d & "-" & y

try
	alias ((((path to desktop) as text) & fldrStart) & ":")
	repeat with n from 97 to 122
		set tryThis to fldrStart & (ASCII character (n))
		try
			alias ((((path to desktop) as text) & tryThis) & ":")
		on error
			exit repeat
		end try
	end repeat
	set fldrStart to tryThis
end try
tell application "Finder" to make file at desktop with properties {name:fldrStart}
display dialog "new name is " & fldrStart

Otherwise this can be adapted to append aa, ab, ac, etc. a la Excel columns which I’d be happy to do…

  • Dan

Here’s a shorter version of my script (this [one] uses numbers):

property counter : 1
property lastDay : 0

if (day of (current date)) ≠ lastDay then
	set counter to 1
	set lastDay to (day of (current date))
end if

set folderName to "Work Back Up " & (do shell script "date +%d-%m-%y") & ¬
	" (" & counter & ")"

try
	tell application "Finder" to make new folder at desktop ¬
		with properties {name:folderName}
	set counter to counter + 1
on error errorMsg
	display dialog errorMsg buttons ¬
		"Cancel" default button 1 with title "Backup Script"
end try

Why is that a list?

Well, you see, its a very complicated thing to explain, um … how’s oops?
I do a lot of cut N paste when building scripts and that was left there on accident. Since I wasn’t getting any errors along with positive results, it slipped right by.
SC