AppleScripts & Keystrokes

Hello everybody!

   I was wondering if it was possible to run an applescript when a keystroke is pressed without the use of a third-party app. An example of this is Alfred, a free app on the app store. With a keystroke (such as Command-A), it will open.

Hi,

it’s not possible with plain-vanilla AppleScript. Consider that Alfred is a third-party app

Could it be done with ASOC?

I think so. In any case you need a running process or daemon which responds to keyboard events

Yes, my application will have ‘Application is Agent’ in the plist file, and it will be my daemon that responds to the events. My question is what code do I write to have it respond to the keystroke?

Maybe this thread is helpful

Thanks! When I was looking through it I found this

So how would I use applescript to toggle the active state of the application?

Okay, did some of my own research, and this is what I learned.

    
script AppDelegate
	property parent : class "NSObject"
  
    on hotKeyDetected_(aNotification)
        log aNotification's userInfo()
    end hotKeyDetected_
	
	on applicationWillFinishLaunching_(aNotification)
        current application's NSEvent's registerHotKeys_notificationName_({{keyName:"hotKey1", keyCode:44, modifier:786432}, {keyName:"hotKey2", keyCode:47, modifier:786432}}, "RDKeyDownNotification")
        set Noter to current application's NSNotificationCenter's defaultCenter()
        Noter's addObserver_selector_name_object_(me, "hotKeyDetected:", "RDKeyDownNotification", missing value)
	end applicationWillFinishLaunching_
	
	on applicationShouldTerminate_(sender)
		-- Insert code here to do any housekeeping before your application quits 
		return current application's NSTerminateNow
	end applicationShouldTerminate_
	
end script

So what I want to figure out is how to know what key code it is. So an example would be that the user would choose the key code and then it would work with that key code.

Download Full Key Codes on softwares.bajram.com

Thanks Stefan, but when I downloaded it Finder told me that PowerPC application are no longer supported. I have OS X 10.7 Lion.

Uhg… This is a hard problem, and I seem to be stuck. All I need to do is have my application run a shell script when a certain key code is pressed. When I try the methods in the thread you sent me, I am constantly getting errors, such as : Undefined symbols for architecture x86_64:
“_main”, referenced from:
start in crt1.10.6.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

If it isn’t annoying, could you please send me what I am suppose to do since I can’t seem to figure it out on my own…

the symbol error indicates a missing (non-linked) framework.

I recommend to move the question to the AppleScriptObjC forum.

Thanks stefan, I will!