|
|
#1 |
|
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
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. |
|
|
|
|
|
#2 |
|
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.
__________________
~Jayson <www.kempinger.us> Last edited by GlowingApple; 12-08-2004 at 04:02 AM. Reason: fixed error in initializing filenames variable |
|
|
|
|
|
#3 |
|
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) Last edited by spakers; 01-09-2005 at 09:30 PM. |
|
|
|
|
|
#4 |
|
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> |
|
|
|
|
|
#5 |
|
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) Last edited by spakers; 01-09-2005 at 11:16 PM. |
|
|
|
|
|
#6 |
|
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. |
|
|
|
|
|
#7 |
|
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
|
|
|
|
|
|
#8 |
|
Prospect
Join Date: May 2005
Posts: 2
|
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.
|
|
|
|
|
|
#9 | |||||||||||||||||||||||
|
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.
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> |
|||||||||||||||||||||||
|
|
|
|
|
#10 |
|
Prospect
Join Date: May 2005
Posts: 2
|
Thx, let me know.
|
|
|
|
|
|
#11 |
|
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:-)
|
|
|
|
|
|
#12 |
|
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. |
|
|
|
|
|
#13 |
|
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 HTH
|
|
|
|
|
|
#14 |
|
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 |
|
|
|
|
|
#15 | |||||||||||||||||||||||
|
Prospect
Join Date: Apr 2004
Posts: 13
|
avoid premature Mail shutdown
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 |
|||||||||||||||||||||||
|
|
|
|
|
#16 |
|
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. |
|
|
|
|
|
#17 |
|
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. |
|
|
|
|
|
#18 | |||||||||||||||||||
|
Major Leaguer
Join Date: Apr 2005
Posts: 270
|
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. |
|||||||||||||||||||
|
|
|
|
|
#19 |
|
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.
|
|
|
|
|
|
#20 | |||||||||||||||||||||||
|
Prospect
Join Date: May 2005
Posts: 3
|
Thanks - did here too |
|||||||||||||||||||||||
|
|
|
![]() |
|
|