Setting focus back to the current application

I have put together a script to change my default Printer. I found it here but when I ran it the List wouldn’t be the activated application so I added a “System events” to allow me to use the keyboard to select the printer. Here is my script I hobbled together:

tell application "Printer Setup Utility"
	set printerlist to name of every printer
end tell
tell application "System Events"
	activate
	choose from list printerlist with prompt "Choose which Printer:"
end tell
set thePrinter to item 1 of result

tell application "Printer Setup Utility"
	set current printer to first printer whose name is thePrinter
end tell

So now when I run the script I can select the printer with the keyboard but after the script finishes I need to apple+tab out and back into the application I was in in order to continue working on it. Does anyone know how to get the application I was working in (not always the same) to activate again?

Thanks,
Brad

Model: Macbook 2.66 i7, SSD
AppleScript: 2.1.2
Browser: Safari 534.30
Operating System: Mac OS X (10.6)

Hi.

If I’ve understood what you’re saying, the best way to get a dialog to appear in front of everything else is to get whatever’s the frontmost application already to display it.

tell application "Printer Setup Utility"
	launch
	set printerlist to name of every printer
end tell

tell application (path to frontmost application as text)
	set theChoice to (choose from list printerlist with prompt "Choose which Printer:")
end tell
if (theChoice is false) then error number -128 -- "Cancel" button clicked.

set thePrinter to item 1 of theChoice

tell application "Printer Setup Utility"
	set current printer to printer thePrinter
	quit
end tell

That did it. I guess I was thinking “System Events” had to display the List.

Thank you so much.