如何复制 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")
注意事项:
示例 AppleScript 代码,如下所示,在 macOS Catalina 下的 Script Editor 中进行了测试 和 macOS 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 而只使用 Lua,Hammerspoon 及其 使用的 语言 >API。但是,由于我已经将各种 AppleScript scripts 与 Hammerspoon 一起使用,并且可以轻松地回收一些现有的 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 code 与 Hammerspoon 结合使用,并保存为 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 代码和Hammerspoon的API已经展示了。
注意事项:
用示例 Lua 代码,作为编码,按下enter 键的行为 仅被捕获和修改以触发 示例 AppleScript 代码,或者如果使用备用选项发送 Hammerspoon keystrokes,而 Calculator 有焦点。 enter 键应该在所有其他应用程序中正常工作。
请参阅我的其他 Hammerspoon 相关答案以获取安装说明和使用此处包含的信息。
粒子中的一个是:
如果使用脚本编辑器,示例 AppleScript 代码保存为文本 在文件格式: 弹出菜单在保存 对话框中。
例子 Hammerspoon和的Lua 代码和API上面直接显示的 AppleScript 代码 分别在 macOS Mojave 下使用 Hammerspoon 和 Script Editor 进行了测试和 macOS Catalina,将 系统偏好设置 中的 语言和地区 设置设置为 English (US) — Primary 并为我没有问题1.
-
1 假设 系统偏好设置 > 安全和隐私 > 隐私中的必要和适当的设置已设置/根据需要解决。