Toggle Safari Extensions

I’m a total scripting noob, and am looking for a simple script to toggle safari’s extensions on and off.
Haven’t got much further than:

activate application "Safari"
tell application "Safari" to

Anyone help nudge me in the right direction?

Thanks,
Pato

Hello.

This worked for me inside my editor, if something doesn’t work, then some delay after the first keystroke should be inserted, I have commented in one for you, and after I click the button for the extensions, that you may wish to comment out, and adjust.

This script wouldn’t have been possible by me, if Yvan haden’t shown a script where he queried for the class of UI Elements. :slight_smile:

-- Toggle Safari Extensions
-- http://macscripter.net/viewtopic.php?pid=179568#p179568
-- http://macscripter.net/viewtopic.php?id=43326 ( Yvan Koenig how to)
tell application "Safari" to activate
try
	tell application id "sevs"
		tell process "Safari"
			keystroke "," using command down
			-- delay 0.5
			tell button 8 of toolbar 1 of window 1 to click
			-- delay 0.5
			tell checkbox 1 of UI element 2 of UI element 1 of scroll area 1 of group 1 of group 1 of window 1 to click
		end tell
	end tell
on error e number n
	tell application (path to frontmost application as text)
		display alert "UIScripting not enabled?" message e & " : " & n
	end tell
end try
tell application "Safari" to close window 1
(*
If it errors, then you should see to that Safari is among the applications that
are eligible for using assistive devices. (That it is enabled under the accessibility 
tab of the Privacy settings of System Preferences.)

*)

Hello.

I made Safari close the preferences window as a last step. (You may want to comment out that line during any testing).

Edit

I have also added a comment about what to do, shouldn’t it work. If it doesn’t work, then Safari isn’t enabled for assistive devices, then you’ll have to go into System Preferences, click the Privacy settings tab, then choose the Accessibility settings, and check off for Safari. -Or answer yes to some dialog when the script errs, and let that dialog take you there. :slight_smile:

Hi,

Sorry it’s taken me so long to get back to you. Long story short, the error message you put in pops up and I have been trying to find an answer to what I assumed was a UIScripting issue related to Yosemite.

To explain what I’ve done, I followed the instructions here, as it’s pretty much the same in 10.10 as it was in 10.9: http://www.macosautomation.com/mavericks/guiscripting/index.html

But, the Accessibility settings are in place:

The error message I’m getting looks like this:

Which makes me think the problem might lie elsewhere.

I tried googling the error code, but found nothing useful. Any further thoughts on what could be wrong?

Thanks in advance for your efforts!
Pato

Hello.

I can’t look at it at the moment, but do you have window open in Safari when you run the script, (just to be assured), secondly, you haven’t saved the script as an applet? Also to be assured. I’ll find some time during the weekend to look at this, unless of course, someone else, solves it before me. :slight_smile:

-In the worst case, they have removed the accessibilty to the preferences window, by converting it to a float, and then there isn’t anything we can do, because then they have done it consciously, to deterr UI scripting of it, which per say, can be justified, in that Safari provides access to the outside world, and as such may be vulnerable to attacks.

Edit

One more thing, if you have had Safari windows in several spaces, while you tried it, then please confine the Safari windows to one space, which you run the script from, this is also just for assuring. :slight_smile:

I also wonder if you can see Safari’s preferences window before you see the error message.

Hi again,

One can export scripts in ScriptEditor 2.7 as Script, Script Bundle, Application & Text. I chose Application.

I have Safari open with multiple tabs, but confined to one space. Also tried both Full-screen and Window modes. The Preferences Window is not open.

Appreciate any help you can give!

Pato

Hello.

The preferences window, should have been opened I think, try enabling a delay of 1 second after the keystroke command in the script, and see if it works then, if it does, then you can decrement the delay in 0.2 sec steps, and keep the last one that rendered the script usable.

Sorry, forgot to mention that I’d tried that already.

Here’s the current state of what I’m using:

[code]tell application “Safari” to activate
try
tell application id “sevs”
tell process “Safari”
keystroke “,” using command down
– delay 2
tell button 8 of toolbar 1 of window 1 to click
– delay 1
tell checkbox 1 of UI element 2 of UI element 1 of scroll area 1 of group 1 of group 1 of window 1 to click
end tell
end tell
on error e number n
tell application (path to frontmost application as text)
display alert “UIScripting not enabled?” message e & " : " & n
end tell
end try

(*
tell application “Safari” to close window 1

If it errors, then you should see to that Safari is among the applications that
are eligible for using assistive devices. (That it is enabled under the accessibility
tab of the Privacy settings of System Preferences.)

*)[/code]
oddly, it opened the Preferences window once, but never thereafter.

Hello.

Am I right in that the preferences window of Safari never turns up for you?

Doesn’t this open the preferences window of Safari, when you save it somewhere in Safari’s or the “main” script menu?

tell application "Safari" to activate
tell application "System Events"
	tell process "Safari"
		keystroke "," using command down
		-- delay 2
	end tell
end tell

that’s the really bizarre thing; it opens the first time I run the scriplet in ScriptEditor. But after that, nothing happens.

Hello.

If that is how it works for you, then it is bizarre. Was the script still open in the script editor? if it was, then please re-save it again, and close its script edtior window. :slight_smile:

Safari’s UI is not the same under Mavericks and under Yosemite.

Here is a version tested under Yosemite.
Under Mavericks it does what McUsr wrote.
Would be fine if somebody wrote what the log instructions returned under this “old” OS.

-- Toggle Safari Extensions
-- http://macscripter.net/viewtopic.php?pid=179568#p179568
-- http://macscripter.net/viewtopic.php?id=43326 ( Yvan Koenig how to)

set isYosemite to (system attribute "sys2") ≥ 10
log result

tell application "Safari" to activate
open location "http://macscripter.net" # ADDED
try
	tell application id "sevs"
		tell process "Safari"
			set frontmost to true # REQUIRED under Yosemite, don't hurt with older OS !
			keystroke "," using command down
			-- delay 0.5
			log (get title of buttons of toolbar 1 of window 1)
			--> (*Général, Onglets, Rempl. auto., Mots de passe, Recherche, Sécurité, Confidentialité, Notifications, Extensions, Avancées*)
			if isYosemite then
				# Here if running Yosemite
				click button 9 of toolbar 1 of window 1
				tell scroll area 1 of group 1 of group 1 of window 1
					log (get class of UI elements) --> {UI element}
					tell UI element 1
						log (get class of UI elements)
						--> {group, group, list, group}
						tell group 2
							log (get class of UI elements)
							--> (*group, checkbox, group*)
							log (get value of checkbox 1)
							--> 0 = extensions disabled
							--> 1 = extensions enabled
							click checkbox 0
						end tell
					end tell
				end tell
			else
				# Here if running Mavericks or older
				tell button 8 of toolbar 1 of window 1 to click
				tell scroll area 1 of group 1 of group 1 of window 1
					log (get class of UI elements) --> {UI element}
					tell UI element 1
						log (get class of UI elements)
						--> {?}
						tell UI element 2 -- EDITED, I WRONGLY WROTE 1
							log (get class of UI elements)
							--> (*?*)
							log (get value of checkbox 1)
							--> 0 = extensions disabled
							--> 1 = extensions enabled
							click checkbox 1
						end tell
					end tell
				end tell
			end if
		end tell
	end tell
on error e number n
	tell application (path to frontmost application as text)
		display alert "UIScripting not enabled?" message e & " : " & n
	end tell
end try
tell application "Safari" to close window 1
(*
If it errors, then you should see to that Safari is among the applications that
are eligible for using assistive devices. (That it is enabled under the accessibility 
tab of the Privacy settings of System Preferences.)
*)

Yvan KOENIG (VALLAURIS, France) vendredi 8 mai 2015 17:54:33

Thanks Yvan.

I’m not running Yosemite, so I didn’t spot the difference. :slight_smile:

Hello McUsr

May you tell what is returned by the two log instructions where I put a question mark ?

Yvan KOENIG (VALLAURIS, France) vendredi 8 mai 2015 19:54:40

Hello Yvan.

First time I ran your script, I got an error message:

As for your questions: I got group, group, list, group, group returned for the first tagged log statement, and static text for the second one. The script fails on the line:

						log (get value of checkbox 1)

Oops

I forgot to change an index.

Now the script is corrected.

