Create Desktop Shortcut

Technical support and scripting issues

Moderators: JRL, Dorian (MJT support)

Post Reply
kpassaur
Automation Wizard
Posts: 696
Joined: Wed Jul 07, 2004 1:55 pm

Create Desktop Shortcut

Post by kpassaur » Mon Jul 09, 2007 9:20 pm

I am trying to create a desktop shortcut and found a nice script on the forum that does just that; however, it changes the "/" to "\" and I need the forward slash to send along the parameters as in this example from the help file

msched Example Script /filename=testfile.txt /path=c:\outpath\the


VBSTART
Sub NewDesktopShortcut(linkfile,targetpath,windowstyle,hotkey,descr)
Set WshShell = CreateObject("Wscript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
set oShellLink = WshShell.CreateShortcut(strDesktop & "\" & linkfile)
oShellLink.TargetPath = targetpath
oShellLink.WindowStyle = windowstyle
oShellLink.Hotkey = hotkey
oShellLink.IconLocation = "C:\consolegrab\ProfileManager\Profile Manager.exe, 0"
oShellLink.Description = descr
oShellLink.WorkingDirectory = strDesktop
oShellLink.Save
End Sub
VBEND

Let>job=Test this variable
let>linkname=%job%.lnk
Let>profile=test

Let>program=C:\consolegrab\ProfileManager\Profile Manager.exe /Profiletouse="%profile%" /Output="Commandline"

VBRun>NewDesktopShortcut,%linkname%,%program%,1,CTRL+SHIFT+R,Profile Manager Shortcut


The line generated from this script in the shortcut looks like this

"C:\consolegrab\ProfileManager\Profile Manager.exe \Profiletouse="test" \Output="Commandline""

Any ideas on how to force it to become a forward slash?

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jul 10, 2007 6:12 am

This is because you are passing the arguments as part of the target so it thinks those should be part of the path. You need to use the arguments property of the shortcut object.

This version adds a new parameter for the command line arguments:

Code: Select all

VBSTART
Sub NewDesktopShortcut(linkfile,targetpath,windowstyle,hotkey,descr,parms)
  Set WshShell = CreateObject("Wscript.Shell")
  strDesktop = WshShell.SpecialFolders("Desktop")
  set oShellLink = WshShell.CreateShortcut(strDesktop & "\" & linkfile)
  oShellLink.TargetPath = targetpath
  oShellLink.Arguments = parms
  oShellLink.WindowStyle = windowstyle
  oShellLink.Hotkey = hotkey
  oShellLink.IconLocation = "C:\consolegrab\ProfileManager\Profile Manager.exe, 0"
  oShellLink.Description = descr
  oShellLink.WorkingDirectory = strDesktop
  oShellLink.Save
End Sub
VBEND

Let>job=Test this variable
let>linkname=%job%.lnk
Let>profile=test

Let>program=C:\consolegrab\ProfileManager\Profile Manager.exe
Let>parms=/Profiletouse="%profile%" /Output="Commandline"

VBRun>NewDesktopShortcut,%linkname%,%program%,1,CTRL+SHIFT+R,Profile Manager Shortcut,parms
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Tue Jul 10, 2007 5:05 pm

Hi Marcus and kpassaur,

Thanks for this, a very useful script. I made a few mods to pull out more of the variables so I could set them in Macro Script and not have to make any changes to the VBScript. Here's what I'm using:

Code: Select all

VBSTART
Sub NewDesktopShortcut(linkfile,targetpath,parms,windowstyle,hotkey,iconlocation,descr)
  Set WshShell = CreateObject("Wscript.Shell")
  strDesktop = WshShell.SpecialFolders("Desktop")
  set oShellLink = WshShell.CreateShortcut(strDesktop & "\" & linkfile)
  oShellLink.TargetPath = targetpath
  oShellLink.Arguments = parms
  oShellLink.WindowStyle = windowstyle
  oShellLink.Hotkey = hotkey
  oShellLink.IconLocation = iconlocation
  oShellLink.Description = descr
  oShellLink.WorkingDirectory = strDesktop
  oShellLink.Save
End Sub
VBEND

Let>shortcut_icon_title=My App
Let>linkfile=%shortcut_icon_title%.lnk

Let>profile=my_profile

//Let>targetpath=C:\consolegrab\ProfileManager\Profile Manager.exe
Let>targetpath=C:\Program Files\Dir\myapp.exe

//Let>parms=/Profiletouse="%profile%" /Output="Commandline"
Let>parms=

//1=Normal, 3=Maximized, 7=Minimized
Let>windowstyle=1

//Let>hotkey=CTRL+SHIFT+R
Let>hotkey=

//Note: the icon image is contained within the app exe
//Let>iconlocation=C:\consolegrab\ProfileManager\Profile Manager.exe, 0
Let>iconlocation=C:\Program Files\Dir\myapp.exe, 0

//Let>descr=Profile Manager Shortcut
Let>descr=My App Icon Description

VBRun>NewDesktopShortcut,linkfile,targetpath,parms,windowstyle,hotkey,iconlocation,descr
I do have two questions though...

1) What is the significance of ", 0" in the following line?
  • Let>iconlocation=C:\Program Files\Dir\myapp.exe, 0
2) For windowstyle, 1 works for "Normal" and 3 works for "Maximized" but 2 does not work for "Minimized". The icon created just has "Normal" for windowstyle when I use 2. How would I get that to work? (Edit: use 7 for "Minimized")

Again, thanks again for this great bit of code.
Last edited by jpuziano on Tue Jul 10, 2007 8:15 pm, edited 1 time in total.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Tue Jul 10, 2007 5:12 pm

1: 0 means use the first icon resource in the given EXE file. Zero based.
2: 2 does mean run minimized. Perhaps the app in question doesn't like being started minimized.

Documentation on the Shortcut object is here:
http://msdn2.microsoft.com/en-us/library/xk6kst2k.aspx
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Tue Jul 10, 2007 8:10 pm

mtettmar wrote:1: 0 means use the first icon resource in the given EXE file. Zero based.
Sounds good, thanks.
mtettmar wrote:2: 2 does mean run minimized. Perhaps the app in question doesn't like being started minimized.

Documentation on the Shortcut object is here:
http://msdn2.microsoft.com/en-us/library/xk6kst2k.aspx
Your link led me to this link that shows to use 7 for "Minimized":
http://msdn2.microsoft.com/en-us/library/w88k7fw2.aspx

I tried 7 and it worked... now when I right-click the shortcut icon that this script creates and click Properties, it shows "Run: Minimized" which is what I was after.

I will correct the code I posted above to show that 7 is for "Minimized".

Thanks again for this very useful script.
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Thu Feb 25, 2010 11:02 am

Hi there,

Great help (all of the above).

If anyone has a minute could you explain how to change the place that the shortcut is placed. Instead of Desktop.

At the moment in my little file moving app I am making the shortcuts (using this VB) and then moving them from the desktop to the desired location.

It works fine but it would be nice to just create the shortcut at the desired location straight away.

:-)
Phil Pendlebury - Linktree

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Feb 25, 2010 11:14 am

Just change this line:

strDesktop = WshShell.SpecialFolders("Desktop")

If to your own path you could use:

strDesktop = "c:\mypath\"

But you might also want to do the sensible thing and change the name of the strDesktop variable to something more useful and therefore change all further references of strDesktop in the rest of the code.

If you want to move it to another "special" location change it to, for example:

strStartup = WshShell.SpecialFolders("Startup")

And change references to strDesktop to strStartup.

Take a look at:
http://msdn.microsoft.com/en-us/library ... S.85).aspx

For "special" folders:
http://msdn.microsoft.com/en-us/library ... S.85).aspx
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Thu Feb 25, 2010 11:29 am

Hi Marcus,

That was quick thanks.

The location would need to be able to be set by a variable so I think I can do it with what you have done there. I'll try it out shortly.

I'm afraid VB Script is gobbledygook to me so I have to work quite hard to decipher it.

Thank you very much.

:-)
Phil Pendlebury - Linktree

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Thu Feb 25, 2010 12:41 pm

So if you want to set it via a variable:

VBSTART
Sub NewDesktopShortcut(strMyPath,linkfile,targetpath,parms,windowstyle,hotkey,iconlocation,descr)
Set WshShell = CreateObject("Wscript.Shell")
set oShellLink = WshShell.CreateShortcut(strMyPath & "\" & linkfile)
oShellLink.TargetPath = targetpath
oShellLink.Arguments = parms
oShellLink.WindowStyle = windowstyle
oShellLink.Hotkey = hotkey
oShellLink.IconLocation = iconlocation
oShellLink.Description = descr
oShellLink.WorkingDirectory = strMyPath
oShellLink.Save
End Sub
VBEND

Let>path=c:\something
VBRun>NewDesktopShortcut,path,etc,etc,etc,
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Phil Pendlebury
Automation Wizard
Posts: 543
Joined: Tue Jan 16, 2007 9:00 am
Contact:

Post by Phil Pendlebury » Thu Feb 25, 2010 12:59 pm

Awesome Marcus thank you.

I hope you don't think I am just asking you to write code for me without trying myself but in the case of VB I'm afraid there's little else I can do. I am trying my best to learn it though.

:-)
Phil Pendlebury - Linktree

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Sun May 09, 2010 4:45 pm

Thanks for the examples.

I have a question. How would I set the "Start in" option? I want it to be set to the directory a user selects. E.g. if the app is located in C:\3rdPartyAppFolder\app.exe I want it to be set to C:\3rdPartyAppFolder instead of "C:\Documents and Settings\USER_NAME\Desktop"

I know I can set it like this strDesktop = "C:\3rdPartyAppFolder" by creating the shortcut in the 3rdPartyAppFolder and then moving it to the desktop. But that's not going to work if the 3rdPartyAppFolder is located in C:\Programs Files\3rdPartyAppFolder

Thanks for any help or suggestions.

User avatar
Marcus Tettmar
Site Admin
Posts: 7395
Joined: Thu Sep 19, 2002 3:00 pm
Location: Dorset, UK
Contact:

Post by Marcus Tettmar » Mon May 10, 2010 8:58 am

I believe The "Start in" folder you see in shortcuts is stored in the WorkingDirectory property:

oShellLink.WorkingDirectory = strMyPath
Marcus Tettmar
http://mjtnet.com/blog/ | http://twitter.com/marcustettmar

Did you know we are now offering affordable monthly subscriptions for Macro Scheduler Standard?

User avatar
Rain
Automation Wizard
Posts: 550
Joined: Tue Aug 09, 2005 5:02 pm
Contact:

Post by Rain » Mon May 10, 2010 1:23 pm

Thanks.

I finally got it to work.

Here is the working code if anyone is interested.

Code: Select all

VBSTART
Sub NewDesktopShortcut(linkfile,targetpath,parms,windowstyle,hotkey,iconlocation,descr,strMyAppFolder)
  Set WshShell = CreateObject("Wscript.Shell")
  set oShellLink = WshShell.CreateShortcut(strMyAppFolder & "\" & linkfile)
  oShellLink.TargetPath = targetpath
  oShellLink.Arguments = parms
  oShellLink.WindowStyle = windowstyle
  oShellLink.Hotkey = hotkey
  oShellLink.IconLocation = iconlocation
  oShellLink.Description = descr
  oShellLink.WorkingDirectory = strMyAppFolder
  oShellLink.Save
End Sub
VBEND


Let>shortcut_icon_title=3rd Party App Name
Let>linkfile=%shortcut_icon_title%.lnk


Let>profile=%USER_NAME%


//Save shortcut to specified Folder and set "Start in" variable
let>strMyAppFolder=C:\MyAppFolder

//Let>targetpath=C:\consolegrab\ProfileManager\Profile Manager.exe
Let>targetpath=C:\MyAppFolder\app.exe

//Let>parms=/Profiletouse="%profile%" /Output="Commandline"
Let>parms=

//1=Normal, 3=Maximized, 7=Minimized
Let>windowstyle=1

//Let>hotkey=CTRL+SHIFT+R
Let>hotkey=CTRL+SHIFT+R

//Note: the icon image is contained within the app exe
//Let>iconlocation=C:\consolegrab\ProfileManager\Profile Manager.exe, 0
Let>iconlocation=C:\MyAppFolder\app.exe, 0

//Let>descr=Profile Manager Shortcut
Let>descr=Temp Shortcut


VBRun>NewDesktopShortcut,linkfile,targetpath,parms,windowstyle,hotkey,iconlocation,descr,strMyAppFolder


//Move shortcut to desktop
MoveFile>%strMyAppFolder%\%shortcut_icon_title%.lnk,%DESKTOP_DIR%\%shortcut_icon_title%.lnk


Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts