Run code after app finishes launching?

Hey there

I am writing an app that will be run when a user logs in, and I have some code that it needs to run as soon as the app finishes launching.

Since this is an AppleScript Studio app with a GUI, it needs to wait for the nib to load and all the components to appear before running this code otherwise.

The reason for this is that if I run the main code immediately, the window doesn’t get set key/main whatever until after the main code has finished, so the interface components, such as my progress bar, stay greyed out.

The app is working perfectly in every way other than that it doesn’t set the window to active or whatever before proceeding to run the main code, where do I need to put this main code to get it run run once everything in the app is loaded and the main window is active?

Sorry if it’s not clear what I’m asking, as this is a little difficult to explain, since I’m new to all this really!

Thanks very much if anyone can help!
Ryan

The place I always refer to to remind me which order the different launch-time events occur is the documentation for the “awake from nib” handler. You are wise to understand already that certain code MUST occur in certain event handlers, largely due to how and when objects are loaded from the nib. If you try to perform actions that effect interface elements before they “exist”, you’re going to run into problems and oftentimes this is a difficult one to catch when you’re not used to troubleshooting ASStudio development. The handler you’re probably looking for is the ‘launched’ handler, which is fired only once, and AFTER the nibs awaken.

Something I point out a lot when questions about launch-time come up, is the use of the “Visible at launch time” property of windows. While this may seem like an easy way to get out of writing a line of code to do the same thing, it can and often does become an issue for some. It seems that when windows are set to be visible at launch using this option, they do not fit into the loading order in the way you would expect. While they must load from nib at the correct time, it seems that they aren’t necessarily available for reference when the application-level handlers are called. This often leads to unexpected errors that can be hard to figure out. I NEVER use the ‘Visible at launch time’ option for windows, and instead choose to always manually show windows. If you want to open a window at launch-time, simply add a ‘launched’ handler to your application object (“File’s Owner”) and put a show window command in it. That way you can be sure that everything is loaded and in order before trying to make references to the window that may break. In your case, I’d de-select the visible at launch-time option, add a show window command to your launched handler, and then add all of your other code to execute after that in the launched handler.

j

Hey there

Thanks for the advice. I tried deselecting the ‘visible on launch’ attirbute for my main window, and set the on launched method to display the window and tested it out and it worked, so then I tried putting the call to the main program code after displaying the window, but the window appears and the code starts before the interface components get focus, so they all stay greyed out. Its not until the main code finishes executing that the window gets focus and the interface components become colour and not greyed out.

I’ve tried putting a delay after displaying the window and calling the main code method, but to now avail.

My current work around is to call the main program code from the ‘on idle’ handler, and use a boolean variable to ensure it only ever gets called once. That works! The interface loads, the window is displayed and gets focus, then the idel handler is triggered and the main code kicks off the processing I’m doing!

If anyone knows of a more elegant way to do this, I’d be grateful.

Thanks
Ryan