V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
gustavo
V2EX  ›  macOS

macOS 怎么设置全局事件的快捷键

  •  
  •   gustavo · 2020-01-18 12:36:23 +08:00 · 2442 次点击
    这是一个创建于 1531 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我一直想实现这样一个功能,即在任何程序窗口按下一个快捷键就可以显示或隐藏菜单栏,就像我在任何程序窗口按下 option+command+D 就可以显示或隐藏 dock 栏一样。

    7 条回复    2020-01-19 06:06:23 +08:00
    astrophys
        1
    astrophys  
       2020-01-18 14:47:02 +08:00
    System Preference -- Keyboard -- Shortcuts -- App Shortcuts
    gustavo
        2
    gustavo  
    OP
       2020-01-18 17:12:49 +08:00
    @astrophys 这个无法设置全局快捷键,这个只能设置某个应用菜单栏里面的选项的快捷键,关键是如果我的活跃窗口不是系统偏好设置,那么给“显示或隐藏菜单栏”设置的快捷键就无效。我想达到的效果是类似于“option+command+D”能在任何活跃窗口下显示或隐藏 dock 栏的效果。全网没有搜到相关经验,我认为只有一个方法,那就是写一个 Applescript,内容是激活“显示或隐藏菜单栏”这个事件,然后利用第三方软件设置一个全局快捷键来激活这个 Applescript。很遗憾,我并不知道激活这个事件的具体代码怎么写,具体来说就是不知道“显示或隐藏菜单栏”这个事件的代码是什么,不知道大佬了不了解。
    minamike
        3
    minamike  
       2020-01-18 20:45:46 +08:00
    Alfred
    Keyboard Maestro
    hammerspoon
    gustavo
        4
    gustavo  
    OP
       2020-01-18 20:54:36 +08:00 via iPhone
    @minamike 我有 Alfred,但是具体怎么写脚本你能告诉我吗,其实用什么软件都一样,关键是写出激活或停止“自动隐藏或显示菜单栏”这个事件的两行代码,苹果官方给出的 API 不知道叫什么
    minamike
        5
    minamike  
       2020-01-18 22:09:49 +08:00 via iPhone
    @gustavo

    tell application "System Preferences"
    reveal pane id "com.apple.preference.general"
    delay 1
    tell application "System Events"
    click checkbox "Automatically hide and show the menu bar" of window "General" of process "System Preferences"
    end tell
    quit
    end tell
    gustavo
        6
    gustavo  
    OP
       2020-01-18 23:01:05 +08:00
    感谢回复,但是运行了之后有报错:error "“System Events”遇到一个错误:不能获得“window \"General\" of process \"System Preferences\"”。" number -1728 from window "General" of process "System Preferences"
    astrophys
        7
    astrophys  
       2020-01-19 06:06:23 +08:00
    @astrophys 原来你说的是整个桌面窗口的 menu bar,我以为是各个应用的… 这个网上有不少例子,不过我没试过: https://apple.stackexchange.com/questions/283633/can-a-shortcut-be-created-to-toggle-menubar-visibility-that-would-persist-as-t
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1396 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 17:30 · PVG 01:30 · LAX 10:30 · JFK 13:30
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.