Skip to content

Commit b60f1cb

Browse files
committed
implement specific Eclipse application handling
set working path and current file by parsing Eclipse's main window title. 1) extract workspace-path, package-name and package-relative-filepath 2) find package-path by using 'find' to search for .project file with package-name in it. 3) combine package-path and package-relative-filepath for full glory
1 parent 50aa258 commit b60f1cb

File tree

3 files changed

+1892
-5
lines changed

3 files changed

+1892
-5
lines changed

DTAppController.m

+96-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "Finder.h"
1010
#import "PathFinder.h"
1111
#import "RTFWindowController.h"
12+
#import "SystemEvents.h"
1213

1314
NSString* const DTResultsToKeepKey = @"DTResultsToKeep";
1415
NSString* const DTHotkeyAlsoDeactivatesKey = @"DTHotkeyAlsoDeactivates";
@@ -486,11 +487,75 @@ - (void)hotkeyPressed {
486487
@catch (NSException* e) {
487488
// Fall through to the default attempts to set WD from selection
488489
}
489-
490+
491+
}
492+
493+
// Also use ScriptingBridge special case for Eclipse IDE
494+
else if([frontmostAppBundleID isEqualToString:@"org.eclipse.platform.ide"]) {
495+
496+
SystemEventsApplication * SBSysEvents = (SystemEventsApplication *)[SBApplication applicationWithBundleIdentifier:@"com.apple.systemevents"];
497+
SystemEventsProcess * process = [[SBSysEvents applicationProcesses] objectWithName:@"Eclipse"];
498+
499+
NSString * windowTitle = @"";
500+
for (SystemEventsWindow * win in [process windows]) {
501+
NSNumber * isMain = [(SBObject *)[(SystemEventsAttribute *)[[win attributes] objectWithName:@"AXMain"] value] get];
502+
if (![isMain boolValue]) {
503+
continue;
504+
}
505+
windowTitle = [(SBObject *)[(SystemEventsAttribute *)[[win attributes] objectWithName:@"AXTitle"] value] get];
506+
NSArray * windowPos = [(SBObject *)[(SystemEventsAttribute *)[[win attributes] objectWithName:@"AXPosition"] value] get];
507+
NSArray * windowSize = [(SBObject *)[(SystemEventsAttribute *)[[win attributes] objectWithName:@"AXSize"] value] get];
508+
NSRect windowBounds = NSMakeRect(
509+
[[windowPos objectAtIndex:0] floatValue],
510+
[[windowPos objectAtIndex:1] floatValue],
511+
[[windowSize objectAtIndex:0] floatValue],
512+
[[windowSize objectAtIndex:1] floatValue]
513+
);
514+
//NSLog(@"Eclipse window bounds: %@", NSStringFromRect(windowBounds));
515+
CGFloat screenHeight = [[[NSScreen screens] firstObject] frame].size.height;
516+
windowBounds.origin.y = screenHeight - windowBounds.origin.y - windowBounds.size.height;
517+
//NSLog(@"Eclipse window bounds: %@", NSStringFromRect(windowBounds));
518+
frontWindowBounds = windowBounds;
519+
}
520+
if ([windowTitle length] == 0) {
521+
NSLog(@"Could not find Eclipse window title");
522+
goto done;
523+
}
524+
//NSLog(@"Ecliplse Title: %@", windowTitle);
525+
526+
NSArray * parts = [windowTitle componentsSeparatedByString:@" - "];
527+
NSString * workspacePath = [parts lastObject];
528+
NSString * filepathWithPackage = [parts objectAtIndex:([parts count] - 3)];
529+
NSRange firstSlash = [filepathWithPackage rangeOfString:@"/"];
530+
NSString * package = [filepathWithPackage substringToIndex:firstSlash.location];
531+
NSString * filepathWithinPackage = [filepathWithPackage substringFromIndex:firstSlash.location+1];
532+
533+
534+
//NSLog(@"WORKSPACE: %@", workspacePath);
535+
//NSLog(@"PACKAGE: %@", package);
536+
//NSLog(@"FILEPATH_WITHIN_PACKAGE: %@", filepathWithinPackage);
537+
538+
NSString *findPackagePathCmd = [NSString stringWithFormat:@"find \"%@\" -type f -name .project | xargs -n10 grep \"<name>%@</name>\" | awk 'BEGIN{FS=\"/.project:\"}{print $1}'", workspacePath, package];
539+
NSString *packagePath = [[self outputStringFromCommand:@"/bin/sh" withArguments:@[@"-c",findPackagePathCmd]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
540+
if ([packagePath length] == 0) {
541+
NSLog(@"Empty Package path");
542+
goto done;
543+
}
544+
//NSLog(@"PACKAGE_PATH: %@", packagePath);
545+
546+
547+
NSString *findFullFilepathCmd = [NSString stringWithFormat:@"find \"%@\" -type f -path '*%@'", packagePath, filepathWithinPackage];
548+
NSString *fullFilepath = [[self outputStringFromCommand:@"/bin/sh" withArguments:@[@"-c",findFullFilepathCmd]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];;
549+
//NSLog(@"FULL_FILEPATH: %@", fullFilepath);
550+
//NSLog(@"FULL_FILEPATH (URL): %@", [[NSURL fileURLWithPath:fullFilepath] absoluteString]);
551+
552+
workingDirectory = packagePath;
553+
selectionURLStrings = @[[[NSURL fileURLWithPath:fullFilepath] absoluteString]];
554+
goto done;
490555
}
491556

492557
// Otherwise, try to talk to the frontmost app with the Accessibility APIs
493-
else if([self isAXTrustedPromptIfNot:NO]) {
558+
else if([self isAXTrustedPromptIfNot:NO]) {
494559
// Use Accessibility API
495560
AXError axErr = kAXErrorSuccess;
496561

@@ -631,4 +696,33 @@ - (void)changeFont:(id) __unused sender{
631696
[currentPrefsValues setValue:fontSize forKey:DTFontSizeKey];
632697
}
633698

699+
#pragma mark util
700+
701+
-(NSString *)outputStringFromCommand:(NSString *)command
702+
withArguments:(NSArray *)arguments {
703+
NSTask *task;
704+
task = [[NSTask alloc] init];
705+
[task setLaunchPath: command];
706+
707+
NSLog(@"run task: %@",task);
708+
[task setArguments: arguments];
709+
710+
NSPipe *pipe;
711+
pipe = [NSPipe pipe];
712+
[task setStandardOutput: pipe];
713+
714+
NSFileHandle *file;
715+
file = [pipe fileHandleForReading];
716+
717+
[task launch];
718+
719+
NSData *data;
720+
data = [file readDataToEndOfFile];
721+
722+
NSString *output;
723+
output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
724+
NSLog(@"output: %@", output);
725+
return output;
726+
}
727+
634728
@end

DTerm.xcodeproj/project.pbxproj

+5-3
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@
201201
3B3E1FB1190C10C500B31D3E /* SRValidator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SRValidator.m; sourceTree = "<group>"; };
202202
3B6316B5190D51E100D62E4E /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
203203
75B0EC781A022E5900C023D1 /* iTerm2Nightly.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = iTerm2Nightly.h; sourceTree = "<group>"; };
204+
75BDB76D1A07EAA9006BB694 /* SystemEvents.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SystemEvents.h; sourceTree = "<group>"; };
204205
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
205206
8D1107320486CEB800E47090 /* DTerm.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DTerm.app; sourceTree = BUILT_PRODUCTS_DIR; };
206207
/* End PBXFileReference section */
@@ -285,6 +286,7 @@
285286
252FA10D1141A4D00043EA6C /* iTerm.h */,
286287
25FA394212E5E47100DE744A /* iTerm2.h */,
287288
75B0EC781A022E5900C023D1 /* iTerm2Nightly.h */,
289+
75BDB76D1A07EAA9006BB694 /* SystemEvents.h */,
288290
);
289291
path = ScriptingBridge;
290292
sourceTree = SOURCE_ROOT;
@@ -599,7 +601,7 @@
599601
);
600602
runOnlyForDeploymentPostprocessing = 0;
601603
shellPath = /bin/sh;
602-
shellScript = "PATH=\"$HOME/bin:/usr/local/bin:$PATH\"\ncd \"$PROJECT_DIR\"\nSHA=$(git rev-parse --short HEAD)\nCOUNT=$(git rev-list --count HEAD)\necho \"*** Building Git Revision: $COUNT @ $SHA\"\nmkdir -p \"$TARGET_BUILD_DIR/include\"\necho \"#define REVISION 1.7.$COUNT\" > $TARGET_BUILD_DIR/include/Revision.h\n\ngit diff --quiet\nif [[ $? -eq 0 ]]; then\n echo \"#define REVISION_PUBLIC 1.7.$COUNT-$SHA\" >> $TARGET_BUILD_DIR/include/Revision.h\nelse\n echo \"#define REVISION_PUBLIC 1.7.$COUNT-$SHA+\" >> $TARGET_BUILD_DIR/include/Revision.h\nfi\n";
604+
shellScript = "PATH=\"$HOME/bin:/usr/local/bin:$PATH\"\ncd \"$PROJECT_DIR\"\nSHA=$(git rev-parse --short HEAD)\nCOUNT=$(git rev-list --count HEAD)\necho \"*** Building Git Revision: $COUNT @ $SHA\"\nmkdir -p \"$BUILT_PRODUCTS_DIR/include\"\necho \"#define REVISION 1.7.$COUNT\" > $BUILT_PRODUCTS_DIR/include/Revision.h\n\ngit diff --quiet\nif [[ $? -eq 0 ]]; then\n echo \"#define REVISION_PUBLIC 1.7.$COUNT-$SHA\" >> $BUILT_PRODUCTS_DIR/include/Revision.h\nelse\n echo \"#define REVISION_PUBLIC 1.7.$COUNT-$SHA+\" >> $BUILT_PRODUCTS_DIR/include/Revision.h\nfi\n";
603605
};
604606
/* End PBXShellScriptBuildPhase section */
605607

@@ -830,7 +832,7 @@
830832
GCC_WARN_UNUSED_PARAMETER = YES;
831833
INFOPLIST_FILE = Info.plist;
832834
INFOPLIST_OTHER_PREPROCESSOR_FLAGS = "-traditional";
833-
INFOPLIST_PREFIX_HEADER = $TARGET_BUILD_DIR/include/Revision.h;
835+
INFOPLIST_PREFIX_HEADER = $BUILT_PRODUCTS_DIR/include/Revision.h;
834836
INFOPLIST_PREPROCESS = YES;
835837
INSTALL_PATH = "$(HOME)/Applications";
836838
PRODUCT_NAME = DTerm;
@@ -883,7 +885,7 @@
883885
GCC_WARN_UNUSED_PARAMETER = YES;
884886
INFOPLIST_FILE = Info.plist;
885887
INFOPLIST_OTHER_PREPROCESSOR_FLAGS = "-traditional";
886-
INFOPLIST_PREFIX_HEADER = $TARGET_BUILD_DIR/include/Revision.h;
888+
INFOPLIST_PREFIX_HEADER = $BUILT_PRODUCTS_DIR/include/Revision.h;
887889
INFOPLIST_PREPROCESS = YES;
888890
INSTALL_PATH = "$(HOME)/Applications";
889891
PRODUCT_NAME = DTerm;

0 commit comments

Comments
 (0)