No result from AppleScript Calculator

I am unable to get a result from a simple AppleScript calculator set of commands.

The calculator shows the right result, but I cannot capture the result to place in the clipboard and read it.

I also ran this in the script editor.

Answered by mrSFguy in 813927022

I got it to work.

The solution is to reference the item in the window and to stick a couple of small delays in at the end.

Accepted Answer

I got it to work.

The solution is to reference the item in the window and to stick a couple of small delays in at the end.

Hi

first the code

tell application "Calculator" to activate
tell application "System Events" to tell application process "Calculator"
	set frontmost to true
	keystroke "(6+2)*20="
	repeat 10 times
		click menu item "Copy" of menu 1 of menu bar item "Edit" of menu bar 1
		if (the clipboard) is not "" then return the clipboard
		delay 0.3
	end repeat
end tell

UI scripting is much easier if you write your code within a "System Events" block instead of the application's block. You are scripting System Events, not the application.

With UI scripting you have to deal with delays and they vary. Even triggering the same action on a menu/button/... can vary over time. If you just use an arbitrary number like delay 0.1. You will have to set that number to the maximum time if you want that script to work 100% of the time. Wich means, you also wait 100% of the time longer than you have too. With a repeat loop you can set a maximum time you want to wait but don't have to wait that long if it's not necessary.

repeat 10 times
	click menu item "Copy" of menu 1 of menu bar item "Edit" of menu bar 1
	if (the clipboard) is not "" then return the clipboard
	delay 0.3
end repeat

or exit repeat instead of return if you want to do more after the repeat.

if (the clipboard) is not "" then exit repeat

______

Not sure what you try to achieve here. If it's just calculations, then there are better ways to do it. Best to avoid UI scripting if you can. UI scripting is used when you have no other options. But on the other hand, it has saved my bacon so many times.

No result from AppleScript Calculator
 
 
Q