Catalina and mail accounts

Hi,
Apple did broke (again) some Mail’s applescript.
It was previously possible to enable/disable an account in Mail using this script :

tell application "Mail"
	set enabled of account "Pro" to true
end tell

It doesn’t work anymore in Catalina.
Did someone find a workaround ?

Are you the one which asked the same question on https://forum.latenightsw.com/t/enable-disable-a-mail-account/2377 ?

If you aren’t, I posted:

Here, under 10.13.6 the account #1 is Gmail.

Your code doesn’t disable it and issue no error.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 1 avril 2020 15:44:40

Salut Yvan :slight_smile:
It’s not me but it used to work on Mojave.
Actually this command :

tell application "Mail"
return properties of account "Pro"
end tell

…doesn’t work either, but this :

tell application "Mail"
return name of account "Pro"
end tell

…works and returns “Pro”.

Maybe the only solution would be to interact with the System Pref panel “Internet Accounts” in which you can activate or deactivate Mail accounts, but I didn’t find any script.

My workaround for the moment :


--SCRIPT TO ACTIVATE/DEACTIVATE MAIL ACCOUNT using gui
property ToActivate : true
set myAccount to "Perso" -- or name of the mail account you want to activate

tell application "System Preferences" to quit
delay 0.5

tell application "System Preferences"
	activate
	delay 1
	
	tell application "System Events"
		tell process "System Preferences"
			click button "Internet
Accounts" of scroll area 1 of window "System Preferences"
			delay 1
			keystroke tab
			delay 0.5
			set theRows to rows of table 1 of scroll area 1 of window "Internet Accounts"
			set theRows_count to count theRows -- count accounts
			repeat with r from 1 to theRows_count
				set aRow to item r of theRows
				if value of static text 1 of UI element 1 of aRow is myAccount then
					
					select row r of table 1 of scroll area 1 of window "Internet Accounts"
					exit repeat
				end if
				
			end repeat
			if value of checkbox 1 of UI element 1 of row 1 of table 1 of scroll area 1 of group 2 of window "Internet Accounts" is 1 then set ToActivate to false
			
			
			if ToActivate is true then
				click checkbox 1 of UI element 1 of row 1 of table 1 of scroll area 1 of group 2 of window "Internet Accounts"
			else if ToActivate is false then
				
				click checkbox 1 of UI element 1 of row 1 of table 1 of scroll area 1 of group 2 of window "Internet Accounts"
				
			end if
			
			
		end tell
	end tell
end tell

Hi, alitaliano

A minor suggestion.

When dealing with booleans the syntax can be even more flexible. In your case, since the property ToActivate is already set, the following statement is valid too:

[format]if ToActivate then
– statements
else

           -- statements
           
       end if

[/format]

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

@ alitaliano

As your original script doesn’t execute the wanted task under High Sierra I decided to test your GUI version.
At first I added instructions allowing it to work accordingly to the language in use.
When it was done I discovered that the GUI is not the same under High Sierra (why I use) and under Catalina which can’t be installed on my machine.

I edited the script so that it may be used in both system.
In fact I made provision for a third GUI possibly used by Mojave.

You will see some comments about your use of the property ToActivate.
If I understand well with both values, true or false, the checkbox is always clicked.
So I see no need to extract the original value and code a test.
All that may be achieved with a single instruction : click enableThisAccount.

Here is the edited script:


--SCRIPT TO ACTIVATE/DEACTIVATE MAIL ACCOUNT using GUI
property ToActivate : true


set myAccount to "Perso" -- or name of the mail account you want to activate

set theBundle1 to ((path to library folder from system domain as text) & "PreferencePanes:InternetAccounts.prefPane:") as «class furl»
set x to "NSPrefPaneIconLabel"
set internetAccountIcon_loc to localized string x from table "InfoPlist" in bundle theBundle1
set theBundle2 to (path to application "System Preferences") as «class furl»
set x to "CFBundleName"
set internetAccountWindow_loc to localized string x from table "InfoPlist" in bundle theBundle1
set systemPreferences_loc to localized string x from table "InfoPlist" in bundle theBundle2

set sys2 to (system attribute "sys2") as integer

tell application "System Preferences" to quit
delay 0.5

tell application "System Preferences"
	activate
	delay 1
	
	tell application "System Events"
		tell process "System Preferences"
			set frontmost to true
			click button internetAccountIcon_loc of scroll area 1 of window systemPreferences_loc
			delay 1
			keystroke tab
			delay 0.5
			set theRows to rows of table 1 of scroll area 1 of window internetAccountWindow_loc
			set theRows_count to count theRows -- count accounts
			repeat with r from 1 to theRows_count
				set aRow to item r of theRows
				if value of static text 1 of UI element 1 of aRow is myAccount then
					select row r of table 1 of scroll area 1 of window internetAccountWindow_loc
					exit repeat
				end if
			end repeat
			
			if sys2 < 14 then
				set enableThisAccount to checkbox 1 of group 2 of window internetAccountWindow_loc -- I checked that it's the real GUI in High Sierra
			else if sys2 < 15 then
				set enableThisAccount to checkbox 1 of group 2 of window internetAccountWindow_loc -- I can't test which is the real GUI of Mojave
			else
				set enableThisAccount to checkbox 1 of UI element 1 of row 1 of table 1 of scroll area 1 of group 2 of window internetAccountWindow_loc -- it's the GUI used in your script
			end if
			(*
			--if value of enableThisAccount is 1 then set ToActivate to false
			set ToActivate to (value of enableThisAccount) is 0
			-- I am puzzled
			if ToActivate is true then
				-- you click it if ToActivate is true
				click enableThisAccount
			else --if ToActivate is false then
				-- here ToActivate is guaranteed to be false
				-- you click it if ToActivate is false
				click enableThisAccount
			end if
			-- what need for this test.
			*)
			click enableThisAccount
		end tell
	end tell
end tell
tell application "System Preferences" to quit

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) mercredi 1 avril 2020 21:49:52

I tried your script on the Catalina, but it doesn’t work. And, I know why not - selecting the row doesn’t open the window containing checkboxes. You need find the position of the row, then click on this position using some mouse clicking tool (as clickClick). The mouse pointer should be moved over the row’s position to do real action.

The path to checkbox of Catalina is right.

And, the property enabled of account is one another bug of Catalina. I am sure, because you can’t even get its value in the Mail.app. I guess, in the other apps using this property, too. I think, this is a bug of accounts processing itself (System Preferences). So, it should be reported to Apple.

As I can’t run Catalina, I reproduced what is available in the OP’s proposal.
More, as is, the script does its duty under High Sierra.
I will add the instruction which you describe but I wonder why what work on OP’s Catalina machine doesn’t do on yours.

May you test the GUI scripting posted by the OP and report its behavior on your machine ?

The described bug isn’t specific to Catalina.

As I already wrote, under High Sierra, the original code doesn’t issue an error but it doesn’t set the status of the account as required.
If you are lucky, the Catalina bug will be solved but the High Sierra one will never be killed.

Here is a modified version.
The code is cleaned.
As I can’t test under Catalina, I applied the protocol proposed by KniazidisR.
It works under High Sierra too.

This time the code is in a handler and we may call it to really enable or disable the account as we want.


set myAccount to "Perso" -- or name of the mail POP account you want to activate

my ruleAccount:myAccount enable:true -- Enable the account
-- my ruleAccount:myAccount enable:false -- Disable the account

on ruleAccount:myAccount enable:wantEnable
	# Grab localized name of some items
	set theBundle1 to ((path to library folder from system domain as text) & "PreferencePanes:InternetAccounts.prefPane:") as «class furl»
	set x to "NSPrefPaneIconLabel"
	set internetAccountIcon_loc to localized string x from table "InfoPlist" in bundle theBundle1
	set theBundle2 to (path to application "System Preferences") as «class furl»
	set x to "CFBundleName"
	set internetAccountWindow_loc to localized string x from table "InfoPlist" in bundle theBundle1
	set systemPreferences_loc to localized string x from table "InfoPlist" in bundle theBundle2
	# Get the code of the running OS (13 = High Sierra, 14 = Mojave, 15 = Catalina …)
	set sys2 to (system attribute "sys2") as integer
	
	tell application "System Preferences" to quit
	delay 0.5
	tell application "System Preferences" to activate
	tell application "System Events" to tell process "System Preferences"
		set frontmost to true
		click button internetAccountIcon_loc of scroll area 1 of window systemPreferences_loc
		repeat until exists window internetAccountWindow_loc
			delay 0.1
		end repeat
		-- keystroke tab -- is useless
		tell window internetAccountWindow_loc
			-- KniazidisR's loop
			repeat until scroll area 1 exists
				delay 0.1 -- NEVER entered here !!!!!!!!
			end repeat
			set theRows to rows of table 1 of scroll area 1
			repeat with aRow in theRows
				if value of static text 1 of UI element 1 of aRow is myAccount then
					set theRow to aRow
					select theRow -- original code
					-- code asked erroneously by KniazidisR
					-- set {x, y} to position of theRow
					-- tell me to do shell script "/usr/local/bin/cliclick c:" & x + 2 & "," & y + 2
					exit repeat
				end if
			end repeat
			try
				if sys2 < 14 then
					set enableThisAccount to checkbox 1 of group 2 -- I checked that it's the real GUI in High Sierra
				else if sys2 < 15 then
					set enableThisAccount to checkbox 1 of group 2 -- I can't test which is the real GUI of Mojave
				else
					set enableThisAccount to checkbox 1 of UI element 1 of row 1 of table 1 of scroll area 1 of group 2 -- it's the GUI used in your script
				end if
			on error
				error "The account “" & myAccount & "” is an IMAP one or isn’t available, please, add it or ask for a POP one."
			end try
			set isEnabled to (value of enableThisAccount) as boolean
			
			if (isEnabled and (not wantEnable)) or ((not isEnabled) and wantEnable) then
				click enableThisAccount
			end if
		end tell
	end tell
	-- delay 1
	tell application "System Preferences" to quit
