【问题标题】:How can I copy result from Calculator.app to the clipboard using AppleScript如何使用 AppleScript 将结果从 Calculator.app 复制到剪贴板
【发布时间】:2021-11-14 22:07:16
【问题描述】:

如何复制 Calculator.app 的结果,包括小数。

默认选择,所以如果你只是做一个 CMD+C 它将它复制到你的剪贴板。我从@jweaks 得到这段代码

set the clipboard to {?????}

我尝试输入了许多不同的选项,但我不知道应该输入什么。

【问题讨论】:

  • @jweaks 我不知道你能不能帮我解决这个问题;o)
  • 您在标题中的问题已被其他 2 位用户回答。我只想添加 当 GUI 脚本编写时 Calculator.app 的结果(真实)文本表单 的形式存储在剪贴板中。因此,要获得真实的内容并进一步使用它,您应该将剪贴板内容强制恢复为真实内容:将剪贴板的 theResult 设置为真实

标签: applescript calculator clipboard


【解决方案1】:

如何复制 Calculator.app 的结果(包括小数)。

set the clipboard to {?????}

您已经知道 ⌘C 可以做到,但是,如果您想使用 set clipboard to 方法,那么这里有一种方法:

示例 AppleScript 代码

if not running of application "Calculator" then return

tell application "System Events" to ¬
    set the clipboard to ¬
        (get the value of ¬
            static text 1 of ¬
            group 1 of ¬
            window 1 of ¬
            process "Calculator")

注意事项:

  • 不要求计算器位于最前面

  • 不需要使用keystrokekey code 来完成任务。

  • 如果想以不同的方式处理它,可以将 value 设置为 变量 而不是 剪贴板

示例 AppleScript 代码,如下所示,在 ma​​cOS Catalina 下的 Script Editor 中进行了测试ma​​cOS Monterey系统偏好设置中的语言和地区设置设置为英语(美国)- 主要 并且毫无问题地为我工作1

  • 1 假设 系统偏好设置 > 安全和隐私 > 隐私中的必要和适当的设置已设置/根据需要解决。

在测试中,Script Editor 中的Replies pane 返回,例如:

tell application "System Events"
    get value of static text 1 of group 1 of window 1 of process "Calculator"
    --> "6200.549407114624506"
    set the clipboard to "6200.549407114624506"
end tell

然后,当粘贴到 文档 时,它粘贴为:6200.549407114624506



更新到地址 cmets

在我的回答下,由 sebseb 解决随后的 cmets,特别是……

每次我在计算器上按回车键时都可以运行脚本吗?然后复制结果。

Basic vanilla AppleScript 没有那么聪明,并且没有能力单独理解一个人在 Calculator 中正在做什么并知道何时按下enter 然后将结果放在剪贴板上。

必须使用中介,一个应用程序,如Hammerspoon,它可以等待计算器 应用程序被激活/停用或其窗口聚焦/不聚焦然后启用/禁用捕获进入 键 em> 在 keyboard 上被按下,然后运行 ​​脚本 以执行操作以通过按下 计算 结果 = key 然后将 result 复制到 剪贴板

这是因为在 Calculator 中按 = 相当于按 enter ,从而启用捕获 enter key 以使用 AppleScript 执行必要的操作。很可能不使用 AppleScript 而只使用 LuaHammerspoon 及其 使用的 语言 >API。但是,由于我已经将各种 AppleScript scriptsHammerspoon 一起使用,并且可以轻松地回收一些现有的 code 我'将使用 Hammerspoon 中的两种方法提供原始答案的附录。

Hammerspoon例子 Lua 代码API放在~/.hammerspoon/init.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 runs the AppleScript script.

local applicationCalculatorEnterHotkey = hs.hotkey.bind({}, "return", function()
    local asFile = "/.hammerspoon/Scripts/CalculatorResultToClipboard.applescript"
    local ok, status = hs.osascript.applescriptFromFile(os.getenv("HOME") .. asFile)
    if not ok then              
        msg = "An error occurred running the CalculatorResultToClipboard script."
        hs.notify.new({title="Hammerspoon", informativeText=msg}):send()            
    end
end)
applicationCalculatorEnterHotkey:disable()


    --  One of two methods of watching Calculator.
    --  
    --  The other is below this one and commented out.

    --  Initialize a Calculator window filter.

local CalculatorWindowFilter = hs.window.filter.new("Calculator")

    --  Subscribe to when the Calculator window is focused/unfocused.

