如何使用命令行启动蓝牙网络共享?


8

我想要一种快速的方法来开始使用iPhone进行网络共享,希望只是使用键盘。使用蓝牙菜单,我可以在设备子菜单中选择“连接到网络”选项,但是可以自动执行此操作吗?

最终,我想将其分配给Alfred.app中的一个快捷方式,但是使用命令行或AppleScript进行的任何操作都可以。

这可能吗?谢谢!!

Answers:


3

看起来没有直接的AppleScript字典可用于以这种方式使用蓝牙。

不过,您可以使用GUI脚本,它基本上使用Mac OS的辅助功能来选择菜单项等。

MacOSAutomation.com上提供了有关如何以GUI AppleScript 开头的出色文章

如果事物列表不断变化,GUI自动化可能会很困难,但是如果您通常连接的蓝牙项目列表保持不变,那么您就可以了。

然后,您可以通过Alfred调用此AppleScript。


1

http://macscripter.net/viewtopic.php?id=38559

在上面的链接中,有一个非常不错的脚本,我刚刚对其进行了更新。请注意,它使用blueutil [ git repo ] [ 网站/二进制文件 ]。

启用蓝牙:

-- Enable Bluetooth and Connect to iPhone

property blueutilPath : "/opt/local/bin/blueutil"

-- Turn on bluetooth.
if execBlueutil("status") contains "Status: off" then
    execBlueutil("on")

    connectDevice()

    doGrowl()

end if

on execBlueutil(command)
    set res to do shell script blueutilPath & " " & command
    if res contains "Error" then
        display dialog res
        quit
    end if
    return res
end execBlueutil

-- Connect Device
on connectDevice()
    tell application "System Preferences"
        activate
        set AppleScript's text item delimiters to "."
        set current pane to pane "com.apple.preference.network"
        set winNetwork to "Network"
        set btooth to "Bluetooth"

        tell application "System Events" to tell process "System Preferences"
            set theRow to row 1 of table 1 of scroll area 1 of window winNetwork whose value of static text 1 contains btooth
            select theRow --clicks the bluetooth row
            --If Bluetooth is already connected, the button will say Disconnect, so we don't want to turn it off:
            try
                click (button 1 of group 1 of window winNetwork whose title is "Connect")
            end try
        end tell
        tell application "System Preferences"
            quit
        end tell
    end tell
end connectDevice

on doGrowl()
    tell application "System Events"
        set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
    end tell
    if isRunning then
        tell application id "com.Growl.GrowlHelperApp"
            set the allNotificationsList to ¬
                {"Bluetooth Setting"}
            set the enabledNotificationsList to ¬
                {"Bluetooth Setting"}
            register as application ¬
                "AppleScript - Bluetooth" all notifications allNotificationsList ¬
                default notifications enabledNotificationsList

            notify with name ¬
                "Bluetooth Setting" title ¬
                "Bluetooth is On & iPhone Connected" description ¬
                "Bluetooth has been enabled with iPhone tethered." application name "AppleScript - Bluetooth" icon of file (path to me)
        end tell
    end if
end doGrowl

禁用蓝牙:

property blueutilPath : "/opt/local/bin/blueutil"

-- Turn off Bluetooth.
if execBlueutil("status") contains "Status: on" then
    execBlueutil("off")

    doGrowl()
end if
on execBlueutil(command)
    set res to do shell script blueutilPath & " " & command
    if res contains "Error" then
        display dialog res
        quit
    end if
    return res
end execBlueutil

on doGrowl()
    tell application "System Events"
        set isRunning to (count of (every process whose bundle identifier is "com.Growl.GrowlHelperApp")) > 0
    end tell
    if isRunning then
        tell application id "com.Growl.GrowlHelperApp"
            set the allNotificationsList to ¬
                {"Bluetooth Setting"}
            set the enabledNotificationsList to ¬
                {"Bluetooth Setting"}
            register as application ¬
                "AppleScript - Bluetooth" all notifications allNotificationsList ¬
                default notifications enabledNotificationsList

            notify with name ¬
                "Bluetooth Setting" title ¬
                "Bluetooth Off" description ¬
                "Bluetooth has been disabled." application name "AppleScript - Bluetooth" icon of file (path to me)
        end tell
    end if
end doGrowl

0

我建议为此使用automator,您可以从Alfred调用它以使其变得简单:)

我当前的自动化程序工作流程是这样的:

Click the "bluetooth" menu bar item.
Connect to Network

使用这种方法,您可以在一分钟内自动完成几乎所有操作

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.