如何使用 AppleScript 将结果从 Calculator.app 复制到剪贴板

How can I copy result from Calculator.app to the clipboard using AppleScript

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

默认情况下处于选中状态,因此如果您只执行 CMD+C,它会将其复制到您的剪贴板中。我从@jweaks

那里得到了这段代码
set the clipboard to {?????}

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

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

How do I copy the result of the Calculator.app including decimals.

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 来完成任务。

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

下面显示的示例AppleScript代码在[=146=中进行了测试macOS CatalinamacOS Monterey 下的 ]Script Editor 以及 Language & Region 设置系统偏好设置设置为英语(美国)-主要并且对我有用1.

  • 1 系统偏好设置 > 安全与隐私[=] 中假定必要且适当的设置242=] > 隐私 已根据需要 set/addressed。

在测试中,回复窗格脚本编辑器中返回,例如:

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



更新以解决评论

为了解决 sebseb 在我的回答下的后续评论,特别是……

Is it possible to run the script every time I hit enter on Calculator? then copy the result.

Basic vanilla AppleScript 不是那么智能,本身没有能力理解一个人在做什么计算器 并知道何时按下 enter key 然后放置 结果 剪贴板上

必须使用中介,应用程序,例如 Hammerspoon,它可以在其中等待 计算器 应用程序是activated/deactivated或其windowfocused/unfocused然后 enabled/disable 捕获 enter key 键盘上被按下然后运行脚本执行一个动作计算结果= 然后将 结果 复制到 剪贴板.

这是有效的,因为在计算器中按下=等同于按下enter key, 从而可以捕获 enter key 来执行使用 AppleScript 的必要操作。很可能不使用 AppleScript 并且只使用 Lua,使用 language通过 Hammerspoon 及其 API。但是,由于我已经将各种 AppleScript scriptsHammerspoon 结合使用,并且可以轻松回收一些现有的 code 我将使用 Hammerspoon.

中的两种方法对原始答案进行补充

下面的例子Lua代码 Hammerspoon 的 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 代码一起使用Hammerspoon 并在 ~/.hammerspoon/Scripts/ 中保存为 CalculatorResultToClipboard.applescript,您需要创建 分层文件夹结构.

示例 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 并使用以下 example 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。它将 AppleScript script 的执行替换为 Hammerspoon 完成相同的任务,同时使用剩余的 example Lua code API of Hammerspoon 已经呈现。

备注:

使用 示例 Lua 代码,如编码所示,行为按下 enter key 仅被捕获并修改为触发 example AppleScript code,或者如果使用替代选项发送 Hammerspoon keystrokes,而 计算器 有焦点。 enter key 应该在所有其他 applications.

中正常工作

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

粒子中的一个是:

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

示例Lua代码APIHammerspoonAppleScript code,如上所示,是在 macOS MojavemacOS Catalina[= 下分别使用 HammerspoonScript Editor 进行了测试242=],语言和地区设置系统偏好设置设置为英语(美国)-主要和毫无问题地为我工作1.

  • 1 系统偏好设置 > 安全与隐私[=] 中假定必要且适当的设置242=] > 隐私 已根据需要 set/addressed。