2010-04-24 20 views
7

Tôi không có bộ định tuyến Wi-Fi, vì vậy khi ở nhà tôi cần chuyển máy tính xách tay của mình thành nguồn Wi-Fi để cả bản thân và đối tác của tôi có thể truy cập internet .Cách khởi động/ngừng chia sẻ Internet bằng AppleScript

Tuy nhiên trong những ngày tôi làm việc tại một quán cà phê và yêu cầu sử dụng Wi-Fi của họ.

Tôi đang chạy Snow Leopard và tôi thấy nó cồng kềnh rườm rà để liên tục bị tắt và bật, Internet Sharing đầu tiên và sau đó là Wi-Fi của tôi.

Bất kỳ ý tưởng nào về giải pháp AppleScript 'n' bẩn nhanh chóng?

Trả lời

6

Bạn có thể sử dụng launchctl để bắt đầu hoặc dừng dịch vụ Internet Chia sẻ theo chương trình.

Các AppleScript sau đây sẽ khởi động Internet Sharing:

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

Sau đây AppleScript sẽ ngừng Internet Sharing:

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

Tôi đang sử dụng AppleScript này từ Automator để tôi có thể dễ dàng sử dụng nó như là một dịch vụ và cung cấp cho nó một phím tắt.

Chuyển đổi Internet Sharing:

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 

Tôi cross-posting này trên stack trao đổi của Apple vì câu hỏi được hỏi ở cả hai nơi.

+0

+1 cho giải pháp phức tạp nhưng dễ sử dụng! cảm ơn! – pille

1

Không chắc chắn nếu bạn vẫn đang tìm kiếm một giải pháp nhưng ... đây là một kịch bản táo để kích hoạt hoặc vô hiệu hóa việc chia sẻ internet

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 

tôi cũng sẽ đăng bài này trên sàn giao dịch bưu táo stack.

Các vấn đề liên quan