Apple Mail scriptability seems to be broken. Cannot 'get' items ScriptDebugger explorer sees

Hi.
This is a side-track to another issue I’m having, developing in Obj-C and accessing Apple Mail’s information using AX (Accessibility) APIs.

In Monterey, I completely fail to access any e-mail message attachment. Even if I just created it in a new Message.

So… I wondered… and opened Script Debugger to see how I fare there.

In ‘explorer’ mode of the Dictionary window, I can dig through Accounts, Mailboxes and Messages, and identify messages that DO HAVE attachments. I can even see counts of attachments, However, the properties of an attachment (the class is “mail attachment”) seem to be always empty.

However – when I use Script debugger’s “paste tell” into a new AppleScript, for something I clearly see in explorer window – it ALWAYS fails, and no matter how hard I try to phrase my attempt to access – I fail, each time with weirder errors.

My questions:

  • Am I the only one to experience this? Did others see their old AppleScripts break with Monterey’s new ‘Mail’ application?
  • How can Script Debugger’s explorer dig into hierarchies an AppleScript can’t?

Here are some of the things that failed.

-- "copy reference" and "paste tell" options yield the same script snippet:
tell application "Mail"
	tell its account "ProofPoint Exchange"
		tell (get mailbox "Bugs")
			tell its message 1
				tell its mail attachment "atl-generated-c38b2d51-f6f0-4a0c-8282-ff77945c1bd2.png"
					file size
				end tell
			end tell
		end tell
	end tell
end tell

which fails with error: Mail got an error: Can’t get mailbox “Bugs” of account “ProofPoint Exchange”.
Event error: Error: no such object (e.g. specifier asked for the 3rd, but there are only 2) (errAENoSuchObject:-1728)

-- 'copy value' of 'file size' works, and yields 425. 
-- However, 'copy value' of the *whole attachment*, won't even compile!

tell application "Mail"
	tell its account id "EAC63017-6E93-4701-B8A0-6BAAD7B8E478"
		tell its mailbox "Work Issues/Bugs"
			tell its message id 5
				mail attachment id "1.2"
			end tell
		end tell
	end tell
end tell

Any idea or hint will be greatly appreciated.

When you see the caution icons inScript Debugger’s explorer, that’s an indication that Script Debugger is using alternate object references to overcome errors in the target application. The Script Debugger dictionary UI isn’t helpful in explaining what its doing - this is something we can improve. In general, Script Debugger does the following to overcome object resolution errors:

  1. get the value of the object specifier, and ask for the property value of that:
tell application "Mail"
	tell its account id "EAC63017-6E93-4701-B8A0-6BAAD7B8E478"
		tell its mailbox "Work Issues/Bugs"
			tell its message id 5
				mail attachment id "1.2"
			end tell
		end tell
	end tell
end tell

becomes:

tell application "Mail"
	tell its account id "EAC63017-6E93-4701-B8A0-6BAAD7B8E478"
		tell its mailbox "Work Issues/Bugs"
			tell (get its message id 5)
				mail attachment id "1.2"
			end tell
		end tell
	end tell
end tell
  1. If the above does not work, we do the same thing to the parent object specifier:
tell application "Mail"
	tell its account id "EAC63017-6E93-4701-B8A0-6BAAD7B8E478"
		tell (get its mailbox "Work Issues/Bugs")
			tell (get its message id 5)
				mail attachment id "1.2"
			end tell
		end tell
	end tell
end tell

This approach tries to get a valid object reference from the target application rather than relying on AppleScript to construct a valid object reference. It does not always work, but often does.

In general, avoid using indexes when accessing objects as they are unstable (object deletion and movement invalidates indexes). IDs are best, names are a second choice.

PS: Caution icons in Script Debugger’s explorer can also indicate an unsupported key form (ID, index, name) in which case Script Debugger has switched to another key form to overcome the error. This is not the case in this instance.