Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have set up a script that launches the terminal with this bash command:

open script.command

script.command is a Perl script, and it gets executed just fine. (This may look a little convoluted, but it's the only way I've found to bundle a script with an interactive command line in a regular application created by Platypus.) The only problem is that the working directory of the script is always my home directory, regardless of where the above command was executed. The same also applies to open -a Terminal.

I've tried doing this:

open script.command --args "`pwd`"

which should technically work (using the argument in a chdir), but the argument isn't passed to the perl script (script.command).

Does Terminal have any command line options I'm unaware of?

share|improve this question
    
Are you trying to open Terminal when it isn’t already open or are you trying to issue a command in one terminal window and have it run in a new terminal window? –  Chris Page May 8 '12 at 8:52
    
The former; the script is executed by a Platypus application, which then opens Terminal (if necessary). This way I can include some resources in the app bundle, plus my client doesn't have to mess about with the command line. –  vbwx May 8 '12 at 22:58

2 Answers 2

up vote 2 down vote accepted

The method you describe doesn't work because the arguments are passed to Terminal (which ignores them) and not the script interpreter of the script.


Instead, you can use AppleScript to make Terminal first cd into the correct directory and then open the interactive script. Replace the open script.command with osascript launchscript.scpt "$PWD" >/dev/null.

In AppleScript Editor, create a script with the following contents:

on run argv
    tell application "Terminal"
        activate
        do script "cd " & quoted form of item 1 of argv & " ; ./script.command"
    end tell
end run

Save as launchscript.scpt and include as resource in Platypus.

This opens a Terminal window that will first cd into the working directory of the main script (the Contents/Resources of your Platypus application) and only then executes script.command.

share|improve this answer
    
I'm redirecting stdout of osascript because it'll tell you the tab ID of the Terminal tab created for the command. Rather irritating if you don't use it. –  Daniel Beck May 6 '12 at 17:36
    
Thanks, that makes sense! But since Platypus allows AppleScript as well, I was able to get rid of the osascript step (after a bit of trial and error) by making the above script the main script and changing line 4 to this: do script "cd \"`dirname " & quoted form of (POSIX path of (path to me)) & "`\"; clear; ./test.command; exit" This seems to open 2 Terminal windows, although, unless Terminal is already launched. –  vbwx May 6 '12 at 18:37
    
@vbwx You didn't mention whether the interactive script is the only thing you're starting, so my answer provided something for the general case of other scripts also being executed. If that's not the case, you can simplify a bit, true. Although, given the call to dirname, it doesn't look like it's simpler ;) –  Daniel Beck May 6 '12 at 18:42
    
True, I should've made it clearer what the purpose of this app/script is... I've added an answer for my own specific case, but for the general case your answer is the right one. –  vbwx May 7 '12 at 13:43

It turns out, I've overlooked a technique that's far easier.

The main script doesn't have to be changed:

open script.command

And the beginning of script.command now looks like this:

#!/usr/bin/perl

use File::Basename;
chdir(dirname($0));

Of course, this only works within the confines of the Perl script, so in more complex cases, the AppleScript solution is the way to go.

share|improve this answer
2  
Or cd "$(dirname "$0")" (bash) or Dir.chdir(Dir.getwd) (Ruby). –  ؘؘؘؘ May 7 '12 at 14:03

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.