Rule type "account" errors out in a Mail rule script

I need to apply the rule to the “Google 2” account. My script:

	if rule named "Delete Apple Support Communities thread intrusive messages" exists then delete rule named "Delete Apple Support Communities thread intrusive messages"
	tell (make new rule with properties {name:"Delete Apple Support Commubities thread intrusive messages", delete message:true, all conditions must be met:true})
		tell (make new rule condition with properties {header:"Subject"})
			set qualifier to does contain value
			set rule type to subject header
			set expression to "app store apps do not install"
		end tell
		
		tell (make new rule condition with properties {header:"Account", rule type:account})
			set expression to "Google 2"
		end tell
		
		
		make new rule condition with properties {rule type:matches every message}
		
		
		set enabled to true
		set stop evaluating rules to false
	end tell
end tell

Throws out the coercion error. Why?

Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 537.86.7
Operating System: macOS 10.9

May you try to use the same syntax already used for the first condition ?

		tell (make new rule condition with properties {header:"Account"})
			set rule type to account
			set expression to "Google 2"
		end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 10 décembre 2020 16:26:32

Tried that too. The same error. It rejects the syntax because “account” doesn’t compile as an enumeration but class instead. I’m afraid it’s a bug.

Looks like a bug.

When I open the dictionary in Script Debugger, account is listed as a class but it’s also described as a constant belonging to the list/record Rule Type.

Maybe Shane Stanley may bring some light about that.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 10 décembre 2020 18:01:14

And how do you script that?

Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 537.86.7
Operating System: macOS 10.9

I added “account” manually in Rules pane and ran this snippet. Retrieving values returns the correct class of “account” as an enumeration not class. The problem is it’s inaccessible.

:mad:

What gives?

Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 537.86.7
Operating System: macOS 10.9

Are you sure that we are allowed to define a rule type for the rule whose header is “Account” ?
There is another one whose french header is “Type de pièce jointe” which accept only a single complementary property.
Some of them accept only the header property.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 10 décembre 2020 21:20:16

No, I’m not. Frankly speaking, I don’t know every valid header and inferred them by analogy with labels in the Rules composer sheet. Some of them are contained within XML files having “Rules” as a part of their names, inside Mail Data subfolder of “Mail” folder (“AnyMessage”, "To, “From”, “Body” - some are camel case if those consist of several words) but I don’t know if that’s what one can use as the value of “header”. Where do I get the complete list of headers?

On the other hand, I don’t think it has to do with the error as it should compile regardless.

If “Account” is not a header then what is it?

Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 537.86.7
Operating System: macOS 10.9

I played around with it and ran into the same problem. I thought a workaround might be to duplicate the account statement from one rule condition to another, but that also failed. Why not let AppleScript perform the steps directly on the Mail account, rather than creating a rule to do so?

Ideally, I’d like to do without the need to plug accounts in at all but in macOS rules are said to fail being applied automatically because it looks like Apple engineers decided that once the message is read the rules don’t need to come into action unless launched manually (via “Apply Rules” from the context menu). Some people on Stackexchange have reported that adding an account makes rules kick in. That’s why I settled on using “account” but I’d rather not to because, logically, there’s no sense at all.
Another disturbing Apple’s bug.

Model: MacBook Pro
AppleScript: 2.3.2
Browser: Safari 537.86.7
Operating System: macOS 10.9

The problem is not that “Account” is or isn’t a header.
It’s that, seemingly, the rule whose header is “Account” can’t have a rule list property.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 11 décembre 2020 08:28:05

The error sticks it out with or without any header.

Again, my question was is there a reason you’re not using AppleScript for the evaluation, rather than using it to create an evaluator?

No.

If the header is “Subject”, you-use the ability to define a rule type property.
It seems that we aren’t allowed to do that when the header is “To” or when the header is “Account”.
I don’t see that as an annoying feature.
Just don’t try to define this property when we aren’t allowed to.
If you can’t remember which headers accept this property and which doesn’t, enclose the instruction supposed to define it in a try / end try block.

tell application "Mail"
	tell (make new rule condition with properties {header:"Account"})
		try
			set rule type to account
		end try
		try
			set expression to "Google 2"
		end try
	end tell
end tell

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 11 décembre 2020 18:46:48

Mail Rules are preset criteria evaluators. Since the terminology conflict doesn’t exist outside the rule condition, then it may have made more sense to directly perform the steps your rule was intended to perform; a GUI approach is seldom the best AppleScript solution.

Looking back through my script library, I’ve stumbled across the solution to your original issue. You speculated that a rule condition’s account is a header, but it’s actually a constant—one that’s poorly documented and implemented in raw AS form. Use «constant eruttacc» instead of “account” in the rule condition.

The last suggestion («constant eruttacc») of Mark Anthony worked for me. Thanks. :smiley:

tell application "Mail"
	tell (make new rule with properties {name:"Delete Apple Support Commubities thread intrusive messages", delete message:true, all conditions must be met:true})
		(make new rule condition with properties {rule type:«constant eruttacc», expression:"imap://kniazidis.rompert%40gmail.com/"})
		set its enabled to true
		set stop evaluating rules to false
	end tell
	save
end tell

Thanks Mark, but it fails on me.

A screen recording of the problem (click)

Model: MacBook Pro 9,1 (mid-2012 15") Core i7 2.3 GHz, 16 GB RAM, 1TB SSD
AppleScript: 2.7
Browser: Safari 605.1.15
Operating System: macOS 10.14

This constant works fine if you make it the LAST CHANGE to the script before compiling or executing it.

But here, try my improvement to the one suggested by Mark Anthony. My approach will allow you to make changes to the script at any time. This is its advantage.

NOTE: Don’t forget to change my iCloud account address to your own.


set ruleConditionAccountConstant to «constant eruttacc»

tell application "Mail"
	tell (make new rule with properties {name:"Delete Apple Support Commubities thread intrusive messages", delete message:true, all conditions must be met:true})
		make new rule condition with properties {rule type:ruleConditionAccountConstant, expression:"imap://kniazidis.rompert%40gmail.com/"}
		set its enabled to true
		set stop evaluating rules to false
	end tell
	save
end tell

Here is other version. It shows how to pass iCloud account address (should be in URL-encoded form) to rule condition’s expression:


set accountName to "iCloud"
set ruleConditionAccountConstant to «constant eruttacc»

tell application "Mail"
	set |iCloud| to "imap://" & my encode_URL(user name of account accountName) & "/"
	tell (make new rule with properties {name:"Delete Apple Support Commubities thread intrusive messages", delete message:true, all conditions must be met:true})
		make new rule condition with properties {rule type:ruleConditionAccountConstant, expression:|iCloud|}
		set its enabled to true
		set stop evaluating rules to false
	end tell
	save
end tell

on encode_URL(txt)
	set python_script to ¬
		"import sys, urllib; print urllib.quote(sys.argv[1])"
	set python_script to "/usr/bin/python -c " & ¬
		quoted form of python_script & " " & ¬
		quoted form of txt
	return do shell script python_script
end encode_URL

Thanks KniazidisR, will try that. A question:

  1. Is account name not enough and must be a URL?[/*]
  2. Do we need save? A created rule exists either way.[/*]