Skip to content

Commit

Permalink
implement specific Eclipse application handling
Browse files Browse the repository at this point in the history
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
  • Loading branch information
muhqu committed Nov 4, 2014
1 parent 50aa258 commit b60f1cb
Show file tree
Hide file tree
Showing 3 changed files with 1,892 additions and 5 deletions.
98 changes: 96 additions & 2 deletions DTAppController.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "Finder.h"
#import "PathFinder.h"
#import "RTFWindowController.h"
#import "SystemEvents.h"

NSString* const DTResultsToKeepKey = @"DTResultsToKeep";
NSString* const DTHotkeyAlsoDeactivatesKey = @"DTHotkeyAlsoDeactivates";
Expand Down Expand Up @@ -486,11 +487,75 @@ - (void)hotkeyPressed {
@catch (NSException* e) {
// Fall through to the default attempts to set WD from selection
}


}

// Also use ScriptingBridge special case for Eclipse IDE
else if([frontmostAppBundleID isEqualToString:@"org.eclipse.platform.ide"]) {

SystemEventsApplication * SBSysEvents = (SystemEventsApplication *)[SBApplication applicationWithBundleIdentifier:@"com.apple.systemevents"];
SystemEventsProcess * process = [[SBSysEvents applicationProcesses] objectWithName:@"Eclipse"];

NSString * windowTitle = @"";
for (SystemEventsWindow * win in [process windows]) {
NSNumber * isMain = [(SBObject *)[(SystemEventsAttribute *)[[win attributes] objectWithName:@"AXMain"] value] get];
if (![isMain boolValue]) {
continue;
}
windowTitle = [(SBObject *)[(SystemEventsAttribute *)[[win attributes] objectWithName:@"AXTitle"] value] get];
NSArray * windowPos = [(SBObject *)[(SystemEventsAttribute *)[[win attributes] objectWithName:@"AXPosition"] value] get];
NSArray * windowSize = [(SBObject *)[(SystemEventsAttribute *)[[win attributes] objectWithName:@"AXSize"] value] get];
NSRect windowBounds = NSMakeRect(
[[windowPos objectAtIndex:0] floatValue],
[[windowPos objectAtIndex:1] floatValue],
[[windowSize objectAtIndex:0] floatValue],
[[windowSize objectAtIndex:1] floatValue]
);
//NSLog(@"Eclipse window bounds: %@", NSStringFromRect(windowBounds));
CGFloat screenHeight = [[[NSScreen screens] firstObject] frame].size.height;
windowBounds.origin.y = screenHeight - windowBounds.origin.y - windowBounds.size.height;
//NSLog(@"Eclipse window bounds: %@", NSStringFromRect(windowBounds));
frontWindowBounds = windowBounds;
}
if ([windowTitle length] == 0) {
NSLog(@"Could not find Eclipse window title");
goto done;
}
//NSLog(@"Ecliplse Title: %@", windowTitle);

NSArray * parts = [windowTitle componentsSeparatedByString:@" - "];
NSString * workspacePath = [parts lastObject];
NSString * filepathWithPackage = [parts objectAtIndex:([parts count] - 3)];
NSRange firstSlash = [filepathWithPackage rangeOfString:@"/"];
NSString * package = [filepathWithPackage substringToIndex:firstSlash.location];
NSString * filepathWithinPackage = [filepathWithPackage substringFromIndex:firstSlash.location+1];


//NSLog(@"WORKSPACE: %@", workspacePath);
//NSLog(@"PACKAGE: %@", package);
//NSLog(@"FILEPATH_WITHIN_PACKAGE: %@", filepathWithinPackage);

NSString *findPackagePathCmd = [NSString stringWithFormat:@"find \"%@\" -type f -name .project | xargs -n10 grep \"<name>%@</name>\" | awk 'BEGIN{FS=\"/.project:\"}{print $1}'", workspacePath, package];
NSString *packagePath = [[self outputStringFromCommand:@"/bin/sh" withArguments:@[@"-c",findPackagePathCmd]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
if ([packagePath length] == 0) {
NSLog(@"Empty Package path");
goto done;
}
//NSLog(@"PACKAGE_PATH: %@", packagePath);


NSString *findFullFilepathCmd = [NSString stringWithFormat:@"find \"%@\" -type f -path '*%@'", packagePath, filepathWithinPackage];
NSString *fullFilepath = [[self outputStringFromCommand:@"/bin/sh" withArguments:@[@"-c",findFullFilepathCmd]] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];;
//NSLog(@"FULL_FILEPATH: %@", fullFilepath);
//NSLog(@"FULL_FILEPATH (URL): %@", [[NSURL fileURLWithPath:fullFilepath] absoluteString]);

workingDirectory = packagePath;
selectionURLStrings = @[[[NSURL fileURLWithPath:fullFilepath] absoluteString]];
goto done;
}

// Otherwise, try to talk to the frontmost app with the Accessibility APIs
else if([self isAXTrustedPromptIfNot:NO]) {
else if([self isAXTrustedPromptIfNot:NO]) {
// Use Accessibility API
AXError axErr = kAXErrorSuccess;

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

#pragma mark util

-(NSString *)outputStringFromCommand:(NSString *)command
withArguments:(NSArray *)arguments {
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: command];

NSLog(@"run task: %@",task);
[task setArguments: arguments];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];

NSString *output;
output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"output: %@", output);
return output;
}

@end
8 changes: 5 additions & 3 deletions DTerm.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@
3B3E1FB1190C10C500B31D3E /* SRValidator.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SRValidator.m; sourceTree = "<group>"; };
3B6316B5190D51E100D62E4E /* XCTest.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = XCTest.framework; path = Library/Frameworks/XCTest.framework; sourceTree = DEVELOPER_DIR; };
75B0EC781A022E5900C023D1 /* iTerm2Nightly.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = iTerm2Nightly.h; sourceTree = "<group>"; };
75BDB76D1A07EAA9006BB694 /* SystemEvents.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = SystemEvents.h; sourceTree = "<group>"; };
8D1107310486CEB800E47090 /* Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
8D1107320486CEB800E47090 /* DTerm.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = DTerm.app; sourceTree = BUILT_PRODUCTS_DIR; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -285,6 +286,7 @@
252FA10D1141A4D00043EA6C /* iTerm.h */,
25FA394212E5E47100DE744A /* iTerm2.h */,
75B0EC781A022E5900C023D1 /* iTerm2Nightly.h */,
75BDB76D1A07EAA9006BB694 /* SystemEvents.h */,
);
path = ScriptingBridge;
sourceTree = SOURCE_ROOT;
Expand Down Expand Up @@ -599,7 +601,7 @@
);
runOnlyForDeploymentPostprocessing = 0;
shellPath = /bin/sh;
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";
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";
};
/* End PBXShellScriptBuildPhase section */

