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/ 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 + 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]; diff --git a/WakeWatcher/main.m b/WakeWatcher/main.m index 1035dac..31a5510 100644 --- a/WakeWatcher/main.m +++ b/WakeWatcher/main.m @@ -1,17 +1,18 @@ // -// 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" int main(int argc, const char * argv[]) { - // TODO: run under launchd @autoreleasepool { NSLog(@"Starting Wake Watcher!"); @try { 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 + + +