KM Macros Can’t Find All Calculator Buttons

I often use the Mac calculator and have assigned keystrokes to many of its buttons to hasten calculations. For example, with Keyboard Maestro I’m able to press ⌘T to calculate the tangent, by assigning this key stroke to the action “Press button named: tangent”. I find the button names in the file “Calculator.app/Contents/Resources/en.lproj/Function.xml”. This works for many of the buttons, including “natural logarithm” and “10 to the x”. But it doesn’t work for all, including “squared” and “inverse tangent”. The KM error is «Macro Cancelled: Press Button “squared” failed to find button.». I don’t see any pattern for the failures.

Any ideas what might be causing these errors?

Hey Andy,

Well...

Apple is not that good at building bug free software, and they are really good at not fixing existing bugs...

I'm looking at the Calculator on Mojave here, so things may have changed.

It looks to me like Apple flubbed a whole lot of the accessibility elements of the app.

-Chris

tell application "System Events"
   tell application process "Calculator"
      tell (first window whose subrole is "AXStandardWindow")
         tell group 3
            name of UI elements
         end tell
      end tell
   end tell
end tell

Result:

{
   missing value, 
   missing value, 
   missing value, 
   "sine", 
   "hyperbolic cosine", 
   missing value, 
   missing value, 
   missing value, 
   "x root of y", 
   "hyperbolic sine", 
   missing value, 
   missing value, 
   "natural logarithm", 
   "logarithm base 10", 
   "tangent", 
   "hyperbolic tangent", 
   "cosine", 
   missing value, 
   missing value, 
   missing value, 
   missing value, 
   missing value, 
   missing value, 
   "10 to the x", 
   missing value, 
   missing value, 
   missing value, 
   missing value, 
   missing value, 
   "e to the x"
}

What do you know. Looks like I’ll have to implement the rest as messy mouse clicks.

Thanks,

—Andy

You can use Applescript UI scripting if you want, but clicks relative to the window will be easier for someone not familiar with UI scripting.

Thanks — looks like that’s not a solution because these missing UI elements are not properly identified in the Calculator code — unless they can be identified by relative position?

The good news is that Apple have gone against form, at least in Ventura. Your script now returns

{")", "(", "1/x", "sine", "hyperbolic cosine", "x2", "√x", "∛x", "y root of x", "hyperbolic sine", "xy", "x3", "natural logarithm", "logarithm base 10", "tangent", "hyperbolic tangent", "cosine", "x!", "π", "Rand", "e", "EE", "Rad", "10 to the x", "2nd", "mc", "m+", "m-", "mr", "e to the x"}

...from the Scientific Calculator view.

@aanderson, hopefully you can get what you need from that, e.g. "squared" is actually x2 -- assuming you're also on Ventura, of course!

2 Likes

Interesting. These additional button names worked even in my current Mojave: x2, √x, and ∛x. The other new ones, including x3, still failed. Once I finally upgrade, hopefully more of them will, too (I’m still busily converting hundreds of QuicKeys to KM!).

1 Like