forked from jbtule/cdto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
86 lines (73 loc) · 2.71 KB
/
main.m
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//
// main.m
// cd to ...
//
// Created by James Tuley on 2/16/07.
// Improvements by Yang Zhang, 2016.
// Copyright Jay Tuley 2007. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import "CD2Protocol.h"
#import "CD2ITerm.h"
#import "CD2Terminal.h"
// Finder.h Generated by: sdef /System/Library/CoreServices/Finder.app | sdp -fh --basename Finder
#import "Finder.h"
NSString* getPathToFrontFinderWindow() {
FinderApplication* finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.Finder"];
FinderItem *target = [[[finder selection] get] firstObject];
if (target == nil) {
target = [[[[finder FinderWindows] firstObject] target] get];
}
NSURL* url = [NSURL URLWithString:target.URL];
NSData* bookmark = [NSURL bookmarkDataWithContentsOfURL:url error:nil];
NSError* error;
NSURL* fullUrl = [NSURL URLByResolvingBookmarkData:bookmark
options:NSURLBookmarkResolutionWithoutUI
relativeToURL:nil
bookmarkDataIsStale:nil
error:&error];
if (fullUrl != nil) {
url = fullUrl;
}
NSString* path = [[url path] stringByExpandingTildeInPath];
BOOL isDir = NO;
[[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:&isDir];
if (!isDir) {
path = [path stringByDeletingLastPathComponent];
}
return path;
}
void sendNotification(NSString* msg) {
NSUserNotification *note = [NSUserNotification new];
note.title = @"cd to ...";
note.informativeText = msg;
[[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:note];
}
int main(int argc, char *argv[]) {
id pool = [[NSAutoreleasePool alloc] init];
NSString* path;
@try {
path = getPathToFrontFinderWindow();
} @catch (id ex) {
path = nil;
}
NSLog(@"FINDER PATH = %@", path);
if (path == nil) {
sendNotification(@"Finder window not found, or is not a valid path!");
} else {
// Try iterm first
CD2ITerm* iterm = [[[CD2ITerm alloc] init] autorelease];
if (![iterm openTermWindowForPath:path]) {
NSLog(@"Failed to open iTerm window");
// iTerm failed to open, try system Terminal
CD2Terminal* term = [[[CD2Terminal alloc] init] autorelease];
if (![term openTermWindowForPath:path]) {
NSLog(@"Failed to open Terminal window");
// system Terminal also failed, send error message to Notification Center
sendNotification(@"Failed to open iTerm/Terminal window!");
}
}
}
[pool release];
return 0;
}