an applescript to automatically backup address book

Hi,

I am trying to write an automator workflow to run the “Back up Address Book” command in address book (I was planning to just write an automator scipt, and have iCal run it every morning), but I seem to have a small problem:

So in the automator workflow, I first run the 'Launch Application" action, and select Address Book. Then I selected the “Run Applescript” Action, and I used the following script:

on run {input, parameters}

tell application "System Events"
	tell process "Address Book"
		tell menu bar 1
			click menu item "Back up Address Book..." of menu "File"
		end tell
	end tell
end tell

return input

end run

I am able to ‘close’ the window, run the ‘minimize’ command from the window menu, and do other little things, but for some reason, the ‘Back up Address Book’ command does not work. Please help!

A couple of things, vgoklani…

Firstly, it’s normally necessary to push a process to the front to access its UI elements.

Secondly, applications don’t seem to be consistent about how an ellipsis is ‘spelled’ in their menus. Some simply use 3 periods (ASCII character 46). Others (Address Book included) opt for the more correct (IMO) single-character form (ASCII character 201). On my keyboard, this is entered by pressing option + “;”.

On that basis, you might like to try this:

tell application "System Events" to tell process "Address Book"
	set frontmost to true
	click menu item "Back up Address Book." of menu "File" of menu bar 1
end tell

From there, you’ll still need to enter a filename and location - but come back on that if you’re not sure how…

I now see that this subject was also raised here: http://bbs.applescript.net/viewtopic.php?pid=43571#p43571

(It’s not generally considered good form to crosspost in this way, since folks can waste time dealing with a question that’s already been answered elsewhere…) :confused:

Just to note to inform you that, on the system level, AddressBook does some off its own backup admin that goes beyond my exact comprehension (and Apples’ rudimentary explanation)

Take a closer look into ~/Library/Application Support/AddressBook.
It may be useful to backup these files (to some safe place) BEFORE running its standard backup procedure.
Once, this multi-level archival may save your day if your contact data appears corrupt/contaminated.

Other tip: it will never hurt to drag all your vCards into a separate folder from time to time.
Preventing you from being locked in into Trusted Computing
(see http://www.boingboing.net/2005/07/31/apple_to_add_trusted.html
on a very, very bad day.

Hi,

Sorry for the multiple post, I was under the impression that some people only check certain forums.

I resolved the issue with the ellipsis, but now the script runs, and the address book is waiting for me to press enter (to confirm the filename and location). As I want this to be an automated process (something that will hopefully run in the background everyday), is there any way for me to code a hard ‘return’ so that it automatically forces it to write the file without a confirmation. And if the script runs twice during the same day, I would like to also incorporate the hard ‘return’ for the confirmation to write the file.

Also, I would like to learn more about applescript - is there a book you could recommend - or a really good site with a few nice tutorials (and sample code).

Thanks!

-V-

I obviously can’t speak for everyone, but I get the impression that quite a few folk browse through several forums. After all, I guess we’re all pretty much interested in similar subjects - and there’s an element of crossover between each. :slight_smile:

Well, you could try continuing with UI scripting to click on the “Save” button. Don’t forget that, if a backup with the same name already exists in a given location, you’ll get a further dialog asking if you want to replace the original. So you may need yet more UI skulduggery to dismiss that, too.

This seems to work OK here (although the second dialog window may take a while to disappear):

tell application "Address Book" to activate
tell application "System Events" to tell process "Address Book"
	click menu item "Back up Address Book." of menu "File" of menu bar 1
	click button "Save" of sheet 1 of window 1
	tell (first window whose description is "dialog") to if exists then click button "Replace"
end tell

Curiously, I’ve somehow neglected to read any books on the subject, although I’ve heard good reports about several that are currently available. Perhaps someone who has a particular favourite can chip in here with a personal recommendation…

In the meantime, you might like to check out a few links:

http://akappleug.org/rev/443.html
http://www.spiderworks.com/books/ashandbook.php

http://www.applescriptsourcebook.com/links/books.html

There’s also the AppleScript Language Guide, from Apple. It’s now a fairly old document, and doesn’t get anywhere near the OS X stuff, such as shell scripts and the like - but I still find it a handy reference to have around at times:

http://developer.apple.com/documentation/AppleScript/Conceptual/AppleScriptLangGuide/

   tell (first window whose description is "dialog") to if exists then click button "Replace"

No worky here → does not recognize the “Replace” button
All descriptions return “window” - whether wds are regular, dialog or other.

OSX 10.3.9 Int’l

Bummer. The distinction between window types is obviously a Tiger thang. (Bit of a double whammy with no dialog identification and no “Replace” button recognition - one or t’other would have offered a workaround.)

Hey ho - time to try a more generic approach. This also worky here - how’s about there?

tell application "Address Book" to activate
tell application "System Events" to tell process "Address Book"
	click menu item "Back up Address Book." of menu "File" of menu bar 1
	set winCount to count windows
	click button "Save" of sheet 1 of window 1
	if (count windows) > winCount then keystroke return
end tell

I was hitting every “replace” btn everywhere, but this is certainly the more official way.

Thanks for the help Kai, sorry it took me so long to get back to you!