【问题标题】:How to use AppleScript with Calculator.app to Automatically select the number of Decimals如何使用 AppleScript 和 Calculator.app 自动选择小数位数
【发布时间】:2021-12-25 11:13:12
【问题描述】:

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

有没有办法根据我输入的数字制作一个自动执行此操作的 AppleScript?

我的输入方式是:

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

对于粘贴部分,如果您粘贴一个以 0 结尾的数字,Calculator.app 将不会显示 0,因此它会显示比实际少一位小数。我不知道这是否也可以克服。

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

【问题讨论】:

  • Calculator 中,例如粘贴时12.30 它确实显示为 12.3,但是无论您将 Calculator > View > Decimal Places 设置为它仍然没有显示0。那么为什么会有这么大的问题呢?
  • @user3439894 再次感谢,它主要用于下一次计算。例如: 12.3 / 1.01 = 12.18 ,它会选择 1 个小数,所以你会看到 12.2,这是不正确的。
  • RE:“所以你会看到 12.2”——不,我明白了:12.178217821782178
  • 这就是我现在所做的,我感觉不到我的指尖。所以,我决定尽可能地自动化。我需要它在使用计算器时一直运行。小数选择器应该在开始时运行,在结束时复制结果。
  • 您看到 12.178217821782178 是因为您有很多小数,但由于我输入 12.30,它应该选择 2 个小数(不多不少)。所以,我应该得到 12.18

标签: applescript calculator automator


【解决方案1】:

在我对您的另一个问题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 代码HammerspoonAPI和AppleScript code,如上所示,分别在 ma​​cOS Mojave 下使用 HammerspoonScript Editor 进行了测试> 和 ma​​cOS 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()

--]]

【讨论】:

  • 评论不用于扩展讨论;这个对话是moved to chat
  • @sebseb,请在 仅使用 Lua 的更新中查看并实施新的 example Lua code没有AppleScript 部分的答案。
  • 哇,它飞起来了,速度如此之快,而且没有任何故障。一切都很完美。我很高兴;o)我在玩旧代码,我什至没能让它复制结果。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-16
  • 1970-01-01
  • 1970-01-01
  • 2013-11-06
  • 1970-01-01
  • 2011-05-23
相关资源
最近更新 更多