forked from jbtule/cdto
-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.m
91 lines (72 loc) · 2.74 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
87
88
89
90
91
//
// main.m
// cd to
//
// Created by James Tuley on 2/16/07.
// Copyright Jay Tuley 2007. All rights reserved.
//
#import <Cocoa/Cocoa.h>
#import "CD2PluginProtocolV1.h"
#import "Finder.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;
}
NSArray *loadPlugins() {
NSString *pluginPath = [[NSBundle mainBundle] builtInPlugInsPath];
NSArray *bundlePaths = [NSArray array];
if (pluginPath != nil) {
bundlePaths =[NSBundle pathsForResourcesOfType:@"bundle"
inDirectory:pluginPath];
}
NSMutableArray *pluginObjectArrays = [NSMutableArray array];
NSEnumerator *enumerator = [bundlePaths objectEnumerator];
NSString *path;
while ((path = (NSString*)[enumerator nextObject])) {
NSBundle* bundle =[NSBundle bundleWithPath:path];
Class pClass =[bundle principalClass];
if ([pClass conformsToProtocol:@protocol(CD2PluginProtocolV1)] && [pClass isKindOfClass:[NSObject class]]) {
[pluginObjectArrays addObject:[[[pClass alloc]init]autorelease]];
}
}
return pluginObjectArrays;
}
int main(int argc, char *argv[]) {
id pool = [[NSAutoreleasePool alloc] init];
NSString *path;
@try {
path = getPathToFrontFinderWindow();
}
@catch (id ex) {
path =[@"~/Desktop" stringByExpandingTildeInPath];
}
NSArray *plugins = loadPlugins();
NSEnumerator *enumerator = [plugins objectEnumerator];
id <CD2PluginProtocolV1> plugin;
while ((plugin = (id <CD2PluginProtocolV1>)[enumerator nextObject])) {
[plugin openTermWindowForPath:path];
}
[pool release];
return 0;
}