Backup Script

Joined
Jan 31, 2006
Messages
10
Reaction score
0
Hi Everyone!
I hope you can help me, im trying to make a little script to backup my music, movies, documents and library to my external HDD. Ive already made an automator script to do this, which is successful, but i would like to expand my knowledge to applescript. the main problems ive experienced are targeting the external HDD to wipe the existing folders, and selecting the new folders to put on there. Im not asking for the entire script to be written for me, because that would defy the learning process! but maybe a few nudges in the right direction would be great!!
For reference my external HDD is ":Volumes:Files:"
Hope you can help! Regards - Anthony
 

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Hi there Anthony and welcome !

(you'll have to let us know whether you prefer Tony or Anthony)

Have you started your script yet, can we see what you have ?

It doesn't matter if you haven't got anything...

Don't want to give to much away, if this is a learning exercise !

regards

Ric
 
Joined
Jan 31, 2006
Messages
10
Reaction score
0
Anthony please! thanks for your prompt reply. I started with:
tell application "finder" activate
then tried to target the external HDD by putting:
delete all folders in ":volumes:files:" whose name is not "downloads"
is didnt like this and wouldnt run. then i took some of the coding out and it said that Files couldnt carry out the opperation because system was occupied... but it wasnt system that i was tring to target

regards - Anthony
 
Joined
Nov 25, 2005
Messages
47
Reaction score
1
Anthony:

You are awfully close. When you want to address a disk, you need only use the disk's name. For instance, I have an external HD named Claw. Here are the commands to address that disk and delete the folders which are not named Downloads:

set Claw_path to "Claw"--Make the name of the disk a variable.
tell application "Finder" to delete (every folder in disk Claw_path whose name does not contain "Downloads")--Tell the Finder to do something with the disk.

There is a short, free publication entitled "Applescript for Absolute Starters" at this address:

http://www.applescriptsourcebook.com/tips/AS4AS.html

It may help you to get on your way.

Good luck,

CAS
 
Joined
Jan 31, 2006
Messages
10
Reaction score
0
Thanks cas! that sounds more like it!!!
so where i was going wrong was the wording and not setting the drive as a variable..?? :eek:
thanks for the link, will be closely investigating that one!
but - cant you just taget the drive without storing it as a variable? just seems a little long-winded for its own good. thanks again!
regards - Anthony
 
Joined
Nov 25, 2005
Messages
47
Reaction score
1
Anthony:

You are correct; a variable is not necessary, but it is considered good practice, only because as you go along, you think of one more thing to add, or perhaps you change the name of the drive, or get a new drive, or whatever, then instead of changing every instance of the name of the drive, you change it once where it is placed with the variable, and you are finished.

Where you were going wrong was simply with your syntax. Although AppleScript is very english-like, there are still some ways to talk to things that have to use precise commands and phrases.

Don't worry, you'll get the hang of it.

CAS
 
Joined
Jan 31, 2006
Messages
10
Reaction score
0
very usefull stuffs there - thanks for that
ok got the basic outline working... my script so far is as follows:

tell application "Finder"
set Files_path to "Files"
set Ant_Path to folder "ant" of folder "users" of startup disk
end tell
tell application "Finder" to delete (every folder in disk Files_path whose name does not contain "downloads")
tell application "Finder"
duplicate folder "movies" of Ant_Path to Files_path
duplicate folder "pictures" of Ant_Path to Files_path
duplicate folder "music" of Ant_Path to Files_path
duplicate folder "documents" of Ant_Path to Files_path
empty trash
end tell
tell application "system events" shut down

firstly is this script correct for what im trying to do?
and secondly im getting appleevent timeout messages... ok so each folder is about 4+GB but shouldnt the program wait?
hope you can help. regards - anthony
 

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Hi there Anthony,

This is how I would possibly do it...

As a learning exercise this is how my brain works...

...as CAS said using variables is the best way to go, but whilst you are still learning it can confuse you...

Break down what you are trying to do into sections...

I like to show people this way...


Section 1

--check that the Backup drive is plugged in !
--for this test mine is called "1GBUSB"

tell application "Finder"
if exists disk "1GBUSB" then
beep 3
end if
end tell

The above script checks if the drive is there, if it is it will then do your commands in this instance it's just do 3 beeps.

I find this is the best way for someone learning to get instant feedback on whether it works !

So now that we know that works, build the next bit.


Section 2

replace "1GBUSB" with your external drive name...

tell application "Finder"
if exists disk "1GBUSB" then
beep 3

--now run the next bit

--do your delete
delete (every folder in disk "1GBUSB" whose name does not contain "downloads")

end if
end tell

It works doesn't it !!!


Now the next bit,


Section 3

We'll add in the path for your folders, again we'll not use variables this time, just so that we can see whats happening...


tell application "Finder"
if exists disk "1GBUSB" then
beep 3

--do your delete
delete (every folder in disk "1GBUSB" whose name does not contain "downloads")

--now run the next bit
duplicate folder "movies" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
--as you can see this is nice plain English and you can see exactly what is happening !
--I created an empty folder called movies for testing
--if this folder is already there it will get overwritten !

end if
end tell

Now we can add in the other folders...

Section 4

tell application "Finder"
if exists disk "1GBUSB" then
beep 3

delete (every folder in disk "1GBUSB" whose name does not contain "downloads")

duplicate folder "movies" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
duplicate folder "pictures" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
duplicate folder "music" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
duplicate folder "documents" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"

end if
end tell



Now run that...

Section 5

I presume that it will give you timeouts...

So to address timeouts we use the 'with timeout' statement...


tell application "Finder"
if exists disk "1GBUSB" then
beep 3

delete (every folder in disk "1GBUSB" whose name does not contain "downloads")

--60 seconds = 1 minute..1800 Seconds = 30 minutes amend as required...
with timeout of 1800 seconds
duplicate folder "movies" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
duplicate folder "pictures" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
duplicate folder "music" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
duplicate folder "documents" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end timeout

end if
end tell




That should now run nicely, whilst you are testing it use dummy folders with less data in !

You'll have to change the timeout depending on how much data you have in your folders...


Section 6

I would wrap the "duplicates" in a try statement then if for any reason one fails it would move onto the next one...

So the script would now look like...

tell application "Finder"
if exists disk "1GBUSB" then
beep 3

delete (every folder in disk "1GBUSB" whose name does not contain "downloads")

--60 seconds = 1 minute..1800 Seconds = 30 minutes amend as required...
with timeout of 1800 seconds

try
duplicate folder "movies" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
try
duplicate folder "pictures" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
try
duplicate folder "music" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
try
duplicate folder "documents" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
end timeout

end if
end tell



So now if the folder "pictures" was not in the folder "ant" it wouldn't stop the script, the rest of the folders would still get backed up.


That should all be working...


Section 7

Now, the empty trash.

You can add this into your script, but it may be best to leave it off till you are happy to use it.

Imagine if while working on this script you got a few names wrong...all your pictures could end up in the trash and deleted, very easily.

I've added the empty into the script, but have 'commented it out', so if you want it in just delete the --

We can also take out the beeps now if we want..

tell application "Finder"
if exists disk "1GBUSB" then

delete (every folder in disk "1GBUSB" whose name does not contain "downloads")

--60 seconds = 1 minute..1800 Seconds = 30 minutes amend as required...
with timeout of 1800 seconds

try
duplicate folder "movies" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
try
duplicate folder "pictures" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
try
duplicate folder "music" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
try
duplicate folder "documents" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
end timeout



end if

end tell


Section 8

Add in the Shut down command !

tell application "Finder"
if exists disk "1GBUSB" then

delete (every folder in disk "1GBUSB" whose name does not contain "downloads")

--60 seconds = 1 minute..1800 Seconds = 30 minutes amend as required...
with timeout of 1800 seconds

try
duplicate folder "movies" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
try
duplicate folder "pictures" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
try
duplicate folder "music" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
try
duplicate folder "documents" of folder "ant" of folder "users" of startup disk to disk "1GBUSB"
end try
end timeout



end if

end tell

tell application "System Events"
shut down
end tell

I would leave the Shut down out whilst you are testing, otherwise it will be a real pain...

hope that helps

Regards

Ric
 
Joined
Nov 25, 2005
Messages
47
Reaction score
1
Ric:

You are completely out of control!! That is some nice scripting you posted. How do you do the markup, or whatever you did with your script to make it look like it came right out Script Editor? That is very nice, and I would like to know how to do it. If you have a page with that info, just point me there, please.

There is another way around these timeout issues. I use this:

with timeout of weeks seconds

instead of trying to guess how long it is going to take for the script to run. The only problem is if there is an error that results in an eternal repeat, you have to stop the script and find the bug, since it will go on for weeks trying to complete the task(s).

Anyway, I agree with trying to break it down into sections like that, testing each section as you go, that is great advice.

CAS
 

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Hi CAS,

thanks !

I use this Convert Script to Markup code for the mark up...but am currently writing my own in Xcode specifically for vBulletin based websites, such as this. Jon's websites has lots of great stuff !

This current version doesn't work to well with this website, so I edit it a bit in TextWrangler as well...

My version is still at Alpha stage...so much to do so little time...

iMessage.jpg


I like to show people to use sections and dialog boxes or beeps...it's instant and they can see whats happening...

So when adding in a new variable etc add in a "display dialog thisIsNew " then you get to see in the script whats happening...

Purposely just put the seconds timeout in...going softly softly so as not to give to much to soon ! ;-)

regards

Ric
 
Joined
Jan 31, 2006
Messages
10
Reaction score
0
all seems well so far! :rolleyes:
thanks to you ric for some good tutoring... thanks to CAS - that timeout phrase should save some stupid timeout messages! this is a great forum, will definatelly be promoting it throughout my Mac-Force! :p
 

Ric

Joined
May 14, 2004
Messages
4,260
Reaction score
5
Thanks for your kind comments !

Promote away, we're here to help ! The more the merrier !

regards

Ric
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top