Use specified program to open mail attachment

Discussion in 'macOS' started by geoff123, Aug 6, 2009.

  1. geoff123 macrumors newbie

    Aug 6, 2009
    I have a mail attachement with the extension .gem, and I would like to use that attachement as the argument to a java .jar program.

    I've figured out how to start the java program using a shell script like this:

    java -jar /Users/geoff/Programs/program.jar $1

    If I name this script and change the permissions to allow execution, I can successfully run the program by typing this in a terminal window:

    ./ filename.gem

    However, when I try to open the attachement filename.gem from mail, it won't let me use to open the attachment. The OS seems to make a distinction between a file and an application that is independent of the execute permission on the file.

    After doing some reading about this, I tried Automator. One of the Automator actions is to execute a shell script, so I added that action to my Automator project and typed in my command: java -jar /Users/geoff/Programs/program.jar $1
    I also changed to input from options from "stdin" to "argument".

    I then saved the Automator project, and was able to associate that project with the .gem file attachement from mail. The java program started, but behaved as if the $1 argument was not included.

    If this is making sense to anybody, do you have any suggestions? (Perhaps I just need to leave out the "$1" in my Automator command, trusting Automator to insert the argument in the proper place. If that theory is correct, my program is interpreting the text "$1" as an argument, which would be consistent with the behavior that I'm seeing.)

    I've seen other suggestions to use AppleScript, but it seems to me that there ought to be some simple way to do this.

    (I just got a Mac, by the way. I was able to do this in Windows using MIME type definitions and a .bat file. The difference is that Windows allowed me to consider program.bat as an executable, but Mac OS doesn't consider to be an executable.)

    Thanks a lot.

  2. geoff123 thread starter macrumors newbie

    Aug 6, 2009
    More information

    Responding to my own post...

    I got this working in Automator. I added an additional step before the execution of the shell command which copies the file to a temporary location, and then passes the resulting filename to the shell script. It was necessary to leave the $1 in the shell command. (In bash, $1 resolves to the value of the first argument.)

    Now I need to figure out how to associate this Automator workflow with all files ending in .gem. Right now when I click on a .gem file in Mail, it asks which program I want to use. When I select my Automater workflow, everything works fine, but ideally it would open the program automatically without asking.

    In Windows, this association would be handled by the Registry. With Mac there is no registry (thank goodness!), so it is unclear to me what mechanism would be used to establish the association. I'm not aware of any file association mechanism in other versions of unix, so maybe it is not possible. But I'll report back if I discover anything.


Share This Page