Saturday, 15 September 2012

windows - How to create .lnk File with Java? -



windows - How to create .lnk File with Java? -

i have found many parsing solutions processing shortcuts windows (.lnk), need create them java-tool.

so questions are:

how can created? (or improve utilize files.createsymboliclink?) - problem have filesize 0 , not treated "normal" files (so when want delete empty folders, symbolic shortcuts (inside) deleted because not recognized "normal" file))

we found reliable way generate temporary .js file , spawn wscript subprocess. feels kludgy, avoids java weaknesses , works older jres (this of import files.createsymboliclink might not available of our utilize cases).

the result looked vaguely following. rewrite utilize path instead of file , other nio.2 features, etc. incoming variables plain string instances, described @ bottom; may empty never null.

it's of import note code creating shortcuts within windows "special folder", not arbitrary locations. can adapt though.

file scriptfile = file.createtempfile ("whatever", ".js"); seek (printwriter script = new printwriter(scriptfile)) { script.printf("try {\n"); script.printf("wshshell = wscript.createobject(\"wscript.shell\")\n"); script.printf("specdir = wshshell.specialfolders(\"%s\")\n", folder); script.printf("shortcut = wshshell.createshortcut(specdir + \"\\\\%s.lnk\")\n", shortcutname); script.printf("shortcut.targetpath = \"%s\"\n", target); script.printf("shortcut.arguments = \"%s\"\n", arguments); script.printf("shortcut.windowstyle = 1\n"); script.printf("shortcut.hotkey = \"\"\n"); if (icon.length() > 0) script.printf("shortcut.iconlocation = \"%s\"\n", icon); script.printf("shortcut.description = \"%s\"\n", description); script.printf("shortcut.workingdirectory = \"%s\"\n", workingdir); script.printf("shortcut.save()\n"); script.printf("} grab (err) {\n"); // commented default script.printf("/*wscript.echo(\"name:\")\nwscript.echo(err.name)\n"); script.printf("wscript.echo(\"message:\")\nwscript.echo(err.message)\n"); script.printf("wscript.echo(\"description:\")\nwscript.echo(err.description)\n"); script.printf("wscript.echo(\"stack:\")\nwscript.echo(err.stack)\n"); script.printf("*/\n"); script.printf("wscript.quit(1)\n"); script.printf("}\n"); script.close(); // run cscript.exe arguments "//nologo" , total // path 'script', using processbuilder , process }

you can test exit value of process, , if it's zero, delete temp file. if went wrong, can leave file behind investigation, including editing script hand uncomment error dumping @ bottom.

the folder special windows name destination folder, e.g., "sendto" or "startmenu", etc. total list on msdn somewhere, main thing remember aren't plain english language names folders.

the shortcutname is, e.g., "my programme shortcut". target think is, , ought total path safest results.

the icon string funky windows thing give icon file name , index number, "myapp.ico, 0". code above treats empty icon string using scheme default.

the description becomes shortcut's properties -> comment field. arguments , workingdir can left blank if don't need set them.

java windows shortcut

No comments:

Post a Comment