aboutsummaryrefslogtreecommitdiffstats
path: root/On Task.m
blob: 9b740b57ea076b8d8aa2cd6fd2f492ec2f112fb9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#import <AppKit/AppKit.h>

#import "NSFileManager+DirectoryLocations.h"

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	// Get Application Support dir path
	NSString *applicationSupportPath = [[NSFileManager defaultManager] applicationSupportDirectory];
	
	// Get a date string
	[NSDateFormatter setDefaultFormatterBehavior:NSDateFormatterBehavior10_4];
	NSDateFormatter *df = [[NSDateFormatter alloc] init];
	[df setDateFormat:@"yyyyMMddHHmmss"];
	NSDate *now = [NSDate date];
	NSString *date = [df stringFromDate:now];
	
    // # Take screenshot
	// Make screenshots directory if needed
	NSString *screenshotsPath = [applicationSupportPath stringByAppendingString:@"/Screenshots"];
	if (![[NSFileManager defaultManager] fileExistsAtPath:screenshotsPath]) {
		if (![[NSFileManager defaultManager] createDirectoryAtPath:screenshotsPath attributes:nil]) {
			NSLog(@"Error: Could not create folder %@", screenshotsPath);
		}
	}
	
	NSTask *takeScreenshot = [[NSTask alloc] init];
	[takeScreenshot setLaunchPath:@"/usr/sbin/screencapture"];
	[takeScreenshot setArguments:[NSArray arrayWithObjects:@"-SxC", [applicationSupportPath stringByAppendingString:[NSString stringWithFormat:@"/Screenshots/%@.png", date]], nil]];
	[takeScreenshot launch];
	[takeScreenshot waitUntilExit];
	[takeScreenshot release];
	
	
	// # Play system beep
	NSSound *beep = [NSSound soundNamed:@"Tink"];
	[beep play];
	while ([beep isPlaying]) {
		// Don't exit the program before the sound finishes
	}
	
	
	// # Open text log
	[df setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
	date = [df stringFromDate:now];
	NSString *logPath = [applicationSupportPath stringByAppendingString:@"/On Task Log.txt"];
	
	NSFileHandle *log_f = [NSFileHandle fileHandleForUpdatingAtPath:logPath];
	if (log_f == nil) {
		[[NSFileManager defaultManager] createFileAtPath:logPath contents:nil attributes:nil];
		log_f = [NSFileHandle fileHandleForUpdatingAtPath:logPath];
	}
	else {
		[log_f seekToEndOfFile];
		[log_f writeData:[[NSString stringWithString:@"\n"] dataUsingEncoding:NSUTF8StringEncoding]];
	}
	[log_f writeData:[[date stringByAppendingString:@": "] dataUsingEncoding:NSUTF8StringEncoding]]; // Write date stamp
	[log_f closeFile];
	
	[[NSWorkspace sharedWorkspace] openFile:logPath withApplication:@"TextEdit"];
	
	
	// Cleanup
	[df release];
	
    [pool release];
    return 0;
}