2

I am using default write to set/unset Block all incoming connections option of OSX firewall.

sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 2
sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 1

I realised that the option is not set if the preference panel is open.
Is there a way avoid this behaviour, possibly refreshing the Preference Panel UI after setting the new value?

1

Depends on whether you want to stick with shell, or use applescript. This applescript script gives you a popup for password:

do shell script "defaults write /Library/Preferences/com.apple.alf globalstate -int SomeInteger" with administrator privileges
tell application "System Events" to if (first application process whose name is "System Preferences") exists then
    tell application "System Preferences"
        quit
        delay 1
        activate
        set current pane to pane "com.apple.preference.security"
    end tell
    tell application process "System Preferences" to tell window 1 to tell tab group 1 to tell radio button "Firewall" to perform action "AXPress"
end if

In Terminal, it looks messy:

sudo defaults write /Library/Preferences/com.apple.alf globalstate -int SomeInteger; osascript -e 'tell application "System Events" to if (first application process whose name is "System Preferences") exists then' -e 'tell application "System Preferences"' -e 'quit' -e 'delay 1' -e 'activate' -e 'set current pane to pane "com.apple.preference.security"' -e 'end tell' -e 'tell application process "System Preferences" to tell window 1 to tell tab group 1 to tell radio button "Firewall" to perform action "AXPress"' -e 'end if'

Replace SomeInteger with a value.

Update:

set firewallState to do shell script "defaults read /Library/Preferences/com.apple.alf globalstate"

if firewallState is "1" then
    tell application "System Events" to display notification with title "Switching Firewall from This to That"
    set newState to "2"
end if

if firewallState is "2" then
    tell application "System Events" to display notification with title "Switching Firewall from That to This"
    set newState to "1"
end if

do shell script ("defaults write /Library/Preferences/com.apple.alf globalstate -int " & newState) with administrator privileges

tell application "System Events" to if (first application process whose name is "System Preferences") exists then
    tell application "System Preferences"
        quit
        delay 1
        activate
        set current pane to pane "com.apple.preference.security"
    end tell
    tell application process "System Preferences" to tell window 1 to tell tab group 1 to tell radio button "Firewall" to perform action "AXPress"
end if
  • Interesting. But I noticed you must guarantee Automator access to Accessibility to reopen the Preferences Panel: is this necessary? – pasine May 11 '15 at 15:33
  • @pasine Yes, It's called GUI Scripting. You effectively allow the script to takeover cursor and perform actions for you. I forgot the part where you said "the option is not set if the preference panel is open". Sorry. It code has to be updated to overcome that. Maybe later. – fartheraway May 11 '15 at 15:43
  • I think your solution is better than mine. But I don't like the idea to quit the preferences panel without alerting the user, so I added a dialog box before closing it and applying the option: what do you think? gist.github.com/pasine/2361dbe0c147698ca926 – pasine May 11 '15 at 16:13
  • 1
    @pasine in the spirit of convivial one-upmanship, see update. – fartheraway May 11 '15 at 16:44
  • Nice, check my new version. I prefer to check that the option has been set correctly before notify to the user. – pasine May 11 '15 at 17:32

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .