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?

  • 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

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"
        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.

  • 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:


use File::Basename;

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.

  • 2
    Or cd "$(dirname "$0")" (bash) or Dir.chdir(Dir.getwd) (Ruby).
    – Lri
    May 7 '12 at 14:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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