end ruleAccount:enable:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 2 avril 2020 12:09:56

I have to apologize. It turns out that all your scripts work, only they work very unstable, are slow and have redundant code. No need extra mouse click tool, it was my fault. I wrote a stable version here:


--SCRIPT TO ACTIVATE/DEACTIVATE MAIL ACCOUNT using gui

set myAccount to "KNIAZIDIS.ROMPERT@gmail.com" -- or name of the mail account you want to activate

tell application "System Preferences"
	activate
	reveal pane id "com.apple.preferences.internetaccounts"
end tell

tell application "System Events" to tell process "System Preferences"
	repeat until window "Internet Accounts" exists -- does not appear quickly	
		delay 0.1
	end repeat
	tell window "Internet Accounts"
		repeat until scroll area 1 exists -- does not appear quickly	
			delay 0.1
		end repeat
		repeat with aRow in (rows of table 1 of scroll area 1)
			if value of static text 1 of UI element 1 of aRow is myAccount then
				select aRow
				exit repeat
			end if
		end repeat
		try
			click checkbox 1 of UI element 1 of row 1 of table 1 of scroll area 1 of group 2
		on error
			display dialog "This account is an IMAP one or isn’t available. Add it or ask for a POP one."
		end try
	end tell
end tell

delay 1 -- this delay is only to see the result
tell application "System Preferences" to quit

NOTE: Maybe, the account doesn’t exist on the scroll area.

@KniazidisR
The fact that the checkbox is missing doesn’t mean that the account is unavailable.
It may be an IMAP one because there is no checkbox in the pane dedicated to such account (at least under High Sierra).
I edited my late message accordingly.
On my machine your loop waiting for scroll area 1 is NEVER entered.

Below is a stripped code.
I wish to know what is returned under Mojave and under Catalina.


set myAccount to "Perso" -- or name of the mail account you want to activate

my ruleAccount:myAccount enable:true -- Enable the account
-- my ruleAccount:myAccount enable:false -- Disable the account

on ruleAccount:myAccount enable:wantEnable
	# Grab localized name of some items
	set theBundle1 to ((path to library folder from system domain as text) & "PreferencePanes:InternetAccounts.prefPane:") as «class furl»
	set x to "NSPrefPaneIconLabel"
	set internetAccountIcon_loc to localized string x from table "InfoPlist" in bundle theBundle1
	set theBundle2 to (path to application "System Preferences") as «class furl»
	set x to "CFBundleName"
	set internetAccountWindow_loc to localized string x from table "InfoPlist" in bundle theBundle1
	set systemPreferences_loc to localized string x from table "InfoPlist" in bundle theBundle2
	# Get the code of the running OS (13 = High Sierra, 14 = Mojave, 15 = Catalina …)
	set sys2 to (system attribute "sys2") as integer
	
	tell application "System Preferences" to quit
	delay 0.5
	tell application "System Preferences" to activate
	tell application "System Events" to tell process "System Preferences"
		set frontmost to true
		click button internetAccountIcon_loc of scroll area 1 of window systemPreferences_loc
		repeat until exists window internetAccountWindow_loc
			delay 0.1
		end repeat
		-- keystroke tab -- is useless
		tell window internetAccountWindow_loc
			-- KniazidisR's loop
			repeat until scroll area 1 exists
				delay 0.1 -- NEVER entered here !!!!!!!!
			end repeat
			set theRows to rows of table 1 of scroll area 1
			repeat with aRow in theRows
				if value of static text 1 of UI element 1 of aRow is myAccount then
					set theRow to aRow
					select theRow -- original code
					-- code asked erroneously by KniazidisR
					-- set {x, y} to position of theRow
					-- tell me to do shell script "/usr/local/bin/cliclick c:" & x + 2 & "," & y + 2
					exit repeat
				end if
			end repeat
			class of UI elements of group 2
			--> {image, static text, static text, static text, static text, text field, static text, text field, static text, text field, static text, checkbox, button} with High Sierra
			--> { ?, ?, ?, ?, ?} with Mojave
			--> {image, static text, static text, button, scroll area} with Catalina
		end tell
	end tell
