i wonder if it is possible to change a buttons title/name/content when the user presses a key (like the alt key by example)
so the button says “skip” and if the user presses the alt key the button changes to “skip always”.
that would be really nice!
I don’t think setting the title is jns’ problem … the problem is probably catching modifier key events application wide. I thought a little while about this problem because I felt there has to be an easier solution but this is the only one I found:
subclass NSApplication in Interface Builder and create files for the subclass (for example named MyApplication)
set MyApplication as custom class for ‘File’s Owner’
connect ‘File’s Owner’ with your AppleScript - which handler does not matter, no script inside of the handler is needed.
set MyApplication as ‘Principal Class’ in your Xcode project → Edit active target → Properties
add this handler in your AppleScript:
on optionkeyEvent(keysState)
tell button "but" of window "win"
if (keysState as boolean) then
set title to "skip always"
else
set title to "skip"
end if
end tell
end optionkeyEvent
add this code in the NSApplication subclass files:
I was able to get your example to work. Works great! Where can I learn more about this stuff? What books or websites did you learn this from? At the moment, I only know Applescript.
I made my fist steps into Objective-C after playing some time with the new AppleScript Studio some years ago. I saw it’s limitations and so I decided to learn some Objective-C. Though I am not at all the learning-from-books-type I read (and worked through all tutorials and examples of ) ‘Learning Cocoa with Objective-C’ (O’Reilly). I already had some C-programming experience so this gave me a good base. (Note - this book might be dated - I don’t know if there is a new edition). Everything else I learned from reading the docs, studying examples from Apple and other sources on the web, reading cocoa-dev@lists.apple.com and trial&error.
here some comments for the above solution:
Since I want to catch the keyboard events not only for the current firstResonder, I subclassed NSApplescript’s method ‘sendEvent:’ since that’s the point where every Event for this application is distributed:
- (void)sendEvent:(NSEvent *)anEvent{
// override the method by writing my own ‘version’ in the subclass
if ([anEvent type] == NSFlagsChanged) {
// checking if a modifier key has changed (up or down)
[self callAsHandler:(([anEvent modifierFlags] & NSAlternateKeyMask) != 0) ];
// call the method ‘callAsHandler’ with a boolean argument depening on the current state of the Option key
}
[super sendEvent:anEvent]; // executing the regular stuff of the ‘sendEvent:’ method
}
For a better understanding of this particular subject you should read sth about handling events and the responder chain in the docs …
In my new method ‘callAsHandler:’ I execute one of two AppleScript objects that i have inited and compiled in ‘awakefromNib’:
asOptKeyUp=[[NSAppleScript alloc] initWithSource:@“(get my script)'s optionkeyEvent(0)”]; // init a new AppleScript:
// get my script - results the applescript we connected to ‘File’s Owner’ in Interface Builder
// and (…)'s optionkeyEvent(0) calls it’s handler optionkeyEvent with argument 0
[asOptKeyUp compileAndReturnError:nil]; // compile the AppleScript for later use