Ubuntu insights, Programming in groovy, java, et als!

Tuesday, April 03, 2012

Creating Windows Shortcuts for your Application Programatically

Here is a 10 liner vbscript that can dump shortcuts for your application on desktop and All Programs menu in windows.

set WshShell = WScript.CreateObject("WScript.Shell" )
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")

pathToDesktop = WshShell.SpecialFolders("Desktop")
set desktopShortcut = WshShell.CreateShortcut(pathToDesktop & "\MyApp.lnk")
desktopShortcut.TargetPath = Wscript.Arguments.Named("target")
desktopShortcut.WindowStyle = 1
desktopShortcut.IconLocation = WinDir & "\system32\SHELL32.dll,43"

pathToStartMenu = WshShell.SpecialFolders("StartMenu")
set startMenuShortcut = WshShell.CreateShortcut(pathToStartMenu & "\Programs\MyApp.lnk")
startMenuShortcut.TargetPath = Wscript.Arguments.Named("target")
startMenuShortcut.WindowStyle = 1
startMenuShortcut.IconLocation = WinDir & "\system32\SHELL32.dll,43"

Execution from Command Line: 

cd to the directory where the above script is saved as mkshortcut.vbs.

Here I create a shortcut to a directory F:\Temp or any other file (even batch files or exe files which launch your application).

mkshortcut /target:"F:/temp"

The above command will create a shortcut called MyApp.lnk on your start menu >> Programs and on Desktop.

Execution from groovy program :

Prefix the above command with "cmd /c" and execute.

"cmd /c $command".execute() where in the command variable stores the mkshortcut line above as string.

Like wise, the same can also be extended to java using the Process Object.


Post a Comment