Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Switch46

macrumors newbie
Original poster
Nov 23, 2012
5
0
Hey guys,

I want the application TeamViewer to start when I Lock my Imac trough either the key chain way or using the hot corner method. And quit again upon unlocking.
Does anyone know how to code the ways, mentioned above, of locking the screen?
I couldn't find anything on the web. I am fine with using either a bash script of using apple script.

Any help would be appreciated.
 
Last edited:

mfram

Contributor
Jan 23, 2010
1,307
343
San Diego, CA USA
I'm looking at the document for the NSWorkspace class. It seems you can ask for notifications when applications launch. I wonder if there's a notification when the screen lock "application" launches. Not sure.

Don't know how you'd get that from a script though. I'm not that familiar with AppleScript. Maybe it can do it.

Oh, and there is a "NSWorkspaceScreensDidSleepNotification" and "NSWorkspaceScreensDidWakeNotification". That may be what you want.
 

mfram

Contributor
Jan 23, 2010
1,307
343
San Diego, CA USA
Ok, I played with this a little bit. I used the NSWorkspace notificationCenter to get notifications. The "NSWorkspaceScreensDidSleepNotification" will fire when the screen sleeps (ie., power off). In order to see when the screen saver starts, I asked for the NSWorkspaceDidActivateApplicationNotification. When the screen saver became active, the application name I got was "ScreenSaverEngine". The only thing that was a little goofy was that when the screen saver turns off, the ScreenSaverEngine activates again before moving onto the next application.

You could work with this a bit to launch a script when the Screen Saver launches.

Code:
- (void)registerForNotifications
{
    [[[NSWorkspace sharedWorkspace] notificationCenter]
                                    addObserver:self
                                    selector:@selector(mScreensSleep:)
                                    name:NSWorkspaceScreensDidSleepNotification
                                    object:nil];
    
    [[[NSWorkspace sharedWorkspace] notificationCenter]
                                    addObserver:self
                                    selector:@selector(mScreensWake:)
                                    name:NSWorkspaceScreensDidWakeNotification
                                    object:nil];

    [[[NSWorkspace sharedWorkspace] notificationCenter]
                                    addObserver:self
                                    selector:@selector(mAppLaunch:)
                                    name:NSWorkspaceDidActivateApplicationNotification
                                    object:nil];
}

- (void)mScreensSleep:(NSNotification *)notification
{
    NSLog(@"got sleep notification");  
}

- (void)mScreensWake:(NSNotification *)notification
{
    NSLog(@"got wake notification");
}

- (void)mAppLaunch:(NSNotification *)notification
{
    NSRunningApplication *runApp = [[notification userInfo] 
                            objectForKey:NSWorkspaceApplicationKey];
    
    NSLog(@"got app launch %@", runApp.localizedName);
}
 

Switch46

macrumors newbie
Original poster
Nov 23, 2012
5
0
Thank you very much, I should be able to get it running around this. I didn't expect this much of an effort. Thanks again
 

mfram

Contributor
Jan 23, 2010
1,307
343
San Diego, CA USA
I don't know what language you are comfortable doing your implementation in. But there's another thread called "Notifications" which shows how to watch these types of notifications in AppleScript instead of Objective-C if you are more comfortable with that.

As for a Cocoa/Obj-C version, if I were going to implement this completely I would probably code is as a "Status Bar" app.
 
  • Like
Reactions: jagooch

Switch46

macrumors newbie
Original poster
Nov 23, 2012
5
0
I am going to go for objective-c, the status bar app seems like a rather good idea. As this is a bit of a pet project and as I am rather new to programming(C++ and Pascal) it will take a while but I'll let you know if it worked out.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.