Sam
Sam

Reputation: 2792

How can I associate a file type in mac within Java code?

How can I associate a file type like .vlan in mac within java code?

Runtime.getRuntime().exec("defaults write com.apple.LaunchServices LSHandlers -array-add 
\"<dict><key>LSHandlerContentTag</key>
<string>.vlan</string><key>LSHandlerContentTagClass</key>
<string>public.filename-extension</string><key>LSHandlerRoleAll</key>
<string>org.category.program</string></dict>\"");

Upvotes: 0

Views: 215

Answers (2)

Bertie
Bertie

Reputation: 17697

Try this :

Runtime.getRuntime().exec(
    new String[] {
        "defaults", 
        "write", 
        "com.apple.LaunchServices", 
        "LSHandlers", 
        "-array-add",   
        "<dict><key>LSHandlerContentTag</key><string>.vlan</string><key>LSHandlerContentTagClass</key><string>public.filename-extension</string><key>LSHandlerRoleAll</key><string>org.category.program</string></dict>"
    }
);

And just to add, Runtime's exec has quite some pitfalls one need to be aware of.

Upvotes: 1

Andrew Thompson
Andrew Thompson

Reputation: 168835

If the app. has a GUI, deploy it using Java Web Start and declare the file extension/type in the JNLP (launch) file. Here is a demo. of the JNLP API file services which should be able to associate the text/sleepytime content type of .zzz file with the (small) app.

Adding an association for a file type is supported to work on Windows, OS X & *nix (for all permissions, as well as sand-boxed apps. (the latter prompted)).

Upvotes: 1

Related Questions