Applescript Sub-menus in Address Book (OSX10.5)

Hey,
This is great site and forum resource for scripts.

I’m trying to (and have been at this tediously for many hours) simply write a script to backup my address book data. I tried many reference links and even downloaded apple’s “UIElementInspector” to try to get this to work. Here’s what I’ve got so far but it isn’t working" SCript editor always returns that it can’t find the “Address Book Archive…” menu item (I even tried access sub-menus of import and could get that to work either.


--BU Address Book Database
tell application "Address Book"
	activate
end tell
tell application "System Events"
	
	tell process "Address Book"
		tell menu bar 1
			tell menu bar item "File"
				tell menu "File"
					tell menu item "Export"
						tell menu "Export"
							tell menu item "Address Book Archive..."
								click menu item "Address Book Archive..."
							end tell
						end tell
					end tell
				end tell
			end tell
		end tell
	end tell
end tell

Many thanks!

Give this a try

activate application "Address Book"
tell application "System Events"
	tell process "Address Book"
		click menu item "Address Book Archive." of menu 1 of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1
		click button "Save" of sheet 1 of window "Address Book"
	end tell
end tell

One thing to note is that the “Address Book Archive.” is actually using Periods of Ellipsis and not simply three periods.

James, thank a million man. I used to live in chicago. Now i’m all California-based (the technology --apple! --is out here, man!!). Anyways.

  1. Okay, Ellipsis Periods versus Periods?? What’s the difference for applescript and how would I enter periods of ellipsis if that was my problem.

  2. Where did you get the idea to substitute “export” and “file” menu item and menu bar item for for menu1. Is that a commonly used apple variable for UI menus? Thanks, I’ll give it a go.

Brilliant, James. That totally worked! So massively helpful, man. I was scanning menues with the UIElementInspector trying to figure out what was going on, like crazy. Your contribution is great appreciated, but you need

tell application “Address Book”
activate
end tell

before the normal sequence or else you get a can’t access “sheet 1” error. What is sheet 1? Also, how did you solve this? Can you substitute menu item names for numbers, like Export = menu item 9? I tried that it didn’t work.

Where’d you get access to the “click button” and “sheet” (1) library terminology. New to me but that will have a high utility ifor my scripting as it will replace the clunky “keystroke return” command I had used previously!

That’s so awesome that I can just post up some code I’ve been working on and have a problem with and someone instantly responds for free and it’s accurate and “slimmer” than my original code. Awesome world of internet bbses!

how would you set up an if statement for a “replace file” scenario? Well for starters how about just adding the replace existing file feature. My pseudo code thinks it would be something like (again I’m new to this sheet thing):

	click button "Replace" of sheet 1 of window "Address Book"

after

activate application "Address Book"
tell application "System Events"
   tell process "Address Book"
       click menu item "Address Book Archive." of menu 1 of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1
       click button "Save" of sheet 1 of window "Address Book"

But it can’t access the button “Replace” .

	click button "Replace" of sheet 2 of window "Address Book"

didn’t worki either.

Thanks! Atleast I’ve got the feature working now!

I debugged my original code and found that it WAS the ellipsis that caused all those naughty problems! So I’ve posted a lot of questions here. The main one is how do you type an ellipsis instead of three periods (other than cutting and pasting)?? Thanks!

Hi,

try this


activate application "Address Book"
tell application "System Events"
	tell process "Address Book"
		click menu item "Address Book Archive." of menu 1 of menu item "Export" of menu 1 of menu bar item "File" of menu bar 1
		repeat until exists sheet 1 of window "Address Book"
			delay 0.5
		end repeat
		tell window "Address Book"
			click button "Save" of sheet 1
			repeat
				if (count sheets) = 0 then
					exit repeat
				else if exists sheet 1 of sheet 1 then
					click button "Replace" of sheet 1 of sheet 1
					exit repeat
				end if
				delay 0.5
			end repeat
		end tell
	end tell
end tell

wow, that’s really clean code. Thanks a ton. Great utilization of the ifs and loops (repeats)!

Out of curisoity. Is there anyway to code this section without the use of repeats? If I wanted to "do nothing’ after the first if statement condition was met, how would I do that?


      repeat
               if (count sheets) = 0 then
                   exit repeat
               else if exists sheet 1 of sheet 1 then
                   click button "Replace" of sheet 1 of sheet 1
                   exit repeat
               end if
               delay 0.5
           end repeat

So much gratitude. Thanks.

I have no idea, what you’re going to accomplish.

The script presses the “Replace” button if the file already exists,
otherwise it exits the loop immediately after the (first) sheet vanishes

nm,


if exists sheet 1 of sheet 1 of window "Address Book" then
			click button "Replace" of sheet 1 of sheet 1 of window "Address Book"
		end if

did the trick. Thanks!

Wow I left the party here for a while didn’t I? LOL Will process after lunch and reply with any of the random thoughts I may have.