Associate a shell script (starting a Java JAR) with a file extension in a Mac application bundle

荒凉一梦 提交于 2021-02-08 11:48:04

问题


To run a Java8-JAR archive on Macs with a higher Java version I pack the jar into an APP bundle (stuff.app) together with a bash-script,

let's call it stuff.sh (edit: removed "-cp" argument, inserted echoing command line arguments):

#!/bin/bash
VM_ARGS_A="--add-modules=java.xml.bind"
# Set the working directory
DIR=$(cd "$(dirname "$0")"; pwd)
APP_JAR="some-java-stuff.jar"
APP_NAME="Some Java Stuff"
APP_ICNS="stuff.icns"

#test: set command line args
STUFF_TITLE="Launching $APP_NAME"
STUFF_ARGS="$1"
ARGS_MSG="command line args: $STUFF_ARGS"
osascript \
    -e "set question to display dialog \"$ARGS_MSG\" with title \"$STUFF_TITLE\" buttons {\"Cancel\"} default button 1"
#end test

exec $_java $VM_ARGS_A -Dapple.laf.useScreenMenuBar=true -Dcom.apple.macos.use-file-dialog-packages=true -Xdock:name="$APP_NAME" -Xdock:icon="$DIR/../Resources/$APP_ICNS" -jar "$DIR/$APP_JAR"

Snippet from my Info.plist:

    <key>CFBundleDocumentTypes</key>
        <array>
            <dict>
                <key>CFBundleTypeExtensions</key>
                <array>
                    <string>skktx</string>
                </array>
                <key>CFBundleTypeIconFile</key>
                <string>stuff.icns</string>
                <key>CFBundleTypeName</key>
                <string>Java Stuff calculation</string>
                <key>CFBundleTypeRole</key>
                <string>Viewer</string>
                <key>LSHandlerRank</key>
                <string>Owner</string>
            </dict>
        </array>
    <key>CFBundleExecutable</key>
    <string>stuff.sh</string>

It's running fine, basically.

What doesn't work: opening files with that app. I'd like it to do is make the jar to open files. The Java application itself can open a file (custom XML file type) that is named as command line argument.

When I "right-click" (ctrl-click) on a file of that type and choose open with (my app), the file name is not passed to the command line. The invoked dialog only displays "command line args: ", when I tell Finder to open a file with stuff.app.

I've read in that question that it can be done with an AppleScript that has a "on open theFiles" section. Or with Automator.

So, I tried to write an AppleScript snippet that collects the passed objects and passes them to the bash script

property theApplicationPath : the path to me as text
property theShellScriptPath : theApplicationPath & "Contents:MacOS:stuff.sh"

set arguments to ""
on open theFiles
    repeat with anItem in theFiles
        set arguments to arguments & space & (quoted form of POSIX path of anItem)
    end repeat
end open

do shell script theShellScriptPath & arguments

I saved it in the ScriptEditor as stuff.scpt and applied the change to my Info.plist.

As a result, I get an error message when I try to open the bundle, roughly translated: "You can't open the application 'stuff.app', because this Mac-Type does not support it". The App-Icon is displayed a strike-through.

On apple.stackexchange I've read that on LaunchAgent.plist you can insert a <ProgramArguments>-key into the plist. Is it also possible in the Info.plist of the application bundle? And can one pass the file path that way too?

Thank you!

来源:https://stackoverflow.com/questions/60965064/associate-a-shell-script-starting-a-java-jar-with-a-file-extension-in-a-mac-ap

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!