Getting a script to wait for user input


i’m brand new to applescript and very quickly ran into the problem of applescript not being able to wait on a button being pushed… after a few days of experimenting, i came up with the attached solution…
(the app i need this for isn’t textedit but i used textEdit for this example since i assume everyone has it and it uses the same print dialog… basically, for this portion of the script, i need to get the user’s desired paper size for printing which will then be used to set the window size to the same aspect ratio)

[EDIT] i guess i should state the goal :slight_smile: …i want the ‘page setup…’ dialog to come up then the user can select and/or change their paper size and orientation… when they press ‘ok’ (or cancel) within the actual dialog, the dimensions of their paper has been assigned to a variable for use further along in the script…

[EDIT2] …for this bit to work, you’ll have to have text edit and a window open…:wink:

tell application "TextEdit"
	tell application "System Events" to tell process "TextEdit"
		set AppleScript's text item delimiters to {" "}
		click menu item "Page Setup." of menu 1 of menu bar item "File" of menu bar 1
		repeat until focused of text area 1 of scroll area 1 of window 1 = true
				set psz to value of static text 2 of sheet 1 of window 1
				if value of radio button 1 of radio group 1 of sheet 1 of window 1 = 1 then
					set PaperWidth to first text item of psz
					set PaperHeight to third text item of psz
					set PaperWidth to third text item of psz
					set PaperHeight to first text item of psz
				end if
			end try
		end repeat
		display dialog "width = " & PaperWidth & "
height = " & PaperHeight
	end tell
end tell

i guess my question is: Is there anything inherently wrong with this approach? in the end, it does do what i want it to do but i’m concerned with the fact that the variables are being updated so often while in the repeat cycle… is this burning through system resources or taxing the system in a way i need to be concerned with?
or, is there a better approach to making something like this happen using applescript?

Model: quad2.66macPro/ 15"i7mbp
Browser: Safari 534.51.22
Operating System: Mac OS X (10.7)

Honestly, without digging into the dictionary of the Print dialog, I think you pretty much did it the best way that Applescript allows. Since it’s only running when you activate it, I dont think you need to worry about resources it uses - really, since it’s not calculating or launching anything that isn’t already active, you should be ok with this approach.

However, if you want to remove the overhead…


       repeat until focused of text area 1 of scroll area 1 of window 1 = true


delay 3
		activate focused of text area 1 of scroll area 1 of window 1 

and remove the closing

end repeat

The delay will give it a chance to make the top window active. You may have to set it longer for slower computers.