forked from sozercan/OpenInCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.m
executable file
·60 lines (47 loc) · 1.61 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
//
// main.m
// Open in Code
//
// Created by Sertac Ozercan on 7/9/2016.
// Copyright Sertac Ozercan 2016. All rights reserved.
//
#import <Cocoa/Cocoa.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;
}
int main(int argc, char *argv[])
{
id pool = [[NSAutoreleasePool alloc] init];
NSString* path;
@try{
path = getPathToFrontFinderWindow();
}@catch(id ex){
path =[@"~/Desktop" stringByExpandingTildeInPath];
}
[[NSTask launchedTaskWithLaunchPath:@"/usr/bin/open" arguments:@[@"-n", @"-b" ,@"com.microsoft.VSCode", @"--args", path]] waitUntilExit];
[pool release];
return 0;
}