Yvan KOENIG (VALLAURIS, France) samedi 9 mai 2015 12:20:06

Hello Yvan,

You’ve helped me before with Numbers scripting and I appreciate your lending a hand here, too.

First of all, can you tell me how I can view the script log, so I can send you feedback from my Yosemite system? In Console?

The first time I ran the script I got the same UIScripting error. So, I deleted Safari from the SysPrefs>Privacy>Accessibility window where it had been and then added it anew. That fixed that problem.

Then, on running, the script opened Safari preferences, toggled the Extensions switch from ON → OFF and then closed the Safari browser window. I changed the script to comment out the close window part.

But, let me explain why I need the script in the first place, so we don’t have any crossed wires.
For some strange reason, when I login to my system and Safari automatically starts up, the Extensions are switched ON but not loaded. AdBlock, for instance, does not block ads or popups. So, I have to manually go in and switch the Extensions OFF and then ON again.

That’s what I’d like the script to do.

Thanks,
Pato

Hello

When a script is open in the editor, at the very bottom of the window we may see three buttons.
(1) the ⓘ one opens the pane entitled [Description] in french
(2) the ↲ one opens the pane entitled [Résultats] always in french
(3) the ⌸ one opens the pane entitled [Résultats Messages Événements Réponses] which is the one in which I read the [Historique]

There is also a menu item in the Windows menu (Fenêtre] in french
Named [Historique], it’s the one whose shortcut is : ⌥⌘L

Yvan KOENIG (VALLAURIS, France) mercredi 13 mai 2015 16:10:25

Got it! Was using Scriptlet app and thought I’d read it in Console or something.

OK, here’s what happened when I ran the script:

An error message pops up reading “System Events got an error: Can’t get window 1 of process “Safari”. Invalid index. : -1719”

The Preferences window opens to the Extensions pane, however, the switch is not toggled and nothing else happens.

The log in Applescript returns:

tell current application
system attribute “sys2”
→ 10
end tell
tell application “Safari”
activate
end tell
tell application “System Events”
set frontmost of process “Safari” to true
keystroke “,” using command down
get title of every button of toolbar 1 of window 1 of process “Safari”
→ error number -1719 from window 1 of process “Safari”
end tell
tell current application
path to frontmost application as text
→ “Yosemite:Applications:Safari.app:”
end tell
tell application “Safari”
display alert “UIScripting not enabled?” message “System Events got an error: Can’t get window 1 of process "Safari". Invalid index. : -1719”
→ {button returned:“OK”}
end tell
Result:
{button returned:“OK”}

Pato

I gave no special permission, in fact I just loaded the script from my message in this thread.
I just added the instruction log result just after : set isYosemite to (system attribute “sys2”) ≥10

The events log was :

tell current application
	system attribute "sys2"
	(*true*)
end tell
tell application "Safari"
	activate
end tell
tell application "System Events"
	set frontmost of process "Safari" to true
	keystroke "," using command down
	get title of every button of toolbar 1 of window 1 of process "Safari"
	(*Général, Onglets, Rempl. auto., Mots de passe, Recherche, Sécurité, Confidentialité, Notifications, Extensions*)
	click button 9 of toolbar 1 of window 1 of process "Safari"
	get class of every UI element of scroll area 1 of group 1 of group 1 of window 1 of process "Safari"
	(*UI element*)
	get class of every UI element of UI element 1 of scroll area 1 of group 1 of group 1 of window 1 of process "Safari"
	(*group, group, list, group*)
	get class of every UI element of group 2 of UI element 1 of scroll area 1 of group 1 of group 1 of window 1 of process "Safari"
	(*group, checkbox, group*)
	get value of checkbox 1 of group 2 of UI element 1 of scroll area 1 of group 1 of group 1 of window 1 of process "Safari"
	(*1*)
	click checkbox 1 of group 2 of UI element 1 of scroll area 1 of group 1 of group 1 of window 1 of process "Safari"
end tell
tell application "Safari"
	close window 1
end tell

Yvan KOENIG (VALLAURIS, France) vendredi 15 mai 2015 19:32:22

Bingo, I was able to reproduce what you described : I got it when no window was open in Safari.
I just added an instruction opening a window and all behaved well.
I missed that because there is always at least one Safari window open on my machine.

I edited the script in my old message.