在我对您的另一个问题How can I copy result from Calculator.app to the clipboard using AppleScript 的回答的附录的基础上,这是一种在 Calculator 中设置 ⌘V 并设置 Calculator 的方法strong> > 查看 > 小数位数到剪贴板上数字的小数位数。
示例 AppleScript 代码的作用:
当在 Calculator 中按 ⌘V 时,剪贴板的内容被设置为一个变量,如果它包含一个小数点,它将 Calculator > View > Decimal Places menu 设置为数字中的小数位数,然后击键将数字输入计算器 .请注意,keystroking number 可能不需要您设置 小数位数,但我将由您决定.
示例 AppleScript 代码:
set theNumberToType to the clipboard as text
if theNumberToType contains "." then
set theNumberOfDecimalPlaces to count characters ((offset of "." in theNumberToType) + 1) thru -1 of theNumberToType
if theNumberOfDecimalPlaces is greater than 15 then set theNumberOfDecimalPlaces to 15
else
set theNumberOfDecimalPlaces to -1
end if
tell application "Calculator" to activate
tell application "System Events"
if theNumberOfDecimalPlaces is greater than -1 then
click (first menu item of menu 1 of menu item "Decimal Places" of menu 1 of menu bar item "View" of menu bar 1 of process "Calculator" whose name is theNumberOfDecimalPlaces)
end if
delay 0.1
keystroke theNumberToType
end tell
上面的示例 AppleScript 代码 保存为 CalculatorSetDecimalPlaces.applescript 在:~/.hammerspoon/Scripts/,就像在我对你其他问题的其他回答。
示例 Lua 代码:
-- Create a hotkey used to trap the command v shortcut and disable it.
-- It will then be enabled/disabled as Calculator is focused/unfocused.
-- When enabled and command v is pressed it runs the AppleScript script.
local applicationCalculatorCommandVHotkey = hs.hotkey.bind({"cmd"}, "V", function()
local asFile = "/.hammerspoon/Scripts/CalculatorSetDecimalPlaces.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)
applicationCalculatorCommandVHotkey:disable()
-- Add the following two line respectively before or after the lines
-- applicationCalculatorEnterHotkey:enable()
-- applicationCalculatorEnterHotkey:disable()
-- in either of the two methods presented in my other answer.
applicationCalculatorCommandVHotkey:enable()
applicationCalculatorCommandVHotkey:disable()
注意事项:
使用示例 Lua 代码,作为编码,⌘V 键盘的行为快捷方式 仅被捕获和修改以触发 示例 AppleScript 代码,而 Calculator 具有焦点。 ⌘V 键盘快捷键应该可以在所有其他应用程序中正常工作。
上面的 example Lua code 从我对您的其他回答中添加到 ~/.hammerspoon/init.lua file其他问题。
示例 Lua 代码和Hammerspoon的API和AppleScript code,如上所示,分别在 macOS Mojave 下使用 Hammerspoon 和 Script Editor 进行了测试> 和 macOS Catalina,将 系统偏好设置 中的 语言和地区 设置设置为 English (US) - Primary 并且可以正常工作对我来说没有问题1。
-
1 假设 系统偏好设置 > 安全和隐私 > 隐私中的必要和适当的设置已设置/根据需要解决。
仅使用 Lua 更新,不使用 AppleScript
以下示例 Lua 代码从这个答案和中都消除了AppleScript的使用代码 在您其他相关问题的其他答案中。
覆盖现有的代码,将其复制并粘贴到您的~/.hammerspoon/init.lua 文件中,保存并从运行Reload Config >Hammerspoon 菜单在菜单栏上。
我已经执行了 cmets 中讨论的所有各种操作和计算,并且这个新的代码没有出现任何问题。希望这将消除您遇到的任何问题。
由于它还必须处理 AppleScript 代码,因此它也应该运行得更快。
请注意,按照编码,它不会自动重新加载配置文件,因为这不是必需的。
-- 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.
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()
-- Create a hotkey used to trap the command v shortcut and disable it.
-- It will then be enabled/disabled as Calculator is focused/unfocused.
-- When enabled and command v is pressed it runs the Lua code within.
local applicationCalculatorCommandVHotkey = hs.hotkey.bind({"cmd"}, "V", function()
-- Get the number copied to the clipboard.
local theNumberToType = hs.pasteboard.readString()
-- See if there is a decimal in the number
-- and if so how many decimal places it has.
local l = string.len(theNumberToType)
local s, e = string.find(theNumberToType, "%.")
if s == nil then
theNumberOfDecimalPlaces = -1
elseif l - s > 15 then
theNumberOfDecimalPlaces = 15
else
theNumberOfDecimalPlaces = l - s
end
-- Set the number of decimal places to show.
if theNumberOfDecimalPlaces > -1 then
local calculator = hs.appfinder.appFromName("Calculator")
local vdpn = {"View", "Decimal Places", theNumberOfDecimalPlaces}
calculator:selectMenuItem(vdpn)
end
-- Type the number into Calculator.
hs.eventtap.keyStrokes(theNumberToType)
end)
applicationCalculatorCommandVHotkey: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 hotkeys when Calculator is focused.
applicationCalculatorCommandVHotkey:enable()
applicationCalculatorEnterHotkey:enable()
end)
CalculatorWindowFilter:subscribe(hs.window.filter.windowUnfocused, function()
-- Disable hotkeys when Calculator is unfocused.
applicationCalculatorCommandVHotkey:disable()
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.
-- The opening '--[[' and closing '--]]' and removed from below added to above.
--[[
function applicationCalculatorWatcher(appName, eventType, appObject)
if (eventType == hs.application.watcher.activated) then
if (appName == "Calculator") then
-- Enable hotkeys when Calculator is activated.
applicationCalculatorCommandVHotkey:enable()
applicationCalculatorEnterHotkey:enable()
end
end
if (eventType == hs.application.watcher.deactivated) then
if (appName == "Calculator") then
-- Disable hotkeys when Calculator is deactivated.
applicationCalculatorCommandVHotkey:disable()
applicationCalculatorEnterHotkey:disable()
end
end
end
appCalculatorWatcher = hs.application.watcher.new(applicationCalculatorWatcher)
appCalculatorWatcher:start()
-- appCalculatorwWatcher:stop()
--]]