Commits

David Keegan committed f528efd Merge

updating terminal code to work with spaces in file paths and other characters like ', ", &, etc

Comments (0)

Files changed (1)

Source/RepoController_Actions.m

 - (IBAction) openInTerminal: (id) sender
 {
     NSURL* fileURL = [NSURL fileURLWithPath: self.repository.absolutePath];
-
+    
     // Terminal
     // Code from John Daniel - ShellHere
     TerminalApplication * terminal = [SBApplication applicationWithBundleIdentifier: @"com.apple.Terminal"];
+	BOOL terminalWasRunning = [terminal isRunning];
+	
+    // Get the Terminal windows.
+    SBElementArray * terminalWindows = [terminal windows];
     
+    TerminalTab * currentTab = nil;
+    
+    // If there is only a single window with a single tab, Terminal may 
+    // have been just launched. If so, I want to use the new window.
+	// (This prevents two windows from being created.)
+    if(!terminalWasRunning) {
+        for(TerminalWindow * terminalWindow in terminalWindows) {
+		    SBElementArray * windowTabs = [terminalWindow tabs];
+            for(TerminalTab * tab in windowTabs) {
+                currentTab = tab;
+            }
+        }
+    }
+	
     // Create a "cd" command.
-    NSString * command = [NSString stringWithFormat: @"cd %@; clear", [[fileURL path] stringByStandardizingPath]];
-    [terminal doScript: command in: nil];
-    
-    // Wait for "a while" for the script to run and get a new window.
-    // I wish there was a better way to do this.
-    [NSThread sleepForTimeInterval: 0.1];
+    // escape code from JayTuley's - cdto
+    NSString *escapedPath = [[[fileURL path] stringByStandardizingPath] 
+                             stringByReplacingOccurrencesOfString:@"'" withString:@"\\'"];
+    NSString * command = [NSString stringWithFormat: @"cd $'%@';clear;", escapedPath];
+    [terminal doScript: command in: currentTab];
     
     // Activate the Terminal. Hopefully, the new window is already open and
     // is will be brought to the front.