Skip to content

Commit

Permalink
Better window closing logic, wip on matching space to finder window
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtule committed Oct 10, 2019
1 parent 8242396 commit 7555521
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 12 deletions.
2 changes: 2 additions & 0 deletions cd to .../cd to .../Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
<true/>
<key>NSSupportsSuddenTermination</key>
<true/>
<key>LSUIElement</key>
<true/>
</dict>
</plist>
49 changes: 37 additions & 12 deletions cd to .../cd to .../main.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#import "Finder.h"
#import "Terminal.h"

NSUInteger linesOfHistory(TerminalTab* tab) {
NSString* hist = [[tab history] stringByTrimmingCharactersInSet:[NSCharacterSet newlineCharacterSet]];
return [[hist componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]] count];
}

int main(int argc, const char * argv[]) {
@autoreleasepool {
// Setup code that might create autoreleased objects goes here.
Expand All @@ -20,8 +25,10 @@ int main(int argc, const char * argv[]) {
TerminalApplication* terminal = [SBApplication applicationWithBundleIdentifier:@"com.apple.Terminal"];

FinderItem *target = [(NSArray*)[[finder selection] get] firstObject];
FinderFinderWindow* findWin = [[finder FinderWindows] objectAtLocation:@1];
NSRect findBounds = [findWin bounds];
if (target == nil){
target = [[[[finder FinderWindows] objectAtLocation:@1] target] get];
target = [[findWin target] get];
}

if ([[target kind] isEqualToString:@"Alias"]){
Expand All @@ -34,26 +41,44 @@ int main(int argc, const char * argv[]) {
}

NSURL* url = [NSURL URLWithString:fileUrl];


if (url != nil){
TerminalWindow* win = nil;
if ([[terminal windows] count] == 1){
TerminalWindow* win = [[terminal windows] objectAtLocation:@1];
//get front most and then reference by id
win = [[terminal windows] objectAtLocation:@1];
win = [[terminal windows] objectWithID: [NSNumber numberWithInteger:win.id]];
}
[terminal open:@[url]];
//get front most and then reference by id
TerminalWindow* newWin = [[terminal windows] objectAtLocation:@1];
newWin = [[terminal windows] objectWithID: [NSNumber numberWithInteger:newWin.id]];
TerminalTab* newTab = [[newWin tabs] objectAtLocation:@1];
if(true){ //close first launch window
if([[win tabs] count] == 1){
TerminalTab* tab = [[win tabs] objectAtLocation:@1];
TerminalTab* tab = [[win tabs]objectAtLocation:@1];
if(![tab busy]){
NSUInteger rows = [tab numberOfRows];
NSString* hist = [tab history];
NSInteger histLength = [[hist componentsSeparatedByCharactersInSet:
[NSCharacterSet newlineCharacterSet]] count];
//a signle
if(rows + 1 >= histLength){
//if history has same number of lines as new window
// assume automatically opened new window, and close it
NSUInteger oldTabLines = linesOfHistory(tab);
while([newTab busy]){
[NSThread sleepForTimeInterval:0.1f];
}
NSUInteger newTabLines = linesOfHistory(newTab);
NSLog(@"%lu",(unsigned long)oldTabLines);
NSLog(@"%lu",(unsigned long)newTabLines);
if(oldTabLines == newTabLines){
[win closeSaving:TerminalSaveOptionsNo savingIn:nil];
}
}else{
NSLog(@"busy");
}
}
}
[terminal open:@[url]];
if(true){ // move to space
NSRect oldBounds = [newWin bounds];
NSRect newBounds = NSMakeRect(findBounds.origin.x,findBounds.origin.y, oldBounds.size.height, oldBounds.size.width);

}
[terminal activate];
}
}
Expand Down

0 comments on commit 7555521

Please sign in to comment.