gustavo
V2EX  ›  macOS

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

  •  
  •   gustavo · Jan 18, 2020 · 3725 views
    This topic created in 2495 days ago, the information mentioned may be changed or developed.

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

    7 replies  •  2020-01-19 06:06:23 +08:00
    astrophys
    astrophys
    Jan 18, 2020
    1
    System Preference -- Keyboard -- Shortcuts -- App Shortcuts
    gustavo
    gustavo
    OP
    Jan 18, 2020
    2
    @astrophys 这个无法设置全局快捷键,这个只能设置某个应用菜单栏里面的选项的快捷键,关键是如果我的活跃窗口不是系统偏好设置,那么给“显示或隐藏菜单栏”设置的快捷键就无效。我想达到的效果是类似于“option+command+D”能在任何活跃窗口下显示或隐藏 dock 栏的效果。全网没有搜到相关经验,我认为只有一个方法,那就是写一个 Applescript,内容是激活“显示或隐藏菜单栏”这个事件,然后利用第三方软件设置一个全局快捷键来激活这个 Applescript。很遗憾,我并不知道激活这个事件的具体代码怎么写,具体来说就是不知道“显示或隐藏菜单栏”这个事件的代码是什么,不知道大佬了不了解。
    minamike
    minamike
    Jan 18, 2020
    3
    Alfred
    Keyboard Maestro
    hammerspoon
    gustavo
    gustavo
    OP
    Jan 18, 2020 via iPhone
    4
    @minamike 我有 Alfred,但是具体怎么写脚本你能告诉我吗,其实用什么软件都一样,关键是写出激活或停止“自动隐藏或显示菜单栏”这个事件的两行代码,苹果官方给出的 API 不知道叫什么
    minamike
    minamike
    Jan 18, 2020 via iPhone
    5
    @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
    gustavo
    OP
    Jan 18, 2020
    6
    感谢回复,但是运行了之后有报错:error "“System Events”遇到一个错误:不能获得“window \"General\" of process \"System Preferences\"”。" number -1728 from window "General" of process "System Preferences"
    astrophys
    astrophys
    Jan 19, 2020
    7
    @astrophys 原来你说的是整个桌面窗口的 menu bar,我以为是各个应用的… 这个网上有不少例子,不过我没试过: https://apple.stackexchange.com/questions/283633/can-a-shortcut-be-created-to-toggle-menubar-visibility-that-would-persist-as-t
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2070 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:11 · PVG 23:11 · LAX 07:11 · JFK 10:11
    ♥ Do have faith in what you're doing.