Refining my backup script

Hi

I’m a relative newcomer to the world of AppleScript, and I can’t figure this out despite lots of searching…so I’m hoping that someone can help me.

I’m trying to refine my backup script which is triggered by a script that runs on startup. If this script detects that it’s a Tuesday (for example), it executes another script which actually does the backup. The problem is, if I start the computer for a second time on the same Tuesday, it backup script attempts to run again - however the backup has obviously already been created. Here’s my attempt to get the script to check to see if the backup folder has already been created - and if so, to ask if it should be run again.

The problem is that when the script detects that the backup folder already exists, it doesn’t present the ‘run again?’ dialog box - it just says that there’s an error because the file already exists.

Any one know how to fix this?

Thanks!

Matthew

– Get system date and convert it to mm–dd–yy format –
set todaysDate to (current date)
set {d, m, y} to {day, month, year} of todaysDate
set monthList to {January, February, March, April, May, June, ¬
July, August, September, October, November, December}

repeat with i from 1 to 12
if m = (item i of monthList) then
set monthString to text -2 thru -1 of (“0” & i)
exit repeat
end if
end repeat

set dayString to text -2 thru -1 of (“0” & d)
set todaysDate to dayString & “/” & monthString & “/” & y
– end date procedure –

set backupfolder to “BackupTest-” & todaysDate

– Create a backup folder on the Desktop –
tell application “Finder”
activate
make new Finder window to disk “Backup”
set target of Finder window 1 to folder “Scripted Backups” of disk “Backup” --opens window of folder where backup will be/is
if not (folder (backupfolder) exists) then
make new folder at folder “Scripted Backups” of disk “Backup” with properties {name:backupfolder} --if the backup hasn’t already been actioned, the script makes a new backup folder with date string attached at backup location

	set target of Finder window 1 to folder "Scripted Backups" of disk "Backup"
else if (folder (backupfolder) exists) then --PROBLEM HERE!! This should check to see if the backup has already been run and if it has, it will discover that the backup folder with date string already exists. But it's not working - I just get an error saying 'an item with that name already exists..."
	display dialog "The backup for today has already been actioned. Backing up again will overwrite files. Continue anyway?" buttons {"Yes", "No"} default button 2
	set the button_pressed to the button returned of the result
	if the button_pressed is "No" then display dialog "Ok, thanks." & (beep)
	if the button_pressed is "Yes" then tell application "Finder"
		delete folder (backupfolder) of folder "Scripted Backups" of disk "Backup"
		open file "TV instructions.doc" of desktop --this is just a trigger to illustrate to me that the script is working. When functioning correctly, this line will activate a separate backup script.
	end tell
end if

end tell

Model: iMac G5 2Ghz
AppleScript: 1.10.3
Browser: Safari 417.8
Operating System: Mac OS X (10.4)

The problem here is that Finder is looking in the wrong place for the backup folder, Matt.

It may seem initially confusing but, since Finder hasn’t been told where to check for the backup folder, it’s actually looking for it on the desktop. (So, unless there’s a folder of the same name on the desktop, Finder will always think the backup doesn’t exist - and then try to create a new one in the correct location.)

While opening and targeting windows may help us to see what’s going on, Finder doesn’t use them in the same way we do. In fact, Finder doesn’t need any windows open at all (or even to be activated) to do this kind of stuff.

To demonstrate the principle, try playing with this variation:

tell (current date) to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
	to set backupfolder to "BackupTest-" & text 2 thru 3 & "/" & text 4 thru 5 & "/" & text -4 thru -1

tell application "Finder" to tell folder "Scripted Backups" of disk "Backup"
	tell folder backupfolder to if exists then
		tell me to display dialog "The backup for today has already been actioned. Backing up again will overwrite files. " & ¬
			"Continue anyway?" default button 1 (* if the "Cancel" button is clicked, the script will stop here *)
		delete
	end if
	make new folder at it with properties {name:backupfolder}
	(* continue with backup script *)
end tell

Kai;

The first part of your script makes a nice little date adder for all kinds of log files or backups that is sortable in time:

to AddDateTo(aName)
	tell (current date) to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
		to return aName & "-" & text -2 thru -1 & "." & text 4 thru 5 & "." & text 2 thru 3
end AddDateTo

AddDateTo("Starlog") --> "Starlog-06.04.05"

And if you’re sure the answer will reside on an Apple Computer, I like this version:

to AddDateTo(aName)
	set apple to ASCII character 240
	tell (current date) to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
		to return aName & apple & text -2 thru -1 & "." & text 4 thru 5 & "." & text 2 thru 3
end AddDateTo

AddDateTo("Starlog") --> "Starlog06.04.05"

Hi Kai & Adam

Thanks for your help. I will tinker with the scripts you supplied, and will let you know how I get on. Thanks for your help!

Matthew

Ok - I tried the script suggested by Kai (much more concise than my one!) and it worked well. However…there’s still a problem - for me at least! I want to be able to begin copying files to the newly created backup folder (ie the one with the date string attached) but can’t seem to get the files to go in there!

Here’s what I added:
tell (current date) to tell 100000000 + day * 1000000 + (its month) * 10000 + year as string ¬
to set backupfolder to “BackupTest-” & text 2 thru 3 & “/” & text 4 thru 5 & “/” & text -4 thru -1

tell application “Finder” to tell folder “Scripted Backups” of disk “Backup”
tell folder backupfolder to if exists then
tell me to display dialog "The backup for today has already been actioned. Backing up again will overwrite files. " & ¬
“Continue anyway?” default button 1 (* if the “Cancel” button is clicked, the script will stop here *)
delete
end if
make new folder at it with properties {name:backupfolder}
duplicate file “SystemX:test.doc” to backupfolder of folder “Scripted Backups” of disk “Backup”

end tell

I also tried

duplicate file “SystemX:test.doc” to backupfolder

but this didn’t work either. How can I fix this?

Matthew