Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

As I've heard, on MacOSX one can write a shell script, give it an extension ".command", and once its clicked on in Finder, the script runs with /bin/sh ?
Does anybody know - can I put sheebang line into such script pointing to Perl interpreter - will it be executed using Perl interpreter or Finder is hardcoded to execute all .command files via /bin/sh? And what is the path to perl on MacOSX - /usr/bin/perl ?

Why I'm asking: I'm struggling with a problem - I have a perl script. I wish to give user ability to invoke it somehow on macosx. I've tried writing "foo.command" file and placing it into the directory where that script resides, with content like this:

#!/bin/sh cd `dirname $0` >/dev/null 2>&1 perl myscript.pl
but $0 appears to be ALWAYS UNDEFINED on MacOSX!! ANd all .command files are being executed with current directory set to user's home directory, so I see NO way of determining the directory to change to in order to be able to execute that perl script. That's why I'm searching for a user-friendly way of executing of a perl script from finder by just doubleclicking on it (so it would work on any macosx-running computer).. Do you have any suggestions?

Thanks in advance for your answers!

  • Comment on Can files with extension ".command" on MacOSX be perl scripts? What is a way to let users execute Perl scripts on macosx from Finder?
  • Download Code

Replies are listed 'Best First'.
Re: Can files with extension ".command" on MacOSX be perl scripts? What is a way to let users execute Perl scripts on macosx from Finder?
by Fletch (Bishop) on Feb 14, 2005 at 13:29 UTC

    You probably want DropScript, which will make an .app containing an arbitrary executable script (shell, Perl, Ruby, yadda yadda yadda) that you can drop things on in the Finder or just double click.

    Update: Oooh, Platypus is spiffy; definitely check it out as well. More bells and whistles than DropScript.

      You probably want DropScript

      Or Platypus.

Re: Can files with extension ".command" on MacOSX be perl scripts? What is a way to let users execute Perl scripts on macosx from Finder?
by Taulmarill (Deacon) on Feb 14, 2005 at 13:27 UTC
    you can use perl for that.
    just take your ordinary perl code and insert #!/usr/bin/perl as the first line to let the shell know, that it's a perl script and give the file a chmod to make it executable.
      Thanks for the answer! But do I have to use .command extension, or Finder is smart enough to try to execute any executable file with any name that has sheebang line using system() syscall?
        finder identifies files by it's extension, so you have to name it foo.command.
Re: Can files with extension ".command" on MacOSX be perl scripts? What is a way to let users execute Perl scripts on macosx from Finder?
by halley (Prior) on Feb 14, 2005 at 17:41 UTC
    What I would really like is to be able to use any executable script (Perl, bash, etc.) as a Mac OS "Speakable Item." Speakable Items are simply scripts with a filename you can say out loud. If the user says the filename, the script is executed.

    Some rules allow only certain scripts in certain directories to be monitored with the voice recognition subsystem. As far as I can tell, there are only two kinds of Speakable Items: an AppleScript .app, or a special text file which indicates that the functionality is built into the voice recognizer itself (such as the Knock Knock joke engine).

    I would like a pile of Speakable Items, but the overhead of wrapping each .pl into .app files is quite a chore. I would also like a certain "Speakable Items" folder to be active when the login prompt is on the screen (with all the caveats of not allowing any GUI interaction, and installed only by sudo, etc). I've recommended this to Apple, and I hope Tiger shows some growth in that area, but I don't hold much hope.

    --
    [ e d @ h a l l e y . c c ]

      I think you can use a compiled applescript. And a compiled applescript can include
      do shell script "/some/unix/command with args | here to | there >outpu +t";
      so it might be inconvenient, but at least you can fire your arbitrary script this way.

      -- Randal L. Schwartz, Perl hacker
      Be sure to read my standard disclaimer if this is a reply.

        I'll have to give that a try, merlyn, but if we're missing a mechanism akin to the shell $0 argument, it'll be hard to make a single generic AppleScript that chains over to a number of Perl behaviors.

        --
        [ e d @ h a l l e y . c c ]