1

How do I copy the result of the Calculator.app including decimals.

By defaults is selected, so if you just do a CMD+C it copies it into your clipboard. I got this code from @jweaks

set the clipboard to {?????}

I tried entering many different options but I don't know what I should put.

sebseb
  • 93
  • 1
  • 1
  • 8
  • @jweaks I don't know if you could help me with this one ;o) – sebseb Nov 12 '21 at 18:21
  • Your question in the title already is answered by other 2 users. I want only add that the **result (real) of Calculator.app when GUI scripting** is stored in the clipboard in the **text form**. So, to get the real and to work with it further, you should coerce the clipboard contents back to the real: **set theResult to the clipboard as real** – Robert Kniazidis Nov 12 '21 at 20:02

2 Answers2

1

How do I copy the result of the Calculator.app including decimals.

set the clipboard to {?????}

As you already know ⌘C can do it, however, if you want to use a set clipboard to method, then here is one way to go about it:

Example AppleScript code:

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

Notes:

  • Does not require Calculator to be frontmost.

  • Does not require the use of keystroke or key code to accomplish the task.

  • Can set the value to a variable instead of the clipboard, if wanting to process it in a different manner.

The example AppleScript code, shown below, was tested in Script Editor under macOS Catalina and macOS Monterey with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.

In testing, the Replies pane in Script Editor returned, e.g.,:

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

Then when pasting into a document it pasted as: 6200.549407114624506



Update to address comments

To address the ensuing comments by sebseb under my answer and specifically…

Is it possible to run the script every time I hit enter on Calculator? then copy the result.

Basic vanilla AppleScript is not that intelligent and does not have the ability in of and by itself to understand what one is doing in Calculator and know when one has pressed the enter key to then place the result on the clipboard.

One would have to use an intermediary, an application like Hammerspoon, where it can wait for the Calculator application being activated/deactivated or its window being focused/unfocused to then enabled/disable trapping the enter key being pressed on the keyboard to then run the script to perform an action to calculate the result by pressing the = key then copy the result to the clipboard.

This works because pressing the = key in Calculator is equivalent to pressing the enter key, thus enabling trapping the enter key to perform the necessary actions using AppleScript. It quite possibly can be done without using AppleScript and just Lua, the language used by Hammerspoon and its API. However, since I already use various AppleScript scripts in conjunction with Hammerspoon and can easily recycle some existing code I'll present an addendum to the original answer using both methods in Hammerspoon.

The following example Lua code and API of Hammerspoon is placed in the ~/.hammerspoon/init.lua file.:

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

--]]

The following example AppleScript code is used in conjunction with Hammerspoon and is saved as CalculatorResultToClipboard.applescript in ~/.hammerspoon/Scripts/, and you'll need to create the hierarchical folder structure.

Example AppleScript code:

One can use either:

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

Or:

tell application "Calculator" to activate
tell application "System Events"
    key code 24
    delay 0.5
    key code 8 using command down
end tell

To accomplish the task.

An alternate option, as previously mentioned, is to forgo the use of AppleScript and use the following example Lua code:

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

This function would be used instead of the same function further above. It replaces the execution of the AppleScript script with keystrokes generated by Hammerspoon to accomplish the same tasks, while using the remaining example Lua code and the API of Hammerspoon already presented.

Notes:

With the example Lua code, as coded, the behavior of pressing the enter key is only trapped and modified to trigger the example AppleScript code, or if using the alternate option send Hammerspoon keystrokes, while Calculator has focus. The enter key should work normally in all other applications.

See my other Hammerspoon related answers for instructions to install it and utilize the information contained herein.

One in particle is:

If using Script Editor, the example AppleScript code is saved as Text in the File Format: pop-up menu in the Save dialog box.

The example Lua code and API of Hammerspoon and AppleScript code, shown directly above, were tested respectively with Hammerspoon and Script Editor under macOS Mojave and macOS Catalina with Language & Region settings in System Preferences set to English (US) — Primary and worked for me without issue1.

  • 1 Assumes necessary and appropriate settings in System Preferences > Security & Privacy > Privacy have been set/addressed as needed.
