Apple脚本,用于设置鼠标首选项的主按钮,辅助按钮和其他属性

时间:2010-08-09 11:53:57

标签: applescript

有人可以建议我:

  

如何设置主按钮,辅助   鼠标的按钮和其他属性   通过AppleScript优惠?

我现在尝试了这个,脚本:

tell application "System Preferences"
    activate
end tell
tell application "System Events"
    tell process "System Preferences"
        click menu item "Mouse" of menu "View" of menu bar 1
        tell window "Mouse"
            set value of pop up button 1 to "Primary Button"
        end tell
    end tell
end tell

但它提供了此错误消息:

  

系统事件出错:无法获取   弹出窗口“鼠标”的按钮1   处理“系统偏好”。无效   索引。

任何人都可以告诉我可能出错的地方或实施它的更好方法吗?

谢谢,

Miraaj

4 个答案:

答案 0 :(得分:2)

这是我的版本,它消除了对Christopher Kemp上面的帖子所示的“键盘技巧”的需要。此外,还有一些额外的东西,例如运行检查以确保辅助设备设置为“打开”,以及一个对话框,以便您可以选择是将鼠标设置为左手还是右手。希望它有所帮助:)

P.S。这是10.6(Snow Leopard)......你必须稍微改变它以适应10.5(Leopard)偏好窗格的差异。

-

# check 'Enable access for assistive devices' is ON

tell application "System Events"

if UI elements enabled then

    # open dialog
    set question to display dialog "Select your mouse preference." buttons {"Left Handed", "Right Handed", "Cancel"} default button 3 with title "Mouse Switch"

    set answer to button returned of question

    if answer is equal to "Left Handed" then

        # open Mouse preferences & set left button to Secondary
        tell application "System Preferences"

            activate
            reveal pane "Mouse"

        end tell

        tell application "System Events"

            tell process "System Preferences"

                # set Secondary Button
                click pop up button 4 of group 1 of window "Mouse"
                click menu item "Secondary Button" of menu of pop up button 4 of group 1 of window "Mouse"
                # set Primary Button
                click pop up button 5 of group 1 of window "Mouse"
                click menu item "Primary Button" of menu of pop up button 5 of group 1 of window "Mouse"

            end tell

        end tell

        tell application "System Preferences" to quit

        display dialog "Your mouse is now set to: Left Handed" buttons {"OK"}

    end if

    if answer is equal to "Right Handed" then

        # open Mouse preferences & set right button to Secondary
        tell application "System Preferences"

            activate
            reveal pane "Mouse"

        end tell

        tell application "System Events"

            tell process "System Preferences"

                # set Secondary Button
                click pop up button 5 of group 1 of window "Mouse"
                click menu item "Secondary Button" of menu of pop up button 5 of group 1 of window "Mouse"
                # set Primary Button
                click pop up button 4 of group 1 of window "Mouse"
                click menu item "Primary Button" of menu of pop up button 4 of group 1 of window "Mouse"

            end tell

        end tell

        tell application "System Preferences" to quit

        display dialog "Your mouse is now set to: Right Handed" buttons {"OK"}

    end if

    # if 'Enable access for assistive devices' is OFF show error
else

    display dialog "Please select 'Enable access for assistive devices' from the Universal Access preference pane to run this script." buttons {"OK"} default button 1 with icon caution

end if

end tell

答案 1 :(得分:1)

转到系统首选项,通用访问窗格,选择“启用辅助设备访问”

这需要在将使用GUI脚本

作为脚本运行的mac上启用

修改

既然你已经这样做了,你可以更改你的脚本,因为它有错误,让你的脚本单击单选按钮

  tell application "System Preferences"
   activate
   set current pane to first pane whose name is "Mouse"
  end tell

  tell application "System Events"
   tell process "System Preferences"
    try
     click radio button "Left" of every radio group of window "Mouse"
    on error theError
     --An error occured
     display dialog ("Sorry, an error occured while altering Keyboard and Mouse settings:" & return & theError) buttons "OK" default button "OK"
    end try
   end tell
  end tell

附加信息

查看Scriptable System Preferences您可能会发现该主题更有帮助

答案 2 :(得分:1)

您需要完成该错误消息所说的内容。在“系统偏好设置”中的“通用访问”下查看,您将看到“启用辅助设备访问”复选框。我将把它作为练习留给你解决如何通过Applescript启用它。


请发布所有相关代码。有很多理由可能不起作用。 Universal Access窗格可能未处于活动状态,可能无法通过Applescript访问按钮或类型(后者很可能)。

答案 3 :(得分:1)

我让这个工作:

# open Mouse preferences & set right button to Secondary

tell application "System Preferences"
    activate
    set current pane to first pane whose name is "Mouse"
end tell
tell application "System Events"
    tell process "System Preferences"

            # activates drop-down menu
        click pop up button 5 of group 1 of window "Mouse"

            # page up key, to ensure we're starting at the top of the list
        key code 116

            # down arrow, to select Secondary Button from list
        key code 125

            # Return key to make selection
        key code 36

        delay 1
        click button "Show All" of group 1 of group 2 of tool bar 1 of window 1
    end tell
end tell

延迟命令不是必需的,只是这样您就可以直观地确认您的选择。 “设置”价值而不是键盘技巧会更好,但至少可以胜任。