• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: applescripting the calculator
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: applescripting the calculator


  • Subject: Re: applescripting the calculator
  • From: kai <email@hidden>
  • Date: Fri, 26 Jan 2007 03:45:32 +0000


On 26 Jan 2007, at 00:18, tom wible wrote:

didn't work if calculator wasn't already running, then it bombed on the activate, then it bombed on the 'as number' inside the finder tell...

There were one or two issues involved with the script suggested, Tom. Since 'activate' is an application command, the application that became activated was actually System Events. This caused the keystroke command to fail, so the script then attempted to coerce the clipboard's current contents (that is, from the last copy operation performed) to a number. If the value could be coerced, then the resulting number (whatever it happened to be) would be returned; if not, an error number -1700 [errAECoercionFail] occurred.


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

As an exercise in GUI scripting, it's not necessary to use the clipboard here - nor essential to even activate Calculator.


For example:

----------------------

to launch_calculator()
	launch application "Calculator"
	tell application "System Events" to tell window 1 of ¬
		process "Calculator" to repeat until exists
		delay 0.1
	end repeat
end launch_calculator

to perform_calculation(calc)
set text item delimiters to space
tell calc to set calc to {"clear"} & text item 1's characters & text from ¬
text item 2 to text item -2 & text item -1's characters & "equals"
if calc's item 2 is "-" then set calc's item 2 to "minus"
set text item delimiters to {""}
tell application "System Events" to tell window 1 of process "Calculator"
tell group 1 to repeat with b in calc
if b's contents is "-" then set b to "minus"
click button b
end repeat
value of group 2's static text 1 as number
end tell
end perform_calculation


set calc_list to {"9 plus 5", "2.4 minus 1", "0.0035 times 40", "-19.796 divided by 1414"}

launch_calculator()
repeat with calc in calc_list
	set calc's contents to perform_calculation(calc)
end repeat

calc_list --> {14, 1.4, 0.14, -0.014}

----------------------

However, if we're activating the app anyway, then we could use something like:

----------------------

to perform_calculation(calc)
	tell application "System Events" to tell process "Calculator"
		keystroke "c" & calc & "="
		value of window 1's group 2's static text 1 as number
	end tell
end perform_calculation

set calc_list to {"9+5", "2.4-1", "0.0035*40", "-19.796/1414"}

activate application "Calculator"
repeat with calc in calc_list
	set calc's contents to perform_calculation(calc)
end repeat

calc_list --> {14, 1.4, 0.14, -0.014}

----------------------

Then again (as has already been mentioned), we could simply:

----------------------

set calc_list to {"9 + 5", "2.4 - 1", "0.0035 * 40", "-19.796 / 1414"}

repeat with calc in calc_list
	set calc's contents to run script calc
end repeat

calc_list --> {14, 1.4, 0.14, -0.014}

----------------------

---
kai


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

This email sent to email@hidden
References: 
 >Re: applescripting the calculator (From: tom wible <email@hidden>)

  • Prev by Date: Re: More FMP8 Questions
  • Next by Date: Re: Where does Mail store deleted messages?
  • Previous by thread: Re: applescripting the calculator
  • Next by thread: Re: applescripting the calculator
  • Index(es):
    • Date
    • Thread