CalculatorWindowFilter:subscribe(hs.window.filter.windowFocused, function()
        --  Enable hotkey when Calculator is focused.
    applicationCalculatorEnterHotkey:enable()
end)
CalculatorWindowFilter:subscribe(hs.window.filter.windowUnfocused, function()
        --  Disable hotkey when Calculator is unfocused.
    applicationCalculatorEnterHotkey:disable()
end)


        --  Alternate method to wait for Calculator and enable/disable the hotkey.
        --  
        --  Uncomment below method and comment the above method to test between them. Adding the 
        --  multiple line opening '--[[' and closing '--]]' to above method and removed from below,
        --  leaving 'local CalculatorWindowFilter = hs.window.filter.new("Calculator")' uncommented.

--[[    

function applicationCalculatorWatcher(appName, eventType, appObject)
    if (eventType == hs.application.watcher.activated) then
        if (appName == "Calculator") then
                --  Enable hotkey when Calculator is activated.
            applicationCalculatorEnterHotkey:enable()
        end
    end
    if (eventType == hs.application.watcher.deactivated) then
        if (appName == "Calculator") then
                --  Disable hotkey when Calculator is deactivated.
            applicationCalculatorEnterHotkey:disable()
        end
    end
end
appCalculatorWatcher = hs.application.watcher.new(applicationCalculatorWatcher)
appCalculatorWatcher:start()
-- appCalculatorwWatcher:stop()

--]]

以下示例 AppleScript codeHammerspoon 结合使用,并保存为 CalculatorResultToClipboard .applescript~/.hammerspoon/Scripts/ 中,您需要创建分层文件夹结构

示例 AppleScript 代码

一个可以使用:

tell application "Calculator" to activate
tell application "System Events"
    key code 24
    delay 0.5
    set the theResult to the value of static text 1 of group 1 of window 1 of process "Calculator"
end tell
set the clipboard to theResult

或者:

tell application "Calculator" to activate
tell application "System Events"
    key code 24
    delay 0.5
    key code 8 using command down
end tell

完成任务。

如前所述,另一种选择是放弃使用 AppleScript 并使用以下 示例 Lua 代码:

local 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")
end)
applicationCalculatorEnterHotkey:disable()

这个 function 将被用来代替上面的相同 function。它用 Hammerspoon 生成的 keystrokes 代替 AppleScript script 的执行来完成相同的任务,同时使用剩下的示例 Lua 代码HammerspoonAPI已经展示了。

注意事项:

示例 Lua 代码,作为编码,按下enter 键的行为 仅被捕获和修改以触发 示例 AppleScript 代码,或者如果使用备用选项发送 Hammerspoon keystrokes,而 Calculator 有焦点。 enter 应该在所有其他应用程序中正常工作。

请参阅我的其他 Hammerspoon 相关答案以获取安装说明和使用此处包含的信息。

粒子中的一个是:

如果使用脚本编辑器示例 AppleScript 代码保存为文本文件格式: 弹出菜单保存 对话框中。

例子 HammerspoonLua 代码API上面直接显示的 AppleScript 代码 分别在 ma​​cOS Mojave 下使用 HammerspoonScript Editor 进行了测试和 ma​​cOS Catalina,将 系统偏好设置 中的 语言和地区 设置设置为 English (US) — Primary 并为我没有问题1.

  • 1 假设 系统偏好设置 > 安全和隐私 > 隐私中的必要和适当的设置已设置/根据需要解决。

【讨论】:

  • @sebseb,您运行的是哪个版本的 macOS
  • @sebseb,我只是在 macOS Mojave 下的答案中测试了 example AppleScript code b> 10.14.6,它确实将 Calculator application 的结果放在 剪贴板 上。
  • 在您的问题中,您明确指出“如何复制 Calculator.app 的结果,包括小数。” result 是在进行 calculation 之后剩下的,那么为什么要在进行 calculation 之前运行 script >!? RE:“但我需要得到结果。不必每次都运行脚本。” -- 抱歉,这不是 scripting 通常的工作方式。我建议您只使用内置的 键盘快捷键 ⌘Cresult 复制到 剪贴板根据需要。
  • RE:“为什么脚本不能一直运行?每次我进行计算时,都会将结果复制到剪贴板。” -- Basic vanilla AppleScript 没有那么智能,并且无法自行理解您在 Calculator 中所做的事情,并且知道您何时在 result 上放置了 剪贴板
  • 每次我在计算器上按回车键时都可以运行脚本吗?然后复制结果。
【解决方案2】:

Calculator.app 没有 AppleScript 字典。

您必须使用 System Events 编写 UI 脚本

tell application "System Events"
    tell process "Calculator"
        set frontmost to true
        keystroke "c" using command down
    end tell
end tell

【讨论】:

  • 谢谢,@vadian,它可以工作,但只能工作一次。我怎样才能做到每次我使用计算器时都会复制结果?
猜你喜欢
  • 1970-01-01
  • 2014-01-02
  • 1970-01-01
  • 2016-11-23
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-26
相关资源
最近更新 更多