Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 23, 2012, 04:20 PM   #1
Switch46
macrumors newbie
 
Join Date: Nov 2012
Run script on Screen Lock

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 by Switch46; Nov 23, 2012 at 08:39 PM. Reason: Clarifying
Switch46 is offline   0 Reply With Quote
Old Nov 24, 2012, 01:33 AM   #2
bumper314
macrumors member
 
Join Date: Jun 2009
Check out Digital Sentry
bumper314 is offline   0 Reply With Quote
Old Nov 24, 2012, 08:43 AM   #3
Switch46
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
Quote:
Originally Posted by bumper314 View Post
Check out Digital Sentry
Thanks for the tip but that program doesn't have the feature to take action on screen lock, only sleep & logout etc
Switch46 is offline   0 Reply With Quote
Old Nov 24, 2012, 11:47 AM   #4
mfram
macrumors 6502a
 
Join Date: Jan 2010
Location: 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 is online now   0 Reply With Quote
Old Nov 24, 2012, 12:33 PM   #5
mfram
macrumors 6502a
 
Join Date: Jan 2010
Location: 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);
}
mfram is online now   0 Reply With Quote
Old Nov 26, 2012, 10:30 AM   #6
Switch46
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
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
Switch46 is offline   0 Reply With Quote
Old Nov 26, 2012, 02:24 PM   #7
mfram
macrumors 6502a
 
Join Date: Jan 2010
Location: 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.
mfram is online now   0 Reply With Quote
Old Nov 26, 2012, 02:32 PM   #8
Switch46
Thread Starter
macrumors newbie
 
Join Date: Nov 2012
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 by Switch46; Nov 26, 2012 at 02:41 PM.
Switch46 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Using Automator / Apple script to run OCR retrospectively duffel7 Mac Basics and Help 3 Jun 21, 2014 06:30 PM
How can I get OSX to run a script weekly tekboi Mac Programming 3 Mar 10, 2014 04:37 PM
How to create a shell script and run it? Killwin98 Mac Programming 3 Mar 18, 2013 06:39 PM
Run Script When iPhone Connected Baklava OS X 3 Feb 27, 2013 06:42 AM
run script when system is idle? yukonluke Mac Programming 0 Aug 10, 2012 04:52 PM

Forum Jump

All times are GMT -5. The time now is 11:21 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC