Toggle Web Sharing

。_饼干妹妹 提交于 2019-12-24 09:49:21

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!