【问题标题】:How to use AppleScript to switch Caps Lock Key to be Ctrl Key如何使用 AppleScript 将 Caps Lock 键切换为 Ctrl 键
【发布时间】:2014-04-28 12:59:46
【问题描述】:

我似乎无法弄清楚如何使用 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

这里是修改键截图:

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

【问题讨论】:

  • 你有什么理由不使用KeyRemap4MacBookPCKeyboardHack吗?
  • 是的,我正在努力做到这一点,一旦我完成它,我就可以在 Alfred 社区中分发工作流,这样他们就不必去下载一堆额外的软件了。如果您查看我上面提到的链接,基本上我无法将其切换回 CAPS LOCK KEY。它只是保持选择 CTRL 键。我想不通。
  • 所以您希望人们下载软件,这样他们就不必下载软件了?哼。说真的,GUI 脚本非常笨拙和脆弱:脚本经常因操作系统/应用程序更新或在具有不同用户设置的其他机器之间共享而中断。当没有其他选项时,您真的只想自己使用 GUI 脚本,并且您希望更少地分发它们。如果用户需要调整键映射,只需告诉他们使用 KR4MB:它成熟、强大且完全免费。或者使用 [例如] Ukelele 制作您自己的自定义键映射并分发。
  • 谢谢@foo!我很感激反馈。现在我知道如何“以正确的方式做事”,我很感激。

标签: macos vim applescript osascript


【解决方案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 中,这真的很烦人,因为您必须为每个应用程序启用对辅助设备的访问(如果您更改脚本,则再次启用)。

【讨论】:

    【解决方案2】:

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

    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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-10-23
      • 1970-01-01
      • 2015-02-09
      • 2010-11-23
      • 2011-01-11
      • 2010-12-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多