切换网络共享 [英] Toggle Web Sharing
问题描述
我正在寻找一个 Applescript 来在 Snow Leopard 中切换网络共享.我试过这个但它没有禁用,只是在我再次运行时重新启动它.或者一个 shell 命令,只要我能把它变成一个 Quicksilver 动作.这就是我的最终目标.非常感谢!
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!
推荐答案
您可以使用以下 shell 脚本来切换 Mac OS X 服务的启用状态:
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
该脚本使用 service 命令来确定服务是否开启和然后通过调用 launchctl 切换其状态.
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
要通过 AppleScript 调用 shell 脚本,您可以使用 执行shell脚本命令:
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
使用password
参数避免被提示.
这篇关于切换网络共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!