Is "background activity count" in Mail.app broken?

I have tried to use the

background activity count

in Mail.app to determine when a task is done but I just get an error message error

"Mail got an error: AppleEvent handler failed." number -10000

. Is it broken or am I using the wrong syntax? Something like:

tell application "Mail"
    activate
    repeat until background activity count = 0
        delay 0.5
    end repeat
end tell

Would be very appreciated if someone else could verify this.

Thank you.

No one knows anything about this?

It looks like that’s broken.

I get the same error message when I run your script. Plus I get the same error message if I try send a command to get:

properties
background activity count
fetches automatically
primary email

But, other application properties, like application version work fine.

In Script Debugger, if I look at the application properties in the Explorer window, I see the same error message for those properties.

→ Script Debugger 8.0.6 (8A62)
→ MacOS 12.4
→ Mail 3696.100.31


tell application "Mail"
	application version --works (as do most other application properties)
	
	properties --errors
	background activity count --errors
	fetches automatically --errors
	primary email --errors
end tell

This works for me.

tell application id "com.apple.Mail"
	set accountId to id of (its accounts) as string
	tell account id accountId
		its properties
	end tell
end tell

Interesting. Please let us know what MacOS version and mail version you’re running. Your script generates an error on my system. And, as with the application properties, when I inspect the account properties in the Script Debugger explorer view, it also shows an error for some.

Some properties work, and some don’t (the ones that don’t in a script show errors in the Script Debugger explorer too).

Also, getting the account properties doesn’t help answer the OP question about background activity count.

Do those scripts generate errors on your mac?



tell application "Mail"
	tell account 1
		its account type-- works
		its enabled-- errors
		its properties -- errors
	end tell
end tell
1 Like

@estockly
It doesn’t use application properties…
Its use accounts that will return account id

This will return {“iCloud”}

tell application "Mail"
	name of (its accounts)
end tell

I use Monterey 12.6.1 on Apple Silicon, Mail is version 16.0

And your example code also works on my machine.

1 Like

I could see from Mail.sdef

This line is not correct


Should be


Also
should be
should be
should be

Any cocoa key start with lowercase and not uppercase.

Little strange, for me this is basic knowledge and I’m little surprised core developers of Mail do not know that.

If I run this code it return 0, so I do not know for sure what that really means.

use framework "Foundation"
use framework "ScriptingBridge"
use scripting additions

set theApp to current application's SBApplication's applicationWithBundleIdentifier:"com.apple.Mail"
(theApp's backgroundActivityCount) as integer

If I run other from the application properties it returns true

(theApp's downloadHtmlAttachments) as boolean

So my conclusion is… AppleScript has problem with application properties.
But on other hand its possible to use ScriptingBridge framework

1 Like

Something is odd with your code examples, they are white/empty:

Fredrik71,

As you rightly pointed out, using ScriptingBridge allows access to problematic properties of the application.

But you are mistakenly using the backgroundActivityCount property instead of the backgroundActivityCount() method. Which by the way correctly returns an integer as a result without the need for any coercions.
 

use framework "Foundation"
use framework "ScriptingBridge"
use scripting additions

set theApp to current application's SBApplication's applicationWithBundleIdentifier:"com.apple.Mail"
theApp's |activate|()
repeat until (theApp's backgroundActivityCount() = 0)
	delay 0.5
end repeat

@KniazidisR
Sure, but its KVC compatible property.

use framework "Foundation"
use framework "ScriptingBridge"
use scripting additions

set theApp to current application's SBApplication's applicationWithBundleIdentifier:"com.apple.Mail"
set backgroundActivityCount to theApp's valueForKey:"backgroundActivityCount"
return backgroundActivityCount as anything

I don’t understand, are any of the two last examples you @Fredrik71 and you @KniazidisR posted supposed to work? I still only get 0 as the result.

Thank you both.

1 Like

@lagr,

My script contains a repeat loop that exits when a condition is met. This should stop the rest of the lines of code while Mail.app is doing something in the background rather than returning the background activity count. That is, most likely you are testing it, but do not understand that its result should be more execution time while Mail.app is doing something in the background. And nothing more.

The script is supposed to work.

Background activity count is used by users to solve the problem of synchronizing user script activities with Mail.app activities. That is, if you correctly insert my fragment into your problematic script and the problem disappears, then this will be an indicator that the solution works.

I understood what your script was doing, it just didn’t perform as I expected. Maybe the problem is a matter of definitions: what is a background activity?

I copied 100 mails from one IMAP account to another, and while this operation was going on, executed your script expecting it to continue until the copying was done but it finished immediately. Is that result expected? What operation should I try to trigger a background activity that makes your script loop?

Thank you.

I am not the developer of Mail.app to give you the exact definition of a background activity in this particular app. Looking through Mail.app’s Preferences, I see a lot of settings to automatically run (shedule) tasks at regular intervals. I think background activity is any activity associated with these settings.

How to test I have already said. Since you are interested in the background activity, you must have some kind of big script that goes wrong precisely because of the out of sync. You add my code to it and if everything is corrected, the test is passed.

Can you create/describe a test case where your script runs for s while?

My use case is that I want to copy 100 000+ mails between two IMAP-accounts. Drag-n-drop doesn’t work, the process is always interrupted somewhere in the middle and you end up with not knowing what has been copied.

I have therefore written a script that copy one message at the time and check that the process was successful before it copies the next message. Before the final check I want to make sure that there are no active background processes, that is, the message really has been physically transferred to the destination account and there is no uploading or syncing still going on.

@lagr
Have you try to import mailbox instead of copy?.