Go Back   The macosxhints Forums > OS X Help Requests > Applications



Reply
 
Thread Tools Rating: Thread Rating: 48 votes, 4.98 average. Display Modes
Old 11-26-2004, 05:13 AM   #1
minton
Prospect
 
Join Date: Oct 2004
Posts: 22
Mini-Hint: Automate backups to gmail using applescript

I got a gmail account a week ago and was surprised to see the notification in the upper right that I now had pop access. Now to put that 1 gb of storage to work, I decided to set up mail.app to automatically archive and backup a few of my directories. Make sure you activate pop in your gmail preferences, and optionally setup mail.app for gmail (only necessary if, like me, gmail is your only ticket into the world of pop mail).
Here's the applescript to backup the Documents and Texts folders in my home directory:

Code:
do shell script "cd ~;tar -czf Backup/`date +%Y-%m-%d`.tgz Documents Texts"
set p to do shell script "ls -t1 ~/Backup |head -1" 
set target_file to "/Users/username/Backup/" & p
tell application "Mail"
	set newMessage to make new outgoing message with properties ¬
		{subject:target_file, content:"Whatever reminder you want inside the message"}
	
	tell newMessage
		make new to recipient at beginning of to recipients with properties ¬
			{address:"user@gmail.com"}
		
		tell content
			make new attachment with properties {file name:target_file} ¬
				at after the last word of the last paragraph
		end tell
	end tell
	send newMessage
end tell
It archives and zips 2 directories under the home directory called Documents and Texts to the ~/Backup directory - note how the filename of the created archive includes the date. The "ls ... | head" command is just a way to get the last modified file in the directory, which would be the zipped archive we just created. (The unix part of the script is inelegant and messy). It then makes a new email message with the archive as an attachment and sends it to your gmail account. Save the script as an application and you just need to double click it any time you want to backup your directories.

Obviously, to make this work as an online backup system, you'll have to select the "leave messages on server" option in your gmail settings. (You can always create a separate gmail account to be used solely for backups). Also, it's rumored that the attachment limit for gmail is 10megs per file, so it's probable that you'll have to modify the script to send multiple attachments if they are > 10 megs. It's a good idea to setup a filter to have your backups automatically stored in their own "folder" outside of the inbox. If you need to restore your files, just download the archive, cd to where you want to expand the archive (only cd to the original directories' parent directory - ~ in this example - if you want to overwrite the existing directories) and use "tar -zxvf /path/to/file" :
tar -zxvf /Users/karl/Desktop/2004-11-16.tgz

Last edited by minton; 11-26-2004 at 05:27 AM.
minton is offline   Reply With Quote
Old 12-08-2004, 03:35 AM   #2
GlowingApple
Major Leaguer
 
Join Date: Feb 2004
Location: Lincoln, NE
Posts: 280
Droplet

Wow, great hint. I love it, thanks! So here's a version of the same script but modified to allow for dropping of files.
Attached Files
File Type: zip G-mail Backup.scpt.zip (4.4 KB, 2882 views)
__________________
~Jayson <www.kempinger.us>

Last edited by GlowingApple; 12-08-2004 at 04:02 AM. Reason: fixed error in initializing filenames variable
GlowingApple is offline   Reply With Quote
Old 01-09-2005, 04:17 PM   #3
spakers
Prospect
 
Join Date: Dec 2004
Posts: 21
G-Mail backup script question

