如何使用AppleScript将Caps Lock键切换为Ctrl键

时间:2014-03-21 08:49:08

标签: macos vim applescript osascript

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

我找到了(this post),但似乎有点hacky。也许这是它应该的方式,但它显示系统优先。每当我使用它时,窗口不同于(this code)切换 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

以下是Modifier Keys屏幕截图:

enter image description here

enter link description here http://imageshack.us/a/img833/474/o5co.png

2 个答案:

答案 0 :(得分: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-Scripts应该是最后的解决方案。特别是在小牛队,它真的很烦人,因为你必须为每个应用程序启用辅助设备访问(如果你改变你的脚本,也是如此)。

答案 1 :(得分: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