Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

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.)

share|improve this question
    
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. Feb 19 '12 at 20:25
    
Thanks Ian, but would prefer to have this done purely with built in commands. –  Kaushik Gopal Feb 20 '12 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 Lawson Feb 21 '12 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 –  Kaushik Gopal Feb 22 '12 at 7:43

2 Answers 2

up vote 5 down vote accepted
+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
share|improve this answer
    
Thanks Daniel. This is helpful, but will definitely wait for your direct solution. That would be ideal! Cheers. –  Kaushik Gopal Feb 23 '12 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 Lawson Feb 23 '12 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. –  Kaushik Gopal Feb 24 '12 at 3:01

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)

share|improve this answer
    
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 Feb 22 '12 at 15:19
    
But be aware that the specific script linked in the question doesn't exactly do what the OP requested –  Daniel Lawson Feb 22 '12 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. –  Kaushik Gopal Feb 23 '12 at 6:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.