forked from jbtule/cdto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.m
More file actions
86 lines (69 loc) · 2.22 KB
/
main.m
File metadata and controls
86 lines (69 loc) · 2.22 KB
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
//
// main.m
// cd to ...
//
// Created by James Tuley on 2/16/07.
// Copyright Jay Tuley 2007. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import "Finder.h"
#import "Terminal.h"
NSString* getPathToFrontFinderWindow(){
FinderApplication* finder = [SBApplication applicationWithBundleIdentifier:@"com.apple.Finder"];
FinderItem *target = [(NSArray*)[[finder selection]get] firstObject];
if (target == nil){
target = [[[[finder FinderWindows] firstObject] target] get];
}
NSURL* url =[NSURL URLWithString:target.URL];
NSError* error;
NSData* bookmark = [NSURL bookmarkDataWithContentsOfURL:url error:nil];
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 openPathInTerminal(NSString*path){
TerminalApplication*terminal=[SBApplication applicationWithBundleIdentifier:@"com.apple.Terminal"];
BOOL found=NO;
for(TerminalWindow*w in terminal.windows){
NSLog(@"%@",w.name);
NSString*main=[path lastPathComponent];
if([w.name containsString:main]){
[terminal doScript:@"pwd" in:w.selectedTab];
if([w.selectedTab.contents containsString:path]){
w.frontmost=YES;
found=YES;
break;
}
}
}
if(!found){
[[NSWorkspace sharedWorkspace] openFile:path withApplication:@"Terminal"];
}else{
[terminal activate];
}
}
int main(int argc, char *argv[])
{
@autoreleasepool{
NSString* path;
@try{
path = getPathToFrontFinderWindow();
}@catch(id ex){
path =[@"~/Desktop" stringByExpandingTildeInPath];
}
openPathInTerminal(path);
}
return 0;
}