【问题标题】:How to use AppleScript or Lua to do an ALT + TAB after results on the Calculator.app如何在 Calculator.app 上获得结果后使用 AppleScript 或 Lua 执行 ALT + TAB
【发布时间】:2022-01-16 23:14:06
【问题描述】:

我使用 Calculator.app,在按下 ENTER 键后,我希望它自动执行 ALT + TAB 来转到我使用的上一个应用程序:Excel 或 Firefox 或 Chrome,......你的名字。

我对 .lua 也有同样的问题,但如果不能用 .lua 来解决,我想用 AppleScript 或 Automator 来解决。

What's the hot key for TAB in .lua? hammerspoon?

更新:

正如 user3439894 所说,CMD + TAB 工作正常。

但在我的最后,TAB 没有发布,因此,我继续显示所有打开的应用程序。因此,我尝试将 RETURN 作为文档。我也试过 ENTER ,但它们都不起作用。

hs.eventtap.keyStroke({"cmd"}, "tab")
hs.eventtap.keyStroke({"return"})
-- hs.eventtap.keyStroke({"enter"})

【问题讨论】:

  • 你的意思是 ⌘-Tab 因为 ⌥-Tab 不是 App Switcher 键盘快捷键.
  • 这对我有用:hs.eventtap.keyStroke({"cmd"}, "tab")
  • 谢谢 user3439894 ,你是对的,它是 cmd 而不是 tab。但是如何释放标签?我注意到它有效,但继续显示所有打开的应用程序。就像tab键没有释放一样。我不明白你提到的“App Switcher”是什么。

标签: lua applescript automator hammerspoon


【解决方案1】:

假设您将它与我在 11 月中旬给您的关于 Calculator 的答案之一结合使用,然后是修改后的 Lua 代码Calculator 中按下 enter key 时,我可以切换到上一个 应用程序。 p>

示例 Lua 代码

    --  Create a hotkey used to trap the enter key and disable it.
    --  It will then be enabled/disabled as Calculator is focused/unfocused
    --  When enabled and the enter key is pressed it presses = then command C.

applicationCalculatorEnterHotkey = hs.hotkey.bind({}, "return", function()
        --  Press the '=' key to finish the calculation.
    hs.eventtap.keyStroke({}, "=")
        --  Copy the result to the clipboard.
    hs.eventtap.keyStroke({"cmd"}, "C")
        --  Bring up the App Switcher.
    hs.eventtap.keyStroke({"cmd"}, "tab")
        --  Act on the selected App in the App Switcher.
        --  This works in testing with Calculator, however
        --  may not work in other applications.
    hs.eventtap.keyStroke({}, "=")  
end)
applicationCalculatorEnterHotkey:disable()

注意事项:

我已经用 cmets 在原来的代码中添加了两个hs.eventtap.keyStroke

控制台中执行hs.eventtap.keyStroke({"cmd"}, "tab") Hammerspoon会自动切换到上一个应用程序而不显示应用程序切换器,但是当在applicationCalculatorEnterHotkey function 中使用时,它会显示 App Switcher,就像按下了 ⌘-Tab 并且 key 未释放。这可能是一个错误,不确定是否对问题进行了足够的测试/研究,因为明显的解决方案是通过编程方式按下 enter key 来跟进,但是这无法在applicationCalculatorEnterHotkey 函数中完成,因为它已经在 Calculator 中捕获了 enter key .

在这个特殊的用例中,hs.eventtap.keyStroke({}, "=") 被用来表示再次按下 enter key 并且是 独有的计算器

【讨论】:

  • 谢谢,@user3439894 效果很好,请告诉我如何通过聊天模式向您发送贝宝捐款。
  • 我刚刚这样做了@user3439894。您还可以回答另一个问题:*.com/questions/70142261/… ...,并附上此问题的链接。如果您改变主意,请告诉我,因为我学到了很多东西,这一切将为我节省大量时间和点击/移动。再次感谢
最近更新 更多