From 4a099d46373c45597448311fd13dfd484cf2509b Mon Sep 17 00:00:00 2001 From: johnmikep <45859899+johnmikep@users.noreply.github.com> Date: Mon, 4 May 2020 13:30:19 -0500 Subject: [PATCH 1/6] Update main.m --- WakeWatcher/main.m | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/WakeWatcher/main.m b/WakeWatcher/main.m index 1035dac..79ded16 100644 --- a/WakeWatcher/main.m +++ b/WakeWatcher/main.m @@ -1,21 +1,22 @@ // -// main.m +// WakeWatcher.m // WakeWatcher // // Created by Eric Kobrin on 7/23/16. +// Updated by John Peterson on 5/1/20 // Released under CC0 1.0 (Public Domain) // https://creativecommons.org/publicdomain/zero/1.0/ -// + + #import -#import "WakeWatcher.h" +#import "wakeWatcher.h" int main(int argc, const char * argv[]) { - // TODO: run under launchd @autoreleasepool { NSLog(@"Starting Wake Watcher!"); @try { - id watcher = [[WakeWatcher alloc] init]; + id watcher = [[wakeWatcher alloc] init]; NSLog(@"Watcher %@", watcher); [watcher registerNotifications]; [[NSRunLoop currentRunLoop] run]; From 872b0cf2720a56dbb36a41092fbaba04d8ffad08 Mon Sep 17 00:00:00 2001 From: johnmikep <45859899+johnmikep@users.noreply.github.com> Date: Mon, 4 May 2020 13:30:55 -0500 Subject: [PATCH 2/6] Update main.m --- WakeWatcher/main.m | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/WakeWatcher/main.m b/WakeWatcher/main.m index 79ded16..31a5510 100644 --- a/WakeWatcher/main.m +++ b/WakeWatcher/main.m @@ -10,13 +10,13 @@ #import -#import "wakeWatcher.h" +#import "WakeWatcher.h" int main(int argc, const char * argv[]) { @autoreleasepool { NSLog(@"Starting Wake Watcher!"); @try { - id watcher = [[wakeWatcher alloc] init]; + id watcher = [[WakeWatcher alloc] init]; NSLog(@"Watcher %@", watcher); [watcher registerNotifications]; [[NSRunLoop currentRunLoop] run]; From 3464ca6a67061c62d7868db3b395417f7454dcdf Mon Sep 17 00:00:00 2001 From: johnmikep <45859899+johnmikep@users.noreply.github.com> Date: Mon, 4 May 2020 13:31:14 -0500 Subject: [PATCH 3/6] Update WakeWatcher.h --- WakeWatcher/WakeWatcher.h | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/WakeWatcher/WakeWatcher.h b/WakeWatcher/WakeWatcher.h index 9b15595..71e8351 100644 --- a/WakeWatcher/WakeWatcher.h +++ b/WakeWatcher/WakeWatcher.h @@ -1,20 +1,22 @@ // -// WakeWatcher.h +// WakeWatcher.m // WakeWatcher // // Created by Eric Kobrin on 7/23/16. +// Updated by John Peterson on 5/1/20 // Released under CC0 1.0 (Public Domain) // https://creativecommons.org/publicdomain/zero/1.0/ -// + #import @interface WakeWatcher : NSObject -@property (nonatomic, retain) NSString *home; -@property (nonatomic, retain) NSString *path; -@property (nonatomic, retain) NSString *executable; +@property (nonatomic, copy) NSString *home; +@property (nonatomic, copy) NSString *path; +@property (nonatomic, copy) NSString *executable; - (void) registerNotifications; @end + From d2259d964b11f743df3d90aee82d58f0161c2227 Mon Sep 17 00:00:00 2001 From: johnmikep <45859899+johnmikep@users.noreply.github.com> Date: Mon, 4 May 2020 13:32:47 -0500 Subject: [PATCH 4/6] Loop through scripts in /opt/wake directory --- WakeWatcher/WakeWatcher.m | 52 ++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/WakeWatcher/WakeWatcher.m b/WakeWatcher/WakeWatcher.m index 528e9d0..ec9ce34 100644 --- a/WakeWatcher/WakeWatcher.m +++ b/WakeWatcher/WakeWatcher.m @@ -3,11 +3,11 @@ // WakeWatcher // // Created by Eric Kobrin on 7/23/16. +// Updated by John Peterson on 5/1/20 // Released under CC0 1.0 (Public Domain) // https://creativecommons.org/publicdomain/zero/1.0/ -// -#import "WakeWatcher.h" +#import "wakeWatcher.h" @import AppKit; @implementation WakeWatcher @@ -15,35 +15,41 @@ @implementation WakeWatcher - (void) receiveWakeNote: (NSNotification*) note { + NSFileManager *filemgr; + NSArray *filelist; + + filemgr = [NSFileManager defaultManager]; + filelist = [filemgr contentsOfDirectoryAtPath: @"/opt/wake" error: nil]; + NSLog(@"receiveWakeNote: %@", [note name]); - if([[NSFileManager defaultManager] isExecutableFileAtPath:self.path]){ - NSLog(@"Attempting to execute: '%@'", self.path); - NSTask *task; - @try { - [[NSFileManager defaultManager] - changeCurrentDirectoryPath:self.home]; - task = [NSTask launchedTaskWithLaunchPath:self.path arguments:[NSArray arrayWithObject:self.executable]]; - } - @catch (NSException *exception) { - NSLog(@"Error executing: '%@': %@", self.path, exception.reason); + + for (id object in filelist) { + + self.executable = object; + self.home = @"/opt/wake"; + self.path = [self.home stringByAppendingPathComponent:self.executable]; + + if([[NSFileManager defaultManager] isExecutableFileAtPath:self.path]){ + NSLog(@"Attempting to execute: '%@'", self.path); + NSTask *task; + @try { + [[NSFileManager defaultManager] + changeCurrentDirectoryPath:self.home]; + task = [NSTask launchedTaskWithLaunchPath:self.path arguments:[NSArray arrayWithObject:self.executable]]; + } + @catch (NSException *exception) { + NSLog(@"Error executing: '%@': %@", self.path, exception.reason); + } + } else { + NSLog(@"No executable file at: '%@'", self.path); } - } else { - NSLog(@"No executable file at: '%@'", self.path); } + } - (void) registerNotifications { - if(self.executable == nil){ - self.executable = @".onwake"; - } - if(self.home == nil){ - self.home = NSHomeDirectory(); - } - if(self.path == nil){ - self.path = [self.home stringByAppendingPathComponent:self.executable]; - } [[[NSWorkspace sharedWorkspace] notificationCenter] addObserver: self selector: @selector(receiveWakeNote:) name: NSWorkspaceDidWakeNotification object: NULL]; From 67582c7d757339154abcff404be751b8300605d0 Mon Sep 17 00:00:00 2001 From: johnmikep <45859899+johnmikep@users.noreply.github.com> Date: Mon, 4 May 2020 13:43:46 -0500 Subject: [PATCH 5/6] Update README.md --- README.md | 23 +++++++---------------- 1 file changed, 7 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 9546c30..5aae333 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,17 @@ # WakeWatcher - -Waits for system to wake from sleep and executes ~/.onwake -Only works on OS X. +Waits for system to wake from sleep and executes any executables in /opt/wake +Only works on macOS. # Building - Build in Xcode or run `xcodebuild`. There are no dependencies. # Using WakeWatcher +Create a LaunchDaemon to run the items you need. -Launch WakeWatcher.app. Until WakeWatcher supports launchd, consider -making WakeWatcher.app a login item. - -On wake from sleep, if `~/.onwake` exists and is executable, -WakeWatcher will `chdir` to the current user's home directory and -execute `.onwake` +On wake from sleep, WakeWatcher will attempt to run anything under +/opt/wake (*.sh, *.py, *.rb, etc). An example LD is included. # Bugs @@ -25,14 +20,10 @@ WakeWatcher is not configurable. WakeWatcher calls NSLog a lot. -WakeWatcher does not check to see if `~/.onwake` is already executing -before attempting to launch it. - -WakeWatcher does not support launchd. Patches welcome. - +WakeWatcher does not check to see if processes under `/opt/wake` are already executing +before attempting to launch them. # Authorship/Licence - Created by Eric Kobrin. Released under CC0 1.0 (Public Domain) https://creativecommons.org/publicdomain/zero/1.0/ From 4e5d9c4fe41bee3ad9b74dc0211730dbb8dfa954 Mon Sep 17 00:00:00 2001 From: johnmikep <45859899+johnmikep@users.noreply.github.com> Date: Mon, 4 May 2020 13:45:28 -0500 Subject: [PATCH 6/6] Create com.corp.wakeWatcher.plist --- com.corp.wakeWatcher.plist | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 com.corp.wakeWatcher.plist diff --git a/com.corp.wakeWatcher.plist b/com.corp.wakeWatcher.plist new file mode 100644 index 0000000..708aafd --- /dev/null +++ b/com.corp.wakeWatcher.plist @@ -0,0 +1,20 @@ + + + + + Label + com.corp.wakeWatcher + StandardOutPath + /tmp/wakeWatcher.log + StandardErrorPath + /tmp/wakeWatcher.log + RunAtLoad + + KeepAlive + + ProgramArguments + + /path/to/wakeWatcher + + +