Expand Down Expand Up @@ -830,7 +832,7 @@
GCC_WARN_UNUSED_PARAMETER = YES;
INFOPLIST_FILE = Info.plist;
INFOPLIST_OTHER_PREPROCESSOR_FLAGS = "-traditional";
INFOPLIST_PREFIX_HEADER = $TARGET_BUILD_DIR/include/Revision.h;
INFOPLIST_PREFIX_HEADER = $BUILT_PRODUCTS_DIR/include/Revision.h;
INFOPLIST_PREPROCESS = YES;
INSTALL_PATH = "$(HOME)/Applications";
PRODUCT_NAME = DTerm;
Expand Down Expand Up @@ -883,7 +885,7 @@
GCC_WARN_UNUSED_PARAMETER = YES;
INFOPLIST_FILE = Info.plist;
INFOPLIST_OTHER_PREPROCESSOR_FLAGS = "-traditional";
INFOPLIST_PREFIX_HEADER = $TARGET_BUILD_DIR/include/Revision.h;
INFOPLIST_PREFIX_HEADER = $BUILT_PRODUCTS_DIR/include/Revision.h;
INFOPLIST_PREPROCESS = YES;
INSTALL_PATH = "$(HOME)/Applications";
PRODUCT_NAME = DTerm;
Expand Down
Loading

0 comments on commit b60f1cb

Please # to comment.