Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: applescripting the calculator



tom wible wrote on Thu, 25 Jan 2007 19:18:46 -0500:

>this works:
>
>tell application "Calculator" to activate
>tell application "System Events" to tell process "Calculator"
>	tell group 1 of window 1
>		click button "Clear"
>		click button "9"
>		click button "plus"
>		click button "5"
>		click button "Equals"
>	end tell
>	keystroke "c" using {command down}
>end tell
>set x to (the clipboard) as number

If the object of the exercise is to practise clicking Calculator's
buttons, you should allow for the fact that the UI geography depends on
what "View" is currently active: "Basic", "Scientific", or "Programmer".
Also, in Programmer View, the names of the operator buttons are slightly
different and the number format might be set to hexadecimal or octal,
which in most cases won't be coercible to number. Finally, it's possible
that the 'the clipboard' command could be executed before the effect of
using 'keystroke "c" using {command down}' filters through to the clipboard.

A simpler and faster-acting way to script the Calculator (assuming only
simple math ops are required) would be to replace the clicks with
keystrokes and to read the results directly from the display:

  tell application "Calculator" to activate
  tell application "System Events"
    keystroke "1" using {command down} -- Force Basic View.
    key code 71 -- Clear.
    keystroke "9+5="
    set x to (name of static text 1 of group -1 of window 1 of
application process "Calculator") as number
  end tell

If you don't want to change the current View, you could instead click the
"Dec" button if it exists:

  tell application "Calculator" to activate
  tell application "System Events"
    tell application process "Calculator" to tell window 1
      tell button "Decimal input" of group 1
        if (it exists) then click
      end tell
      key code 71
      keystroke "9+5="
      set x to (name of static text 1 of group -1) as number
    end tell
  end tell


NG

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-users/email@hidden
Archives: http://lists.apple.com/mailman//archives/applescript-users

This email sent to email@hidden



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.