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

It is not enough. You can see in the preferences. The rule condition’s second field will be empty your way.

To be the checkbox of new rule checked (to work really) you need save the changes.

I experimented a bit with the expression property of the account condition. The script now doesn’t throw errors, creates a rule but this is what I see at GUI level, regardless of whether I supply the imap URI scheme or a simple name of the account.

The script:

property AccountConstantRawSyntax : «constant eruttacc»
tell application "Mail"
	if not (exists account "Google 2") then return
	tell account "Google 2"
		set _username to user name
		set _username to GetURI of me given ReplString:_username
		set _imapscheme to "imap://"
		set _userresource to _imapscheme & _username
	end tell
	if (every rule whose name is "Delete Apple Support Commubities thread intrusive messages") is not {} then
		repeat with i from 1 to number of items in (every rule whose name is "Delete Apple Support Commubities thread intrusive messages")
			set this_item to item i of (every rule whose name is "Delete Apple Support Commubities thread intrusive messages")
			delete this_item
		end repeat
	end if
	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:matches every message}
		
		tell (make new rule condition with properties {header:"Subject"})
			set rule type to subject header
			set qualifier to does contain value
			set expression to "apps do not installed properly"
		end tell
		
		
		
		tell (make new rule condition with properties {rule type:AccountConstantRawSyntax}) to set expression to "Google 2" -- {rule type:AccountConstantRawSyntax, expression:_userresource} doesn't make a difference regardless of whether _userresource is "Google 2" or "imap://blablabla".
		set stop evaluating rules to false
		set enabled to true
	end tell
end tell


to GetURI given ReplString:UserName
	get UserName
	set AppleScript's text item delimiters to "@"
	set TempTxt to text items of UserName
	set AppleScript's text item delimiters to "%40"
	return TempTxt as text
end GetURI

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

There’s no solution at all. Per dictionary, constant is not a text, but expression is. Those never match. Since account is an enumeration it should be followed by an unquoted value. Apple hasn’t included proper syntax for enumerations in Mail Dictionary, specifically, they haven’t provided unquoted values for the enumeration of Account.
The issue’s aggravated by the fact that even though saved when I relaunch Mail the checkmark is gone. I looked into SyncedRules.plist and it has the entry. Before that, I had created the rule manually with the same rule condition (1 for 1) and checked that plist and, turned out, it had “Header” as “Account” and “Expression” for my account as the imap URI string. I replaced Account as a constant with Account as a header, added the imapURI as its expression and ran the script. After that, the SyncedRules.plist showed the identical entries, the identical classes and the identical values for my rule but the rule was still unsaved and the blank option for “account” in “Rules”. Why Mail chokes on “Account” is an unsolved mystery.

Outstanding. “Just works”.

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

On my machine I have 1 Cloud account (named “iCloud”) and Google account (named “Gmail”).

I made some changes to your script and it worked for me with my “Gmail”. Here is fixed for you script:


property accountName : "Google 2" -- ADDED
property ruleName : "Delete Apple Support Commubities thread intrusive messages" -- ADDED
property AccountConstantRawSyntax : «constant eruttacc»

tell application "Mail"
	if not (exists account accountName) then return
	tell account accountName
		set _username to user name
		set _username to GetURI of me given ReplString:_username
		set _imapscheme to "imap://"
		set _userresource to _imapscheme & _username & "/" -- ADDED "/" 
	end tell
	set theRules to (every rule whose name is ruleName)
	if theRules is not {} then -- made simpler
		repeat with anItem in theRules
			delete contents of anItem
		end repeat
	end if
	
	tell (make new rule with properties {name:ruleName, delete message:true, all conditions must be met:true})
		make new rule condition with properties {rule type:matches every message}
		tell (make new rule condition with properties {header:"Subject"})
			set rule type to subject header
			set qualifier to does contain value
			set expression to "apps do not installed properly"
		end tell
		tell (make new rule condition with properties {rule type:AccountConstantRawSyntax}) to set expression to _userresource
		set stop evaluating rules to false
		set enabled to true
	end tell
end tell


to GetURI given ReplString:UserName
	get UserName
	set AppleScript's text item delimiters to "@"
	set TempTxt to text items of UserName
	set AppleScript's text item delimiters to "%40"
	return TempTxt as text
end GetURI

Thank you, that finally worked. The key to the solution was the extra backslash character. I added save to your script and the rule finally stuck in Preferences.

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

IMPORTANT CONSIDERATION AS TO THE FORM OF THE VALUE OF THE EXPRESSION PROPERTY. In older revisions of macOS (such as Mavericks) a value of the expression property of a rule condition must be a relative Posix path to the account’s directory without the trailing “/”.

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