forked from jbtule/cdto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.m
93 lines (70 loc) · 2.22 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
//
// 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"];
FinderFinderWindow* frontWindow =[[finder windows] objectAtIndex:0];
FinderItem* target = [frontWindow.properties objectForKey:@"target"] ;
NSURL* url =[NSURL URLWithString:target.URL];
FSRef fsRef;
Boolean isDir =NO;
Boolean wasAliased;
if (CFURLGetFSRef((CFURLRef)url, &fsRef)){
if (FSResolveAliasFile (&fsRef, true /*resolveAliasChains*/,
&isDir, &wasAliased) == noErr && wasAliased){
NSURL* newURL = (NSURL*)CFURLCreateFromFSRef(NULL, &fsRef);
[newURL autorelease];
if(newURL!=nil)
url = newURL;
}
}
NSString* path = [url path];
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;
}