如何在Calculator.app中使用AppleScript自动选择小数位数 [英] How to use AppleScript with Calculator.app to Automatically select the number of Decimals
问题描述
有没有办法根据我输入的数字创建一个自动执行此操作的AppleScript?
我的输入方式是:
- 粘贴号码
- 通过键入数字
对于粘贴部分,如果粘贴一个以0结尾的数字,Calculator.app将不会显示0,因此它将显示比实际值小一位的小数。我不知道这是否也能被克服。
例如,如果粘贴:12.30,它将显示12.3 如果您键入:12.30,它将显示12.30推荐答案
在我回答您的另一个问题的附录How can I copy result from Calculator.app to the clipboard using AppleScript的基础上,在计算器中捕获<⌘V,并将计算器查看&小数位数设置为剪贴板上数字的小数位数。
在计算器中按<⌘V时,剪贴板的内容被设置为变量,如果它包含小数点,它将计算器和<>视图示例应用程序脚本代码的作用:
示例应用程序脚本代码:
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
上面的示例AppleScrip代码保存为~/.hammerspoon/Scripts/
中的CalculatorSetDecimalPlaces.applescript,就像我对其他问题的另一个回答一样。
示例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()
备注:
使用示例代码编码,<⌘V键盘快捷键的行为仅被捕获和修改以触发示例代码,而计算器具有焦点。⌘V键盘快捷键在所有其他应用程序中应该可以正常工作。
上面的示例Lua代码已添加到~/.hammerspoon/init.lua
文件中,来自我对您另一个问题的回答。
- 1假定在系统首选项&安全和隐私&隐私中进行了必要的适当设置。隐私已根据需要进行了设置/寻址。
仅使用Lua更新,不使用AppleScript
以下示例Lua代码在此答案和其他相关问题答案中的代码中都不使用AppleScrip。
覆盖现有代码,将其复制并粘贴到您的~/.hammerspoon/init.lua
文件中,保存它并从菜单栏菜单运行重新加载配置。
我已经执行了注释中讨论的所有各种操作和计算,此新代码没有出现任何问题。希望这将消除您所遇到的任何问题。
现在它还必须处理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()
--]]
这篇关于如何在Calculator.app中使用AppleScript自动选择小数位数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!