good thinking- would this script work if i substituted "entourage" for "mail"- (don't even wan't to look at apple mail if i don't have to-too many bad memories)
I think I am gonna to make a folder action using this script- gonna attach it to this fancy icon I scrapped and put it on my Desktop-
- A make-shift G-mail drive for mac OS X!!! - (now if i could just make a "retrieve contents of drafts folder script)
Attached Files
File Type: zip Gmail HD.dmg.zip (42.0 KB, 3716 views)

Last edited by spakers; 01-09-2005 at 09:30 PM.
spakers is offline   Reply With Quote
Old 01-09-2005, 09:27 PM   #4
GlowingApple
Major Leaguer
 
Join Date: Feb 2004
Location: Lincoln, NE
Posts: 280
I'm sure it would. You'd just have to substute the lines that compose the mail message with those that do in Entourage. The way the script works is just creates a new mail message and adds an attachment to it. I'm not sure how scriptable Entourage is (Microsoft apps have been notoriously bad at including good scripting support), and since I don't use Entourage I have no way of testing it. You could look in the library for Entourage through Script Editor to look for similar commands in Entourage and just use the same variables.
__________________
~Jayson <www.kempinger.us>
GlowingApple is offline   Reply With Quote
Old 01-09-2005, 09:32 PM   #5
spakers
Prospect
 
Join Date: Dec 2004
Posts: 21
Danke

Worked pretty good after some adjustments- didn't try to configure for entourage, just made it so I didn't have to look at mail (made it quit when it was done)- for small files I barely had to see it at all!





Wait there is more! - THE GMAIL DRIVE FOR OS X is here! (sorta)
Attached Files
File Type: zip Gmail Drive (OS X).dmg.zip (52.6 KB, 11007 views)

Last edited by spakers; 01-09-2005 at 11:16 PM.
spakers is offline   Reply With Quote
Old 02-09-2005, 04:16 PM   #6
MBHockey
MVP
 
Join Date: Sep 2003
Location: New York
Posts: 2,211
Great! THanks so much!

Last edited by MBHockey; 02-09-2005 at 04:18 PM.
MBHockey is offline   Reply With Quote
Old 04-17-2005, 10:25 PM   #7
davidw
Major Leaguer
 
Join Date: Apr 2005
Posts: 270
I just made a few modifications for myself, and now its awsome.

1)
When I double click it dosent ask me if I want to launch Gmail and it goes directly to the label in which I store all my data.
I did this by control clicking my label that they are sent to and hiting open in new window. I copied that link and put it in the open location part of the script
2)
I added support to a prefix on the file. So when I drag a file over to it it asks for the title.
Then adds it to the front of the subject
Code:
tell application "Safari"
	open location "http://gmail.google.com/gmail"
end tell



property username : "" --gmail username
property newline : (ASCII character 10) --character for new line

on open of these_items
	--calls send_message with the list of files gotten as a droplet
	send_message(these_items)
end open

--accepts a list of files
on send_message(target_files)
	set theResult to display dialog "Title:" default answer ""
	set theSubject to text returned of theResult
	tell application "Mail"
		
		--combine filenames of all files into one string for subject
		set n to 1
		set filenames to ""
		repeat until n is the ((length of target_files) + 1)
			set n_file to item n of target_files
			if n is less than (length of target_files) then
				set filenames to filenames & POSIX path of n_file & ", "
			else
				set filenames to filenames & POSIX path of n_file
			end if
			set n to n + 1
		end repeat
		
		--set message properties
		set newMessage to make new outgoing message with properties ¬
			{subject:theSubject & ": " & filenames, content:newline}
		
		tell newMessage
			--message should be sent to self
			make new to recipient at beginning of to recipients with properties ¬
				{address:username & "@gmail.com"}
			
			--add attachments from the list		
			set n to 1
			tell content
				repeat until n is the ((length of target_files) + 1)
					set n_file to item n of target_files
					set filepath to POSIX path of n_file
					make new attachment with properties {file name:filepath} ¬
						at after the last word of the last paragraph
					set n to n + 1
				end repeat
			end tell
		end tell
		
		--send the message
		send newMessage
		
	end tell
end send_message
davidw is offline   Reply With Quote
Old 05-14-2005, 02:17 PM   #8
RTD
Prospect
 
Join Date: May 2005
Posts: 2
Unhappy Tigered, and...

hmmm, I´m a newbie and not familiar with scripting,
I love to use this script, but I have problems to work with it.
Sometimes it works, sometimes not.
I´m running now TIGER and mail and maybe it has to do with that.
By dragging a file to the script an attachment would be applied to a new mail and mail will be instantly quit. Thats the normal way. But sometimes only mail will be opened and closed but nothing happens.
Is that right that i have to open mail after scripting the files to send everything?

Thx for reply.
RTD is offline   Reply With Quote
Old 05-20-2005, 04:07 PM   #9
GlowingApple
Major Leaguer
 
Join Date: Feb 2004
Location: Lincoln, NE
Posts: 280
I've been getting some comments and ideas from a few people and have some ideas of my own to work in. Now that school is over for the summer I will have to play around with the code some more and see what I can do.


Quote:
Originally Posted by RTD
I´m running now TIGER and mail and maybe it has to do with that.
By dragging a file to the script an attachment would be applied to a new mail and mail will be instantly quit. Thats the normal way. But sometimes only mail will be opened and closed but nothing happens.
Is that right that i have to open mail after scripting the files to send everything?

I have Tiger and will be testing the script on Tiger, so I'll let you know what I find. The script should open Mail.app for you before it sends, but there could be some scripting changes for Mail.app in Tiger.
__________________
~Jayson <www.kempinger.us>
GlowingApple is offline   Reply With Quote
Old 05-21-2005, 05:24 AM   #10
RTD
Prospect
 
