Customizing Application startup Behavior [Key down on launch]

I’d like to add a “hidden” preferences window to an ASS app that can be invoked by holding down a key or sequence of keys at startup. Does anyone have a tried-and-true method for doing this?

  1. Create a subclass of NSObject in IB, and name it “StartupController”.
  2. Create files for the subclass by selecting the subclass in the “Classes” pane in IB, and then selecting “Classes > Create Files for StartupController” in the IB main menu.
  3. Add an “on launched” handler to your ‘files owner’ object, and connect it your your script.
  4. Save your UI and go to Xcode.
  5. Find your new StartupController files, and place the following code into each of the files, respectively…
  1. Add the following call to your script to check if the key is down and act accordingly…
on launched theObject
	set ctrlKeyDown to (call method "controlKeyDown" of class "StartupController") as boolean
	if (ctrlKeyDown) then show window "StartupWindow"
end launched

Obviously you need to make sure that your window isn’t set to open automatically on launch in IB. See the comment in the StartupController.m page for other function keys you can test for.

j

I tried this method and could not get it to work – got build errors involving not recognizing “GetCurrentKeyModifiers”. Apparently this method is not too easy. I found this:

http://www.cocoadev.com/index.pl?TestForKeyDownOnLaunch

Not terribly important for my purposes but thought I’d post on the troubles here.

Thanks.

I had an error while building:

Edit: Building was successful after including the Carbon framework in my project, but now the app crashes (SIGSEGV).

Model: MacBook Pro (2.16 GHz)
Operating System: Mac OS X (10.4.9)

Thanks Bruce,

My code compiles OK with the #include <Carbon/Carbon.h> at the top of the StartController.h file (that is the correct place to include Carbon, right?) But I get an error on my g4 17 laptop running 10.4.8

And now, back at my office, (Grr!) it works on my desktop dual g4 1Ghz running 10.4.8! I’m not sure what is different (well, probably a lot of things :).

Maybe this is off topic, but is there a way to step through (debug) an AS studio app? Placing breakpoints does not do anything during the build or running. Maybe if I can step through some of it, the symptoms will become clearer.

Thanks for the help, this is my first project with xcode, I’m a little wet behind the ears.

Model: Mac Dual G4 1Ghz, 2Gb
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

I am trying to compile these examples, but seem to be encountering some errors. I’m using the terminal commands

cc -c StartupController.h

in hopes of getting a StartupController.o file that I can then

cc StartupController.m StartupController.o -o StartupController

Is this the right way to do it or are you all using Xcode?

Also is it possible to combine these two into one .m file such as:

#import <Cocoa/Cocoa.h>

@interface StartupController : NSObject {}
+(BOOL)controlKeyDown;
@end

@implementation StartupController
+(BOOL)controlKeyDown {
return ((GetCurrentKeyModifiers() & controlKey) != 0);
//… you can also use ‘optionKey’, ‘cmdKey’, or ‘shiftKey’ in place of “controlKey”
}
@end

After the Usual amount of confusion, I was able to get this work by going the xcode route with separate .m and .h files

I used the xcode applescript droplet template and got the droplet part to work with the modifier keys:

AppleScript Error
-1708

-1708 doesn’t understand the message.

And it does not exit?
What template can I start with for a clickable and droplet?

Is it also possible to customize droplet behavior?
I tried to include the method call in a on open names handler, but it does not work.
It does not return anything.
How should I go about this?

Thanks in advance

Jobo, Just wanted to say thanks for this code.

I wanted to detect option key down, when doing a drag and drop onto a table view.

The table view is set to delete every row when new files are dropped.
But I wanted it to add the new files to the table view and also keep the old ones if option key was down.

I did not need to use the on launch or link the ‘file owner’.
Instead I already had a on drop handler for the table view linked and just put the call at the top of that.

I am sill very new at playing with Applescript Studio, and not sure if there are other ways to get the behavior I was after,
But your code worked first time for me.

So Thank you. :smiley: