Script to enable/disable pop up blocker in Safari or perhaps to add a URL exception

Snickasaurus
Contributor

I've done a fair amount of googling and came up with

To Disable the blocker
defaults write com.apple.safari WebKitJavaScriptCanOpenWindowsAutomatically 1
To Enable the blocker
defaults write com.apple.safari WebKitJavaScriptCanOpenWindowsAutomatically 0

But it doesn't seem to be effective on Safari version 6.0.5 and the website I found it on was for an older version of OS X. I'm usually pretty darn good at figuring things out but this has me pulling my hair out. Rarely do I seek out help but this time I'm REALLY hoping someone can throw me a rope here.

Any thoughts as to how I can do this so I can script it and push it out?

Thanks in advance.

13 REPLIES 13

bentoms
Release Candidate Programs Tester

Are you the nick that replied to my site?

http://macmule.com/2012/07/31/disabling-safari-5-1-xs-6-xs-pop-up-blocker-from-terminal-2/

Sorry if so, I need to update that more!

Is this better?

Display Name:   Safari 5.1+ Disable Pop-Up Blocker      
Application Domain: com.apple.Safari        
Key Name:   com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptCanOpenWindowsAutomatically        
Key Type:   boolean     
Key Value:  true

Snickasaurus
Contributor

Thanks for the reply! I can't check your site (though I've been there before) from our internal network but I'll have a look at it when I can throw a spare MacBook on our customer wlan.

I ran it like so in terminal:

defaults write com.apple.Safari com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptCanOpenWindowsAutomatically -bool YES (per your site)

but above you put the key value to true so do I have to end that line with "TRUE" instead of "YES"?

Also is it supposed to show the "Block pop-up windows" unchecked? I'm running the command with Safari closed of course but when I open it and view Preferences it's still showing the box is checked. Just wondering. Again thanks for your response and help!!

bentoms
Release Candidate Programs Tester

Sorry. Typo in blog.

Should be true as above & as I put in comments.

Amended the blog now!

Above is copied from my JSS.

aaron_kelley
Contributor

I don't mean to bring up old comments, but i'm trying to run this as a Login/Recurring checkin policy to push out to teacher MacBooks and I can't get it to work. The policy runs, the script runs, and the policy log states below....
STEP 1 of 4]
Executing Policy Disable Pop-Up Blocker in Safari for Teachers
[STEP 2 of 4]
Running script Disable Pop-Up Blocker in Safari...
Script exit code: 0
Script result:
[STEP 3 of 4]
[STEP 4 of 4]

what am I doing wrong? I've tried to use SUDO in the script line as well. The script works completely fine when I open terminal directly on the device. Is the script in JSS not writing to the user level like if you were to open terminal?

RobertHammen
Valued Contributor II

@aaron.kelley Scripts in Jamf run as the root user, not the currently-logged-in user, so you're setting that preference for the root user. You may need to do some scripting wizardry to determine the logged-in user and run the commands as them...

aaron_kelley
Contributor

@RobertHammen I figured as much, but not sure how to do that, as i'm new to scripting.

aaron_kelley
Contributor

Any ideas? I'm still stumped on this.

millersc
Valued Contributor

@aaron.kelley Give this a quick test...

#!/bin/bash

#Casper User variable
USER=$3

#Path to User's Safari plist
theFile=/Users/$USER/Library/Preferences/com.apple.Safari.plist

defaults write $theFile  com.apple.Safari.ContentPageGroupIdentifier.WebKit2JavaScriptCanOpenWindowsAutomatically -bool true

aaron_kelley
Contributor

@millersc that didn't work :/

millersc
Valued Contributor

Did it give any error? @aaron.kelley Also, I just noticed there is 2 spaces before "com.apple.Safari...." fix that and see again.

aaron_kelley
Contributor

@millersc It did not. It says it completed. I did noticed the 2 spaces and fixed it and tried with same results. Now when I try just the defaults write in terminal, it's saying "could not write domain /Users/aaron.kelley/Library/Preferences/com.apple.Safari; exiting"

aaron_kelley
Contributor

So for some reason my plist file is no longer in the ~/Library/Preferences folder...hmm.

EDIT: I copied it from my virtual testing machine. Plist is there again.

aaron_kelley
Contributor

FYI, found the solution here. https://www.jamf.com/jamf-nation/discussions/22170/script-to-disable-pop-up-blocker-in-safari