No, when my app starts, it doesn't have focus. Yet.
My program is a "quick lookup" app, kinda like Apple's Dictionary. I usually invoke it as a service, when I'm in Mail, OmniFocus, Safari or Terminal. I highlight a string and hit a keyboard shortcut to invoke the app. It starts and passes control to it's applicationDidFinishLaunching method, which looks up the string I highlighted. It writes the result of the lookups into a file, in HTML. We're still in applicationDidFinishLaunching, before the app's own window has displayed, so the previous app still has focus. Then it tells Safari to display the HTML file in a new Safari window, and it activates Safari to bring Safari's windows to the top. At this point, Safari gets focus. Next, applicationDidFinishLaunching exits and my app's window is displayed, and my app gets focus. My app's window consists of only an "OK" button. When I hit RETURN (or click the "OK"), the app's okPushed method is called, which tells Safari to close Safari's top window, and then terminates the app. My problem is that after this sequence of events, Safari is left with the focus. I want focus to end up with whatever app was initially running when the app was invoked. It's essential that my app require the minimum interaction possible: the steps are highlight a string, hit a hot key, hit RETURN, and be back where you were.
In the initial version of this app, I displayed a WebView in my app's window, along with the "OK" button. There was no issue of who had focus. It worked well but for one problem. When the app looks up things, the results can contain content from websites, which may contain HTML links. Often, the user will want to follow the links. A WebView will support this, but even though you can make a WebView support Forward and Back buttons, it's just not as good as a real browser. My colleagues want to start using the app, so I want it to display the HTML using a familiar browser.
I decided to abandon the WebView and use Safari to display the HTML, even though I knew I'd have to deal with focus issues. To prove the concept, I wrote this AppleScript:
set previousApp to the name of the current application tell application "Safari" activate display dialog "Click OK when you're done with the Safari window" close window 1 end tell tell application previousApp to activate
This worked, so I began converting it to the corresponding Scripting Bridge code. I successfully converted the first "tell" block, but I'm having trouble figuring out how to implement the first and last lines with Scripting Bridge. Hence this thread.
BTW, thanks very much for the clue about NSRunningApplication. It seems to be a better way to tell Safari to display a file in a new window, but I don't see how I'll be able to tell Safari to close the window when I'm done with it.
BTW2, I have a copy of your AppleScript book on my shelf. So I guess this is kinda like customer support!
Thanks very much!
-- Pete
On Aug 4, 2010, at 11:48 AM, Matt Neuburg wrote: On or about 8/4/10 9:51 AM, thus spake "Pete Siemsen" < email@hidden>: the one that has focus when my program starts
When your program starts, *your* program has focus. That could be part of your problem here. Maybe you need a time machine so you can jump back in time and see which one *was* frontmost? You could look to see which program is *second*, I suppose, but then you are just guessing of course. m. -- matt neuburg, phd = email@hidden, http://www.tidbits.com/matt/pantes anthropoi tou eidenai oregontai phusei Among the 2007 MacTech Top 25, http://tinyurl.com/2rh4pfAppleScript: the Definitive Guide, 2nd edition http://www.tidbits.com/matt/default.html#applescriptthingsTake Control of Exploring & Customizing Snow Leopard http://tinyurl.com/kufyy8 RubyFrontier! http://www.apeth.com/RubyFrontierDocs/default.html TidBITS, Mac news and reviews since 1990, http://www.tidbits.com
|