5

I'm seeking a programmatic method for disabling all the sharing services on a Mac. My preference is for an AppleScript or a shell script.

I'd like to have the services shown in the picture below disabled when I execute the script. The solution should be compatible with Alfred, Keyboard Maestro or any other global productivity tool.

The services I'd like to disable are:

Sharing options

I could come up with the following with some googling:

File sharing (on/off)

do shell script "/usr/sbin/AppleFileServer" password "x" with administrator privileges
do shell script "/usr/bin/killall AppleFileServer" password "x" with administrator privileges

Internet Sharing (on/off)

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist        

(I would prefer terminal commands or batch scripts to apple scripts. Here's an apple script solution for those interested.)

4
  • Would you accept an app that does this sort of change automatically based on your location settings? ControlPlane and an alt link because his site seems to be down today...
    – Ian C.
    Commented Feb 19, 2012 at 20:25
  • Thanks Ian, but would prefer to have this done purely with built in commands.
    – KG -
    Commented Feb 20, 2012 at 2:40
  • 2
    To be clear, you want these services off with minimal side effects, right? For instance sudo shutdown -h now will disable the services, but it will have other effects as well.
    – Daniel
    Commented Feb 21, 2012 at 19:55
  • @Daniel : yup that is correct. I basically want the programmatic/terminal command equivalent of going to System Preferences and unchecking the sharing options
    – KG -
    Commented Feb 22, 2012 at 7:43

2 Answers 2

6
+50

Here's a shell script that turns off the services, but I'm working on finding a more direct solution:

#!/bin/sh
osascript << HERE
tell application "System Preferences" to activate
tell application "System Events" to tell process "System Preferences"
    click menu item "Sharing" of menu "View" of menu bar 1
    delay 2
    tell window "Sharing" to repeat with x from 1 to 11
        if value of checkbox 1 of row x of table 1 of scroll area 1 of group 1 is 1 then click checkbox 1 of row x of table 1 of scroll area 1 of group 1
    end repeat
end tell
tell application "System Preferences" to quit
HERE
3
  • Thanks Daniel. This is helpful, but will definitely wait for your direct solution. That would be ideal! Cheers.
    – KG -
    Commented Feb 23, 2012 at 6:05
  • I recognize that it would be ideal, and I hope I can come up with it :-) -- but I can't promise I'll be successful.
    – Daniel
    Commented Feb 23, 2012 at 11:35
  • Glad you're trying. You look like someone who can come up with the solution :) . Marking your answer cause it's the best available solution at the moment and you should get the cred on this. Do try the direct solution as well. Cheers.
    – KG -
    Commented Feb 24, 2012 at 3:01
2

Don't forget you can simply execute your AppleScript at the command-line using the osascript tool rather than embedding it <<EOF style within the script as Daniel suggests.

That means you can reuse the script you know already works with a one line shell script:

osascript ChangeSettings.scpt

(assuming ChangeSettings.scpt is the file in the current working directory containing the AppleScript you mentioned)

3
  • Intriguing - using a shell script to call the apple script. And since the OP is asking for user level scripting, this should work just fine for to reuse the Apple Script. It may not be the OP's choice unless they were unaware of the ability to reuse the chosen AppleScript but launch from the shell.
    – bmike
    Commented Feb 22, 2012 at 15:19
  • But be aware that the specific script linked in the question doesn't exactly do what the OP requested
    – Daniel
    Commented Feb 22, 2012 at 19:54
  • @Kit Thanks. didn't know about osascript. good stuff. My main reason for requesting a "direct" terminal/bash command over an Apple Script is purely for performance purposes. Running a command over terminal like the ones posted in my question is definitely faster than the equivalent UI AppleScripting. So your point is well taken, but I would prefer to have the shell perform the direct command, rather than acting as a shell for an AppleScript call. Cheers.
    – KG -
    Commented Feb 23, 2012 at 6:09

You must log in to answer this question.

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