error -128 before any button is pressed

I may be suffering from the bug reported in http://macscripter.net/viewtopic.php?id=24563
and also experienced by another user in http://macscripter.net/viewtopic.php?id=23364

I have AppleScript Editor 2.3,
AppleScriptRunner 1.0.2
AppleScript Utility 1.1.1
running on OS X 10.6.4

My script tries to get Address Book (5.0.2) to take a backup, using the Export command.
It runs fine, if the default output file name doesn’t exist.
But if that file name already exists, the first dialog box (Save As:) opens a second dialog box (.already exists. Do you want to replace it?)
and before I press any button, the AppleScript Editor results window shows that error -128 has already been triggered.
(“Address Book got an error: User canceled.”)
The error seems untrappable.

I’m new at scripting, and would welcome guidance on the approach I should take.

Must I upgrade software? I really don’t want to upgrade OS X because in my experience the most important applications will then have problems.
Is there a way to upgrade AppleScript without upgrading OS X?

Or should I attempt to programmatically create a unique file name and feed it to the Save As dialog box?
In that case I might be able to delete the file before starting, as a precaution.

Or have I simply done something wrong in my code?

Here’s the full script:

– Back Up Address Book to default location
tell application “Address Book”
activate
tell application “System Events”
tell process “Address Book”
tell menu bar 1
tell menu bar item “File”
tell menu “File”
tell menu item “Export.”
try
tell menu “Export.”
try
click menu item “Address Book Archive.”
on error number -128
display dialog “Bug number 1 being bypassed.”
end try

try
– The following “keystroke return” stmt
– goes into the where-to-save-file dialog.
keystroke return
on error number -128
display dialog “Bug number 2 being bypassed”
end try
end tell
on error number -128
display dialog “Bug number 3 being bypassed”
end try
end tell
end tell
end tell
end tell
end tell
end tell
quit
end tell

May thanks.

Hi, composer. Welcome to MacScripter.

The problem’s coming from the ‘quit’ command right at the end of the script!

The GUI Scripting stuff before that merely tells System Events to do clicks in Address Book’s GUI. It’s scripting by proxy. It doesn’t provide any feedback from Address Book to the script.

So once System Events has acknowledged doing the clicks, the script ” without knowing what’s happened as a result of those clicks ” is going on to tell Address Book directly to quit. It’s the conflict between this and Address Book trying to deal with the existing-file problem which is causing the error, although I don’t know why it’s a “User canceled” error particularly.

The simplest cure to omit the ‘quit’. If you want Address Book to quit at the end of the process, it’ll involve more complex stuff in the GUI code to decide when it’s time to do that. I’ll try to think about that this afternoon.

Meanwhile, two points not related to your problem:

  1. It’s not usually a good idea to nest ‘tell’ blocks to different applications. They should be kept separate where possible:
-- Back Up Address Book to default location
tell application "Address Book"
	activate
end tell
tell application "System Events"
	tell process "Address Book"
		-- [snip]
	end tell
end tell
tell application "Address Book"
	--	quit
end tell

  1. There are special tags on this forum which allow script code to appear as I’ve posted above. They’re [applescript] and [/applescript] and the script code should go between them. You can either write them into your posts yourself or there’s a button just above the posting window which will enter them for you.

That’s been extremely useful, Nigel. Thanks!

Let’s see whether I’ve correctly learned your second point about including code in postings:


-- Back Up Address Book to default location
--
tell application "Address Book"
	activate
end tell
--
tell application "System Events" to 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
	-- At this point a dialog box will open, asking where to put the archive.
	-- The following "keystroke return" stmt accepts the default location and filename.
	keystroke return
	-- If the file already exists, we get a cancel/replace dialog.
end tell

The script is now functional, although I need to learn how to make it neater. Do I gather from your first point that I could tell Address Book directly what to do, rather than pass GUI actions via System Events? I think I read that AppleScript is object-oriented, so maybe I should learn the Address Book DOM and how to talk to it?

The lyfe so short, the craft so long to lerne – Chaucer

Here we go, assuming the user’s language is English:

-- Back Up Address Book to default location
tell application "Address Book" to activate

tell application "System Events"
	tell process "Address Book"
		click menu item "Address Book Archive." of menu "Export." of menu item "Export." of menu "File" of menu bar item "File" of menu bar 1
		tell sheet 1 of window 1
			-- Wait for the drop-down sheet to appear.
			repeat until (it exists)
				delay 0.2
			end repeat
			-- Click the drop-down sheet's "Save" button.
			click button "Save"
			-- Wait for the sheet either to disappear or to sprout a new sheet of its own.
			repeat until not (it exists) or (sheet 1 exists)
				delay 0.2
			end repeat
			-- If there's a new sheet, click the "Replace" button and wait for the original sheet to disappear.
			if (sheet 1 exists) then
				click button "Replace" of sheet 1
				repeat while (it exists)
					delay 0.2
				end repeat
			end if
		end tell
	end tell
end tell

-- It should now be safe to tell Address Book to quit.
tell application "Address Book" to quit

PS. I’ve just seen your second post.

Address Book does have its own “dictionary” of commands and objects which can be used in an AppleScript script. If you open the “Library” window in AppleScript Editor, Address Book should be listed there. If you double-click the entry, it’ll open an initially confusing window showing the specialised keywords and syntax for Address Book’s own AppleScript implementation. Unfortunately, this doesn’t include exporting an archive, so GUI Scripting is the only way for the moment.

Nigel, you’ve been most helpful, not only writing code for me but also “teaching me how to fish”. I extended the code to backup iCal in the same way (its object model also lacks the Export command).

Many thanks.