How do you tell when a certain window in your app has become the frontmost window? And when it resigns from being the frontmost?
Use the Notifications
→ NSWindowDidBecomeKeyNotification
→ NSWindowDidResignKeyNotification
Look at the documentation for NSWindow
How do i reference the specific window I want. I cant subclass it.
property myWindow : missing value
Have you gone through the tutorials on unscripted?
Start with tutorial 5 and then 1-4.
I know how to actually do that but using the NSWindowDidBecomeKeyNotification how do i get that to work for the “mywindow” and would you write it as:
on NSWindowDidBecomeKeyNotification_(aNotification)
– were does the reference to the window go in here?
end NSWindowDidBecomeKeyNotification_
# Add this to awakeFromNib
tell current application's class "NSNotificationCenter"
addObserver_selector_name_object_(me, "windowBecameKey:", "NSWindowDidBecomeKeyNotification", missing value)
end
# method called when notification comes in
on windowBecameKey_(notification)
# do stuff here
end
Dont’ forget to add your class as the delegate of the window
For some reason its not working for me. I delegated the class’ to the window in IB and pasted the script over. The first part is in awakefromnib and so on. But nothing happens. Here is what the console says:
2010-05-01 17:15:50.798 fadeitmes[21612:a0f] +[NSNotificationCenter addObserver:selector:name:object:]: unrecognized selector sent to class 0x7fff70f04188
2010-05-01 17:15:50.809 fadeitmes[21612:a0f] *** -[windowcamekey awakeFromNib]: +[NSNotificationCenter addObserver:selector:name:object:]: unrecognized selector sent to class 0x7fff70f04188 (error -10000)
Try this.
tell current application's class "NSNotificationCenter"
its defaultCenter's addObserver_selector_name_object_(me, "windowBecameKey:", "NSWindowDidBecomeKeyNotification", missing value)
end
It worked! But for some reason when the window resigns from the frontmost window within the same application it will return “Not key” then “Key” at the same time and stay key even though it isn’t the frontmost window within the application. So in actuality it hasn’t done what I needed. It basicly is doing what “applicationWillBecomeActive” would do much easier, and in addition when you switch apps and try to come back sometimes you cannot return to the app it just stays in the background because of this action.
Can this be fixed? Anyhow thanks for all the help!