0

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>\"");
Sam
  • 2,702
  • 5
  • 31
  • 45
  • Huh? Could you rephrase that in the form of a question? Associate it with what? – Brian Roach Jan 23 '12 at 06:02
  • Well, my application is supposed to be crossed platform so I already went through the ftype and assoc commands to cover the Windows part of things and it works great, however, now I need to cover the same file type for mac. Please keep in mind that my understanding of the mac environment is very limited – Sam Jan 23 '12 at 06:05
  • Duplicate, answered [here](http://stackoverflow.com/a/2976711/493928) – khachik Jan 23 '12 at 06:08
  • But that answer is not in regards to Java. I'm looking for a solution from within Java. – Sam Jan 23 '12 at 06:13
  • @Sam `Runtime.getRuntime().exec("...");`. I think you should search a little before asking. – khachik Jan 23 '12 at 06:16
  • You mean something like what I put in the question area will work in mac? – Sam Jan 23 '12 at 06:23

2 Answers2

1

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

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
1

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.

Bertie
  • 17,277
  • 45
  • 129
  • 182