Join Date: May 2005
Posts: 2
Thx, let me know.
RTD is offline   Reply With Quote
Old 05-26-2005, 09:16 AM   #11
richardyates
Prospect
 
Join Date: May 2005
Posts: 3
The only problem with the script that I'm having using Tiger is that it causes mail to quit before some of the messages have had time to send. I don't know anything about scripting (I've had my mac less than a month), but if it could be rewritten so that mail checked to make sure all messages were sent before closing, it would make it just about perfect - its already very good:-)
richardyates is offline   Reply With Quote
Old 05-28-2005, 04:08 PM   #12
davidw
Major Leaguer
 
Join Date: Apr 2005
Posts: 270
yea, im on 10.3.8
and it quits before it has time to send
I looked through the mail dictionary and found nothing that would return if it is done sending or not.
davidw is offline   Reply With Quote
Old 06-01-2005, 11:58 AM   #13
Maddoktor2
Registered User
 
Join Date: Jun 2005
Posts: 1
The premature Mail shutdown is easily fixed by removing this line of code at the end of the script:
Code:
tell application "Mail" to quit
The downside is you will now have to shut Mail down manually.

HTH
Maddoktor2 is offline   Reply With Quote
Old 06-01-2005, 01:14 PM   #14
richardyates
Prospect
 
Join Date: May 2005
Posts: 3
Thanks - that's exactly what I did, it would just be nice to have it do it automatically.

Only other problem I've had with it is that the script keeps forgetting the gmail drive icon and reverting to a script icon - hardly the end of the world although it woiuld be nice to fix it once and for all

Thanks for the help, lots of what I've read on here has helped me in my move from XP
richardyates is offline   Reply With Quote
Old 06-02-2005, 01:43 PM   #15
powderhound
Prospect
 
Join Date: Apr 2004
Posts: 13
avoid premature Mail shutdown

Quote:
Originally Posted by Maddoktor2
The premature Mail shutdown is easily fixed by removing this line of code at the end of the script:
Code:
tell application "Mail" to quit
The downside is you will now have to shut Mail down manually.

HTH

You can avoid the premature quitting of Mail by adding a line that tells the script to delay for a certain time (number of seconds). It works with a delay of only 1 second, even for multiple files. The end of the script should therefore be:

end tell
delay 1
tell application "Mail" to quit
end send_message
powderhound is offline   Reply With Quote
Old 06-04-2005, 01:08 PM   #16
powderhound
Prospect
 
Join Date: Apr 2004
Posts: 13
An important addendum to my fix for premature quitting:

Depending upon the speed of your internet connection, you may have to increase the delay. For example, at work, I need only 1 second. However, at home (with average upload of 300-400 kbits/sec), I have increased the delay to 5 seconds.
powderhound is offline   Reply With Quote
Old 06-05-2005, 10:18 PM   #17
davidw
Major Leaguer
 
Join Date: Apr 2005
Posts: 270
use time. I never thought of that.
But what if your on slow internet at about 3 kb per second
if you could get the total size of the email in kb and divide that by your transferspeed you could get the correct delay for large and small files.
davidw is offline   Reply With Quote
Old 06-05-2005, 10:22 PM   #18
davidw
Major Leaguer
 
Join Date: Apr 2005
Posts: 270
Quote:
Only other problem I've had with it is that the script keeps forgetting the gmail drive icon and reverting to a script icon - hardly the end of the world although it woiuld be nice to fix it once and for all

it did the same thing to me.
Do this.
First copy the folder that has the gmail drive icon to somewhere on your computer. like the documents folder or something. Then copy the icon from it and paste it on the gmail drive apple script. It should stay on now... well it at least worked for me.
davidw is offline   Reply With Quote
Old 08-21-2005, 09:18 PM   #19
euskir
Prospect
 
Join Date: Sep 2002
Location: Buenos Aires, Argentina
Posts: 9
A good one.

A good one is to use Disk Utility to compress and encrypt a folder or file, then you can attach that to a "fake" mail. It's working ok for me right now.
euskir is offline   Reply With Quote
Old 08-31-2005, 10:17 AM   #20
richardyates
Prospect
 
Join Date: May 2005
Posts: 3
Smile

Quote:
Originally Posted by davidw
it did the same thing to me.
Do this.
First copy the folder that has the gmail drive icon to somewhere on your computer. like the documents folder or something. Then copy the icon from it and paste it on the gmail drive apple script. It should stay on now... well it at least worked for me.

Thanks - did here too
richardyates is offline   Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump



All times are GMT -5. The time now is 04:12 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.
Site design © IDG Consumer & SMB; individuals retain copyright of their postings
but consent to the possible use of their material in other areas of IDG Consumer & SMB.