user3439894
  • 7,266
  • 3
  • 17
  • 28
  • @sebseb, What version of **macOS** are you running? – user3439894 Nov 13 '21 at 12:11
  • @sebseb, I just test the _example_ **AppleScript** _code_ in my answer under **macOS Mojave** 10.14.6 and it does indeed place the result of the **Calculator** _application_ on _the clipboard_. – user3439894 Nov 13 '21 at 12:29
  • In you question you explicitly state "How do I copy the result of the Calculator.app including decimals." The _result_ is what is left after a _calculation_ is made, so why would you run the _script_ before making the _calculation_!? RE: "But I would need to get the result. Without having to run the script every time." -- Sorry, but that's not the way _scripting_ usually works. I'd suggest you just use the built-in _keyboard shortcut_ **⌘C** to copy the _result_ to _the clipboard_ as needed. – user3439894 Nov 13 '21 at 12:59
  • RE: "Why can't the script be running all the time? every time I do a calculation copy the result to clipboard." -- Basic _vanilla_ **AppleScript** is not that intelligent and does not have the ability in of and by itself to understand what you are doing in **Calculator** and know when you are at a _result_ you what placed on _the clipboard_. – user3439894 Nov 13 '21 at 13:13
  • Is it possible to run the script every time I hit enter on Calculator? then copy the result. – sebseb Nov 13 '21 at 13:32
  • @sebseb, RE: "Is it possible to run the script every time I hit enter on Calculator? then copy the result." -- As I previous said "Basic _vanilla_ **AppleScript** is not that intelligent and does not have the ability in of an by itself …", so you would have to use an intermediary, an _app_ like [**Hammerspoon**](https://www.hammerspoon.org), where it waits for **Calculator** being activated/deactivated and then enabled/disable trapping the **enter** _key_ on the keyboard being pressed to then run the _script_ to copy the _result_ to _the clipboard_. – user3439894 Nov 13 '21 at 15:56
  • @sebseb, The _script_ would also have to perform the function of the **enter** _key_ since its being trapped by **Hammerspoon**. It could `key code 24` to perform the _calculation_ and then copy it to _the clipboard_. Note that while this _method_ can and does work, you have to keep in mind that as **Calculator** is either activated/deactivated or focused/unfocused that enabling/disabling trapping the **enter** _key_ is not necessarily instantaneous, it takes a number of milliseconds to a second for it to occur, depending on how much activity the **CPU** is currently processing system wide. – user3439894 Nov 13 '21 at 15:56
  • @sebseb, RE: "Is it possible to run the script every time I hit enter on Calculator? -- I have update my answer to address this. See the **Update to address comments** _section_ in my answer, thanks. – user3439894 Nov 13 '21 at 20:48
  • Wow, amazing job. Thanks so much. A few questions, HS works, Hello World, works. When I hit enter in the Calculator HS says: An error occurred running: the CalculatorResultToClipboard script I used: ~/.hammerspoon/init.lua file and the first script saved in ~/.hammerspoon/Scripts/CalculatorResultToClipboard.applescript – sebseb Nov 14 '21 at 16:51
  • Your code works, the auto copy to works. The first 2 times works fine, after that I need to hit enter twice, to get the calculation to work. Very strange. Do you have the same thing at your end? – sebseb Nov 14 '21 at 17:17
  • @sebseb, RE: "Your code works, the auto copy to works. The first 2 times works fine, after that I need to hit enter twice, to get the calculation to work." -- In my initial testing I did not have any issue, however, I probably didn't run it multiple times in a row as if in production use. Testing now I can replicate the _bug_ but not sure why it's happening. That said, I did stop using the original `local applicationCalculatorEnterHotkey = hs.hotkey.bind({}, "return", function()` for the one that only use **Hammerspoon** _keystrokes_ and not **AppleScript** and it didn't fail in my testing. – user3439894 Nov 14 '21 at 18:58
  • Thanks so much, it all works perfectly, like a Swiss clock !! – sebseb Nov 15 '21 at 11:55
0

Calculator.app doesn't have an AppleScript dictionary.

You have to script the UI with System Events

tell application "System Events"
    tell process "Calculator"
        set frontmost to true
        keystroke "c" using command down
    end tell
end tell
vadian
  • 274,689
  • 30
  • 353
  • 361
  • Thanks, @vadian, it works, but only works once. How can I do so that every time I use the calculator it's copying the result ? – sebseb Nov 13 '21 at 12:05