end ruleAccount:enable:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 2 avril 2020 14:26:40

For the IMAP you are right. I updated my script due to you suggestion. But why you quit “System preferences”, then delay (some delay taken from where?) , activate, click button “Internet/nAccounts”, which need 1 another localized string an so on?

Why do you not simply open the needed pane as I do? And I can explain you why this loop on your machine NEVER entered and ENTERED on my machine. My machine runs this moment expensive tasks - 4 updates. One of this updates is xCode. So, you understand what happens.

NOTE: I tried your stripped code on new Catalina, and it works fine.

As for the application quitting time taken for good luck, the script itself should always find it. It is just stable:


tell application "System Preferences"
	quit
	repeat while running
		delay 0.1
	end repeat
	activate
end tell

This is correct application’s restarting code, asked many times from users. But here we can avoid this step.

The OP did that. I assume that he had good reasons to do that but I may be wrong

Don’t worry, I was puzzled but as I guessed that something was behaving differently on your machine I played safe and inserted your loop

I knew that it work, what I wish to know is the list of classes of UI elements which it returns, mostly for Mojave about which I have no info.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 2 avril 2020 16:55:14

→ {image, static text, static text, button, scroll area} with Catalina 10.15.4

Thank you

Now I hope that somebody will be able to test under Mojave.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) jeudi 2 avril 2020 19:26:56

This is much faster workaround to set enabled true. We should forget the “System Preferences”:


set myAccount to "KNIAZIDIS.ROMPERT@gmail.com"
tell application "Mail" to activate

tell application "System Events" to tell process "Mail"
	repeat until window 1 exists
		delay 0.1
	end repeat
	keystroke "," using command down -- open "Accounts..." window
	repeat until window "Accounts" exists
		delay 0.1
	end repeat
	tell window "Accounts"
		set theRows to rows of table 1 of scroll area 1 of group 1
		repeat with aRow in theRows
			if value of static text 1 of UI element 1 of aRow is myAccount then
				select aRow
				exit repeat
			end if
		end repeat
		tell checkbox "Enable this account" of tab group 1 of group 1 to if value is 0 then click
	end tell
end tell

tell application "Mail" to close window "Accounts"

Now, I go at work.

Taking benefit of the KniazidisR’s proposal, here is my localized and enhanced version.
KniazidisR’s one assume that triggering the preferences open the Accounts pane which is not guaranteed :rolleyes:


set myAccount to "KNIAZIDIS.ROMPERT@gmail.com"

my ruleAccount:myAccount enable:true -- Enable the account
-- my ruleAccount:myAccount enable:false -- Disable the account

on ruleAccount:myAccount enable:wantEnable
	
	set aBundle to ((path to library folder from system domain as text) & "Automator:Mail.definition:") as «class furl»
	set accounts_loc to localized string "accounts" in bundle aBundle
	
	set x to "5fB-ji-kiv.title"
	tell application "Mail"
		activate
		set loc to localized string x from table "Prefs"
	end tell
	if loc = x then set loc to "Enable this account" -- under High Sierra, Prefs.strings is missing in en.lproj
	set enableThisAccount_loc to loc
	set sys2 to (system attribute "sys2") as integer
	
	tell application "System Events" to tell process "Mail"
		set frontmost to true
		repeat until window 1 exists
			delay 0.1
		end repeat
		keystroke "," using command down -- open "Preferences" window
		delay 0.2 -- must use a default delay because the window may be named : Général, Comptes, Courrier indésirable, Présentation, Rédaction, Signatures, Règles.
		tell window 1 to click button accounts_loc of toolbar 1 -- activate the Account pane
		repeat until window accounts_loc exists
			delay 0.1
		end repeat
		tell window accounts_loc
			set theRows to rows of table 1 of scroll area 1 of group 1
			repeat with aRow in theRows
				if value of static text 1 of UI element 1 of aRow is myAccount then
					select aRow
					exit repeat
				end if
			end repeat
			set enableThisAccount to checkbox enableThisAccount_loc of tab group 1 of group 1
			set isEnabled to (value of enableThisAccount) as boolean
			if (isEnabled and (not wantEnable)) or ((not isEnabled) and wantEnable) then
				click enableThisAccount
			end if
		end tell
	end tell
	
	tell application "Mail" to close window accounts_loc
end ruleAccount:enable:

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) vendredi 3 avril 2020 12:29:08