如何使用AppleScript启动/停止Internet共享

时间:2010-04-24 15:00:35

标签: applescript

我没有Wi-Fi路由器,因此在家时我需要将笔记本电脑变成Wi-Fi信号源,这样我和我的伙伴都可以访问互联网。

然而,在我在咖啡店工作并需要使用Wi-Fi的日子里。

我正在运行Snow Leopard,我发现不断关闭和开启,首先是Internet共享,然后是我的Wi-Fi,这非常麻烦。

快速'n'脏AppleScript解决方案的想法?

4 个答案:

答案 0 :(得分:6)

您可以使用launchctl以编程方式启动或停止Internet共享服务。

以下AppleScript将启动Internet共享:

do shell script "/bin/launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges

以下AppleScript将停止互联网共享:

do shell script "/bin/launchctl unload -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges

答案 1 :(得分:4)

我正在使用Automator中的AppleScript,以便我可以轻松地将其用作服务并为其提供键盘快捷键。

切换互联网共享:

register_growl()

try
    if isRunning("InternetSharing") then
        do shell script "launchctl unload -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges

        if isRunning("InternetSharing") then
            error "Internet Connection Sharing was Not Disabled"
        else
            my growlnote("Success", "Internet Connection Sharing Disabled")
        end if

    else
        do shell script "launchctl load -w /System/Library/LaunchDaemons/com.apple.InternetSharing.plist" with administrator privileges

        if isRunning("InternetSharing") then
            my growlnote("Success", "Internet Connection Sharing Enabled")
        else
            error "Internet Connection Sharing was Not Enabled"
        end if

    end if

on error errMsg
    my growlnote("Error", errMsg)

end try

on isRunning(processName)
    try
        return 0 < length of (do shell script "ps ax | grep -v grep | grep " & processName)
    on error
        return false
    end try
end isRunning

on register_growl()
    try
        tell application "GrowlHelperApp"
            set the notificationsList to {"Success", "Warning", "Error"}
            register as application "Toggle Internet Connection Sharing" all notifications notificationsList default notifications notificationsList icon of application "Sharing"
        end tell
    end try
end register_growl

on growlnote(growltype, str)
    try
        tell application "GrowlHelperApp"
            notify with name growltype title growltype description str application name "Toggle Internet Connection Sharing"
        end tell
    end try
end growlnote

我在Apple堆栈交换机上cross-posting,因为这两个地方都提出了问题。

答案 2 :(得分:1)

不确定您是否还在寻找解决方案但是......这是一个启用或禁用互联网共享的苹果脚本

tell application "System Preferences"
    activate
    reveal (pane id "com.apple.preferences.sharing")
end tell

tell application "System Events"
    tell process "System Preferences"
        try
            click checkbox of row 11 of table 1 of scroll area of group 1 of window "Sharing"

            if checkbox of row 11 of table 1 of scroll area of group 1 of window "Sharing" is equal to 1 then
                repeat until sheet of window 1 exists
                    delay 0.5
                end repeat

            end if

            if (sheet of window 1 exists) then
                click button "Start" of sheet of window 1

            end if

            tell application "System Preferences" to quit
            activate (display dialog "Internet Sharing preferences sucessfully flipped")

        on error

            activate
            display dialog "something went wrong in automation but you are probably in the right menu..."
            return false
        end try

    end tell

end tell

我也会在苹果堆栈交换帖子上发布此内容。

答案 3 :(得分:0)

这就是我想让Mojave使用(主要是)可访问性切换Internet共享的方法-不幸的是,涉及使用launchctl和/或编辑com.apple.nat.plist的解决方案对我都不起作用。

open location "x-apple.systempreferences:com.apple.preferences.sharing?Internet"
tell application "System Events"
    tell process "System Preferences"
        repeat until window "Sharing" exists
            delay 0.1
        end repeat
        tell window "Sharing"
            set _row to group 1's scroll area 1's table 1's first row whose selected is true
            set _wasSharing to _row's checkbox's value as number
            if _wasSharing is 1 then
                click _row's checkbox
                set _wasSharing to _row's checkbox's value as number
                repeat until _wasSharing is 0
                    delay 0.1
                end repeat
            end if
            if _wasSharing is 0 then
                click _row's checkbox
                repeat until sheet 1 exists
                    delay 0.1
                end repeat
                click sheet 1's button "Start"
            end if
        end tell
    end tell
end tell
tell application "System Preferences" to quit