#352 ✓resolved
Al Chou

Open Directory in Command Line doesn't work on OS X

Reported by Al Chou | October 22nd, 2010 @ 04:22 AM

On Mac OS X, right-click in the project drawer to get the context menu, choose "Open Directory > in File Browser" or "Open Directory > in Command Line". They both open a Finder window. I expected "in Command Line" to invoke Terminal.app or something like that. Or maybe "in File Browser" was supposed to mean in Redcar's file browser? In any case, they shouldn't both do exactly the same thing.

Comments and changes to this ticket

  • delisa

    delisa October 22nd, 2010 @ 04:46 AM

    I pushed a possible fix for this to a branch on github: http://github.com/kattrali/redcar/tree/352-osx-open-terminal

    I don't have a OS X machine to test it, can someone give it a try?

  • Al Chou

    Al Chou October 22nd, 2010 @ 02:12 PM

    Replying to Lighthouse's email didn't work (said I wasn't authorized to post to the ticket -- huh?). Here's what I had said:

    I grabbed your changed commands.rb and put it in place in my 0.7. It does now open Terminal.app, but it apparently tries to execute the string "#{path}" as a command rather than cd'ing or pushd'ing into the directory named by it.

  • delisa

    delisa October 22nd, 2010 @ 02:28 PM

    Ah, okay. I think we're on the right track then. I added the change dir cmd, maybe give it another go?

  • Al Chou

    Al Chou October 22nd, 2010 @ 03:04 PM

    I figured out the command line to do it:

    osascript -e 'tell application "Terminal"' -e "do script "cd \"pwd\""" -e 'end tell'

  • Al Chou

    Al Chou October 22nd, 2010 @ 03:09 PM

    Damn, some of the required backslashes got eaten. Let's try again:

    osascript -e 'tell application "Terminal"' -e "do script \"cd \\\"`pwd`\\\"\"" -e 'end tell'
    

    And I forgot to mention in the previous comment that you can/should substitute the value of path in place of pwd, obviously.

  • delisa

    delisa October 22nd, 2010 @ 04:11 PM

    I think it should be good now (provided I actually escaped everything)

    run_application(
      "osascript",
         "-e", "'tell application \"Terminal\"'"     ,
         "-e", '"do script \"cd \\\"`#{path}`\\\"\""',
         "-e",
       "'end tell'"
    )
    
  • Al Chou

    Al Chou October 22nd, 2010 @ 04:14 PM

    I just tried commit 1d73d4f19d4b22354e28 and it doesn't work, but I think it's because of the nasty nested quoting, which you sidestepped by wrapping the scriptlets in single quotes, but which makes #{path} not get interpolated. Probably the easiest way forward is to compose that scriptlet as substrings, e.g.,

    '"do script "cd \"' + path + '\"""'

  • delisa

    delisa October 22nd, 2010 @ 04:26 PM

    Yeah, the escaping is a bit nasty, I was having a moment of difficulty figuring out which quotes were necessary. :)

    Round Four!

    run_application(
      "osascript",
        "-e", "'tell application \"Terminal\"'",
        "-e", '"do script \"cd \"' + path + '\"""',
        "-e",
      "'end tell'"
    )
    
  • Al Chou

    Al Chou October 22nd, 2010 @ 07:46 PM

    Not sure whether I have Spoon, but if not, the other branch in run_application is being taken and is doing even more quote wrapping and doesn't end up with the right string. I tried the code in irb and get the following output from the puts statement:

    Running: osascript "-e" "'tell application "Terminal"'" "-e" ""do script "cd "/Users/achou/.rvm/gems/ree-1.8.7-2010.02/gems/redcar-0.7/plugins/project/lib/project"""" "-e" "'end tell'"

    Pasting the command from there to the command line, the shell finds the double quotes to be unbalanced.

    I found another way to do it (following the example of the script at the end of http://stackoverflow.com/questions/989349/running-a-command-in-a-ne... ), which would work if only run_application didn't wrap a pair of double quotes around each option:

    run_application( 'osascript', "<<END\ntell application "Terminal"\n do script "cd \"" + path + "\""\nend tell\nEND" )

  • delisa

    delisa October 22nd, 2010 @ 09:25 PM

    Hmmm, that may work, if app is passed with its options directly.
    http://github.com/kattrali/redcar/commit/02d06cabb93e93eedabf613d15... :

    run_application(
      "osascript <<END\ntell application \"Terminal\"\n do script \"cd \"" + path + "\"\"\nend tell\nEND"
    )
    
  • Al Chou

    Al Chou October 23rd, 2010 @ 06:02 AM

    I wish it did work, but it doesn't....

    Maybe it would be easier to make Spoon a strict requirement for Redcar? Assuming Spoon handles this command correctly, of course.

  • delisa

    delisa October 23rd, 2010 @ 12:27 PM

    • Title changed from “Open Directory context menu items in project drawer both open a Finder window on OS X” to “Open Directory in Command Line doesn't work on OS X”

    What does it do? Details help. :) Does the command in a Terminal?

  • Al Chou

    Al Chou October 23rd, 2010 @ 02:51 PM

    • Tag changed from context menu, open directory, project drawer to context menu, open directory, os x, project drawer

    Oh, yeah, sorry! :) Terminal.app doesn't even launch.

  • Al Chou

    Al Chou October 23rd, 2010 @ 03:26 PM

    I just realized that I forgot to format my comment so that it showed all the backslashes correctly. Here's what the line should say:

    run_application( 'osascript ' + "<<END\ntell application \"Terminal\"\n do script \"cd \\\"" + path + "\\\"\"\nend tell\nEND" )
    

    And that actually works, now that I've correctly converted the argument of run_application into one string (didn't figure out the escaping yet again to make it one big string literal, but who cares?)!

  • Al Chou

    Al Chou October 23rd, 2010 @ 03:42 PM

    Being an iTerm fan, I figured out how to do the same thing in that app:

    run_application( 'osascript ' + "<<END\ntell application \"iTerm\"\n tell the first terminal\n  launch session \"Default Session\"\n  tell the last session\n   write text \"cd \\\"" + path + "\\\"\"\n  end tell\n end tell\nend tell\nEND" )
    

    I don't know how you would want to expose this in the UI, if at all, but I wanted to write it down for posterity, if nothing else. :)

  • Al Chou

    Al Chou October 23rd, 2010 @ 04:09 PM

    One more refinement, to bring the terminal app to the foreground so the user doesn't mistakenly think nothing happened:

    run_application( 'osascript ' + "<<END\ntell application \"Terminal\"\n do script \"cd \\\"" + path + "\\\"\"\n activate\nend tell\nEND" )
    

    And for iTerm :) :

    run_application( 'osascript ' + "<<END\ntell application \"iTerm\"\n tell the first terminal\n  launch session \"Default Session\"\n  tell the last session\n   write text \"cd \\\"" + path + "\\\"\"\n  end tell\n end tell\n activate\nend tell\nEND" )
    
  • delisa

    delisa October 23rd, 2010 @ 04:35 PM

    Awesome stuff :) I pushed a patch for it, also adding a preference option for what terminal to use (Preferences > project > preferred_command_line).
    http://github.com/kattrali/redcar/commit/a14918213e2c871005769f027c...

  • Mat Schaffer

    Mat Schaffer October 23rd, 2010 @ 07:43 PM

    Had to step around spoon for the open and osascript command. Filed a bug with spoon to see if maybe there's a possible fix there:

    http://github.com/headius/spoon/issues/issue/1

  • delisa

    delisa October 23rd, 2010 @ 08:06 PM

    Merged it. should be fixed.

  • Daniel Lucraft

    Daniel Lucraft October 23rd, 2010 @ 08:14 PM

    • State changed from “new” to “resolved”

    Heck of a thread. Closing this then.

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

A programmer's text editor for Gnome.

People watching this ticket

Pages