tags:

views:

29

answers:

1

I'm looking for an Applescript to toggle Web Sharing in Snow Leopard. I tried this but it doesn't disable, just restarts it when I run it again. Or a shell command as long as I can turn it into a Quicksilver action. That's my end goal. Thanks so much!

A: 

You can use the following shell script to toggle the enabled state of a Mac OS X service:

#!/bin/sh
# toggle OS X service

if [ "$#" -ne "1" ]
then
    echo 1>&2 Usage: `basename $0` service
    echo 1>&2 Toggle the enabled state of the given service.
    exit 2
fi

SERVICE_NAME=$1
SERVICE_PLIST=/System/Library/LaunchDaemons/$SERVICE_NAME.plist

if [ ! -f "$SERVICE_PLIST" ]
then
    echo 1>&2 Service $SERVICE_NAME is not available.
    exit 1
fi

/sbin/service --test-if-configured-on "$SERVICE_NAME"
if [ $? -eq 0 ]
then
    /bin/launchctl unload -w "$SERVICE_PLIST"
else
    /bin/launchctl load -w "$SERVICE_PLIST"
fi

The script uses the service command to determine if the service is on and then toggles its state by invoking launchctl.

The name of the service has to passed as the only argument. To toggle web sharing run:

sudo toggle_service.sh org.apache.httpd

To invoke the shell script via AppleScript you can use the do shell script command:

do shell script "toggle_service.sh org.apache.httpd" password "pwd" with administrator privileges

Use the password parameter to avoid being prompted.

sakra
Is there a way I can save my password in the applescript so I don't have to enter it in the command? I want to use this with Quicksilver and set a keyboard shortcut to it.
Marc
@Marc see my updated answer.
sakra
Thanks so much!
Marc
So I tried to compile this and I got this: http://cl.ly/2LJH -> http://cl.ly/2Lgv -> http://cl.ly/2LlE
Marc
You are missing the "do shell script" part. Copy the command exactly as it is shown in code section.
sakra