如何将 AppleScript 与 Calculator.app 一起使用以自动 select 小数位数

How to use AppleScript with Calculator.app to Automatically select the number of Decimals

每次我在 Calculator.app 中输入数字时,我都需要转到顶部菜单和 select: 查看 >> 小数位数 >> [0 , 15] 并选择小数位数。

有没有办法制作一个 AppleScript,它会根据我输入的数字自动这样做?

我的输入法是:

  1. 通过粘贴数字
  2. 通过输入数字

对于粘贴部分,如果你粘贴一个以0结尾的数字,Calculator.app不会显示0,所以它会显示比实际少一位小数。不知道这个能不能克服

例如,如果您粘贴:12.30,它将显示 12.3 如果您键入:12.30,它将显示 12.30

基于我对你的另一个问题 的回答的附录,这是 计算器[=142] 中陷阱 ⌘V 的一种方法=] 并设置 Calculator > View > Decimal Places 为数字的小数位数剪贴板.

示例 AppleScript code 的作用:

当在计算器中按⌘V时,剪贴板的内容被设置为a 变量,如果它包含 小数点,它会设置 计算器 > 视图 > 小数位数 菜单小数位数中的number,然后 keystrokesnumber 转换为 Calculator。请注意,击键 数字可能不需要您设置小数位数,但是我由您决定。

示例 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 in: ~/.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 codeCalculator 有焦点。 ⌘V 键盘快捷键 应该在所有其他 应用程序.

中正常工作

将上面的例子Lua代码添加到~/.hammerspoon/init.lua file 来自我对你的其他问题的其他回答。

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

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


仅使用 Lua 而不使用 AppleScript

进行更新

下面的例子Lua代码消除了[=的使用90=]AppleScript 来自这个答案和 code 在你的其他相关问题的另一个答案中。

覆盖现有的代码,将其复制并粘贴到您的~/.hammerspoon/init.lua文件,保存并运行 Hammerspoon 菜单栏 Reload Config .

我已经执行了评论中讨论的所有各种操作和计算,并且这个新的 代码 没有出现任何问题。希望这会消除您遇到的任何问题。

这也应该 运行 更快,因为它还必须处理 AppleScript code

请注意,如编码所示,它不会自动重新加载配置文件,因为这不是必需的。

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

--]]