Run script on Screen Lock

Discussion in 'Mac Programming' started by Switch46, Nov 23, 2012.

  1. Switch46, Nov 23, 2012
    Last edited: Nov 23, 2012

    macrumors newbie

    Joined:
    Nov 23, 2012
    #1
    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.
     
  2. macrumors member

    Joined:
    Jun 23, 2009
  3. thread starter macrumors newbie

    Joined:
    Nov 23, 2012
    #3
    Thanks for the tip but that program doesn't have the feature to take action on screen lock, only sleep & logout etc
     
  4. macrumors 6502a

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #4
    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.
     
  5. macrumors 6502a

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #5
    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);
    }
    
     
  6. thread starter macrumors newbie

    Joined:
    Nov 23, 2012
    #6
    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
     
  7. macrumors 6502a

    Joined:
    Jan 23, 2010
    Location:
    San Diego, CA USA
    #7
    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.
     
  8. Switch46, Nov 26, 2012
    Last edited: Nov 26, 2012

    thread starter macrumors newbie

    Joined:
    Nov 23, 2012
    #8
    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.
     

Share This Page