问题
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!
回答1:
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.
来源:https://stackoverflow.com/questions/3683263/toggle-web-sharing