Activate application from non activating panel

I made a project that uses a non activating NSPanel as its main “window”. It’s a program that’s designed to run alongside Adobe Illustrator. The idea is that the user can click on the NSPanel, change contents of its textfield, click its “Apply” button that runs an AppleScript without Illustrator’s interface disappearing.

The problem I have is stopping my NSPanel to be Key. When I finish my script with:

tell application "Adobe Illustrator" to activate

my NSPanel will still be key. One way around I found is to finish the script with:

tell me to activate
tell application "Adobe Illustrator" to activate

Which isn’t pretty as you can see Illustrator popping off and on.

The other way is to make the NSPanel dissapear and re-appear quickly:

tell application "Adobe Illustrator" to activate
mainPanel's orderOut_(0)
mainPanel's orderFront_(0)

Which means you can see the interface of my application popping on and off

Any suggestions for a more elegant way around this?

By the way, I did try:

tell application "Adobe Illustrator" to activate
tell application "System Events"
	tell process "Adobe Illustrator"
		set frontmost to true
		click window 1
	end tell
end tell

with no luck, it looks to me like you can’t reach any windows in Illustrator through GUI scripting.

Assuming you are using ASOC, did you look at NSWindow’s setLevel: ? there is constants to set the level at which the window will be, there is even one that will put it behind the icons on your desktop. You should look at that. If you are not using ASOC then it will be tricky because you will need OBjc and you will have to subclass NSWindow… But it’s not impossible to do.

Browser: Safari 6533.18.5
Operating System: Mac OS X (10.6)

Thanks, but I found the solution. This is the code that does the trick:

set theApp to current application's class "NSApplication"'s sharedApplication
theApp's deactivate()

FYI, you can use the enum NSApp as a shortcut for 'current application’s class “NSApplication”‘s sharedApplication’. So:

tell current application's NSApp to deactivate()

Thanks. I did see “NSApp” mentioned in the NSApplication class reference but couldn’t figure how to make use of it.