0

我似乎无法弄清楚如何使用 Applescript 选择正确的按钮。我开始学习 vim 并希望能够在 Caps Lock 和 Ctrl 之间切换 Caps Lock 键。我已经标记了我需要完成的步骤。

我找到了(这篇文章),但它似乎有点 hacky。也许这是它应该的方式,但它显示了系统偏好。每次我使用它时的窗口,不像(此代码)切换fn键并且工作无缝。

有人可以提供一些建议吗?

这是我的代码:

tell application "System Events"
    tell application "System Preferences"
        reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
    end tell

    -- [STEP 1] set mod_keys to value of output from within "Modifier Keys..."
    set mod_keys to button "Modifier Keys..." of tab group 1 of window 1 of application process "System Preferences"

    -- I would prefer not to have to click the mod_keys because I don't want the window popping up but if it's necessary then okay
    click mod_keys

    -- [STEP 2] set cl_key to the second dropdown of mod_keys
    set cl_key to menu item 2 of menu 1 of pop up button 4

    set cl to value of cl_key
    if cl is menu item 2
        set q to menu item 2 of menu 1 of pop up button 4
    else
        set q to menu item 1 of menu 1 of pop up button 4
    end if

end tell

-- This is just to make sure it works, but may be unneccessary
if application "System Preferences" is running then
    tell application "System Preferences" to quit
end if

return q

这是修改器键屏幕截图:

在此处输入图像描述

在此处输入链接描述 http://imageshack.us/a/img833/474/o5co.png

4

2 回答 2

1

这会切换键(您必须更改德语标签):

    tell application "System Events"
tell application "System Preferences"
    reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"
end tell

tell window 1 of application process "System Preferences"
    click button "Sondertasten …" of tab group 1

    tell sheet 1
        tell pop up button "Feststelltaste (⇪):"

            set state to value
            click
            delay 0.2
            if "Feststelltaste" is in state then
                click menu item "⌃ ctrl-Taste" of menu 1
            else
                click menu item "⇪ Feststelltaste" of menu 1
            end if
            delay 0.2
        end tell
        click button "OK"
    end tell
end tell
    end tell

    if application "System Preferences" is running then
tell application "System Preferences" to quit
    end if

但正如 foo 所写,GUI 脚本应该是最后的解决方案。尤其是在 Mavericks 中,这真的很烦人,因为您必须为每个应用程序启用对辅助设备的访问(如果您更改脚本,则再次启用)。

于 2014-03-22T10:18:01.150 回答
0

我已将您的代码考虑在内并实施了。所以对于那些使用英语的人,你可以使用下面的代码:

tell application "System Events"
    tell application "System Preferences"
        reveal anchor "keyboardTab" of pane "com.apple.preference.keyboard"

    end tell
    tell window 1 of application process "System Preferences"
        --  click button "Modifier Keys…" of tab group 1
        set uiElems to entire contents

        click button "Modifier Keys…" of tab group 1 of window "Keyboard" of application process "System Preferences" of application "System Events"
        delay 1
        click pop up button "Caps Lock (⇪) Key:" of sheet 1 of window "Keyboard" of application process "System Preferences" of application "System Events"
        delay 0.2
        keystroke "n"
        delay 0.2
        key code 36
        delay 0.2
        click button "OK" of sheet 1 of window "Keyboard" of application process "System Preferences" of application "System Events"

    --  set uiElems to entire contents

    end tell

end tell

if application "System Preferences" is running then
    tell application "System Preferences" to quit
end if
于 2018-05-24T18:58:31.263 回答