如何使用 AppleScript 将结果从 Calculator.app 复制到剪贴板 [英] How can I copy result from Calculator.app to the clipboard using AppleScript

查看:43
本文介绍了如何使用 AppleScript 将结果从 Calculator.app 复制到剪贴板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

默认情况下是选中的,所以如果你只是执行一个 CMD+C,它会将它复制到你的剪贴板中.我从@jweaks 得到了这个代码

By defaults is selected, so if you just do a CMD+C it copies it into your clipboard. I got this code from @jweaks

set the clipboard to {?????}

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

I tried entering many different options but I don't know what I should put.

推荐答案

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

将剪贴板设置为{?????}

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

As you already know ⌘C can do it, however, if you want to use a set clipboard to method, then here is one way to go about it:

示例 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来完成任务.

Does not require the use of keystroke or key code to accomplish the task.

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

Can set the value to a variable instead of the clipboard, if wanting to process it in a different manner.

示例 AppleScript 代码,如下所示,在 ma​​cOS Catalina 下的 脚本编辑器 中进行了测试ma​​cOS Monterey语言和系统偏好设置中的区域设置设为英语(美国) - 主要并且对我来说没有问题1.

The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina and macOS Monterey with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 系统偏好设置中进行必要和适当的设置>安全&隐私 >隐私已根据需要设置/解决.
  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

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

In testing, the Replies pane in Script Editor returned, e.g.,:

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

Then when pasting into a document it pasted as: 6200.549407114624506

为了解决 sebseb 在我的回答中随后提出的评论,特别是......

To address the ensuing comments by sebseb under my answer and specifically…

是否可以在每次在计算器上按 Enter 键时运行脚本?然后复制结果.

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

Basic vanilla AppleScript 不是那么智能,并且本身没有能力理解计算器并知道何时按下enter ,然后将结果放在剪贴板上.

Basic vanilla AppleScript is not that intelligent and does not have the ability in of and by itself to understand what one is doing in Calculator and know when one has pressed the enter key to then place the result on the clipboard.

必须使用中介,一个应用程序,例如Hammerspoon,它可以等待计算器 应用程序被激活/停用或者它的窗口聚焦/不聚焦 然后启用/禁用捕获输入 键盘上被按下,然后运行脚本执行一个操作来计算结果,按= 然后复制结果剪贴板.

One would have to use an intermediary, an application like Hammerspoon, where it can wait for the Calculator application being activated/deactivated or its window being focused/unfocused to then enabled/disable trapping the enter key being pressed on the keyboard to then run the script to perform an action to calculate the result by pressing the = key then copy the result to the clipboard.

这是有效的,因为在计算器中按= 相当于按enter ,从而能够使用 AppleScript 捕获 enter 以执行必要的操作.很可能不使用 AppleScript 而只使用 LuaHammerspoon 使用的语言及其>API.但是,由于我已经将各种 AppleScript 脚本Hammerspoon 结合使用,并且可以轻松回收一些现有的代码,我'将使用 Hammerspoon 中的两种方法提供原始答案的附录.

This works because pressing the = key in Calculator is equivalent to pressing the enter key, thus enabling trapping the enter key to perform the necessary actions using AppleScript. It quite possibly can be done without using AppleScript and just Lua, the language used by Hammerspoon and its API. However, since I already use various AppleScript scripts in conjunction with Hammerspoon and can easily recycle some existing code I'll present an addendum to the original answer using both methods in Hammerspoon.

以下示例 Lua 代码HammerspoonAPI放在~/.hammerspoon/init.lua 文件.:

The following example Lua code and API of Hammerspoon is placed in the ~/.hammerspoon/init.lua file.:

    --  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结合使用并保存为CalculatorResultToClipboard.applescript~/.hammerspoon/Scripts/ 中,您需要创建分层文件夹结构.

The following example AppleScript code is used in conjunction with Hammerspoon and is saved as CalculatorResultToClipboard.applescript in ~/.hammerspoon/Scripts/, and you'll need to create the hierarchical folder structure.

示例 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 代码:

An alternate option, as previously mentioned, is to forgo the use of AppleScript and use the following example Lua code:

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()

这个函数将被用来代替上面相同的函数.它用 Hammerspoon 生成的 击键 代替 AppleScript 脚本 的执行来完成相同的任务,同时使用剩余的示例 Lua 代码HammerspoonAPI已经呈现.

This function would be used instead of the same function further above. It replaces the execution of the AppleScript script with keystrokes generated by Hammerspoon to accomplish the same tasks, while using the remaining example Lua code and the API of Hammerspoon already presented.

注意事项:

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

With the example Lua code, as coded, the behavior of pressing the enter key is only trapped and modified to trigger the example AppleScript code, or if using the alternate option send Hammerspoon keystrokes, while Calculator has focus. The enter key should work normally in all other applications.

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

See my other Hammerspoon related answers for instructions to install it and utilize the information contained herein.

粒子中的一个是:

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

If using Script Editor, the example AppleScript code is saved as Text in the File Format: pop-up menu in the Save dialog box.

示例 Lua 代码API HammerspoonAppleScript 代码,直接显示在上面,分别在 ma​​cOS Mojave 下使用 HammerspoonScript Editor 进行了测试和 ma​​cOS Catalina语言和;系统偏好设置中的区域设置设为英语(美国) - 主要并且对我来说没有问题1.

The example Lua code and API of Hammerspoon and AppleScript code, shown directly above, were tested respectively with Hammerspoon and Script Editor under macOS Mojave and macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 系统偏好设置中进行必要和适当的设置>安全&隐私 >隐私已根据需要设置/解决.
  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

这篇关于如何使用 AppleScript 将结果从 Calculator.app 复制到剪贴板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