×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Adding an icon to a shortcut
2

Adding an icon to a shortcut

Adding an icon to a shortcut

(OP)
Hi

I've seen code that will create a .lnk shortcut but I need to do the equivalent for a .url shortcut. I seem to remember finding the code somewhere on t'internet but then abandoning the idea in favour of a .BAT solution (because I know batch far better) only then to be told that I must use vbscript.

So, I've written working vbscript code to copy the .URL shortcuts from a central repository to the All Users Startup folder but I reckon it'll be quicker, and have less of an impact on the network, if I can produce code to create new shortcuts instead. However, what I can't find anything on is how to specify which icon from a (Windows standard and present on the client PC) .DLL to use for the shortcut instead of the rather dull default IE icon.

This must be possible, right? But how?

Thanks for all responses in advance (as well as afterwards!)

Jeff

RE: Adding an icon to a shortcut

Yes it is possible and easy to do.

The property you want to set is iconlocation

Defines the location of the shortcut’s icon. Typically, its value is the complete path and filename to
the file containing the icon followed by a comma and the zero- based position of the icon within
the file. If the default icon is used, the value of IconLocation is ” ,0”.
TargetPath Sets or returns the path and filename to the shortcut’s executable file.

Here is an example of how to do it.

CODE

SET fso = Wscript.CreateObject("Scripting.FileSystemObject")
SET WshShell = WScript.CreateObject("WScript.Shell")
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")
strDsk = WshShell.SpecialFolders("Desktop")
strshortcut = strDsk & "\Calculator.lnk"
If Not fso.FileExists(strshortcut) Then
    SET oUrlLink = WshShell.CreateShortcut(strshortcut)
    oUrlLink.TargetPath = Windir & "\System32\Calc.EXE"
    oUrlLink.IconLocation = Windir & "\System32\moricons.dll,6"
    oUrlLink.Save
End If

I hope you find this post helpful.  

Regards,

Mark

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

RE: Adding an icon to a shortcut

(OP)
That's marvellous. One query though, will this be significantly different bearing in mind I'm creating a url shortcut (http://www.website.com) rather than to a shortcut to an .exe?

And by the by, how do you add an event to an event log (local or to a server)?

Thanks for this.

JJ

RE: Adding an icon to a shortcut

There is no difference between a LNK or URL when creating the shortcut as far as this property is concerned.

Sorry I can not assist with writing to the event log.  I suggest posting that question in a new thread so you get new sets of eyes looking to respond.

I hope you find this post helpful.  

Regards,

Mark

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

RE: Adding an icon to a shortcut

OK Curiosity got the best of me and it turns out it is quite simple.

CODE

Const EVENT_SUCCESS = 0

Set objShell = Wscript.CreateObject("Wscript.Shell")

objShell.LogEvent EVENT_SUCCESS, _
    "Payroll application successfully installed."

I like this so much I can see myself adding this feature to most of my scripts.  Thanks for getting my curiosity up. smile

I hope you find this post helpful.  

Regards,

Mark

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

RE: Adding an icon to a shortcut

(OP)
Glad I could help...

winky smile

And thanks for yours!

JJ

RE: Adding an icon to a shortcut

(OP)
Just for the record, the IconLocation property doesn't seem to work for .URL icons, only .LNK icons.

As a .LNK icon works just as well as a .URL it's hardly a show-stopper but still...

JJ

RE: Adding an icon to a shortcut

>Just for the record, the IconLocation property doesn't seem to work for .URL icons, only .LNK icons.

If you want to do it for url shortcut, you can take a look at my post here.
    http://tek-tips.com/viewthread.cfm?qid=919812

RE: Adding an icon to a shortcut

Sorry I neglected to mention how I cheat on my method.  If you give the icon an extension of LNK but point it to a URL all of the above code works without having to do the extra steps that tsuji points to.  Tsuji's solution is really nice however in that it supports pointing to an ICO file where the Shortcut method only supports pointing to an EXE or DLL.  I usually just go with the DLL method and use a program to add my ICO files to the DLL.

Here is an example that create a link to CNN.Com with a world map icon.

CODE


SET fso = Wscript.CreateObject("Scripting.FileSystemObject")
SET WshShell = WScript.CreateObject("WScript.Shell")
WinDir = WshShell.ExpandEnvironmentStrings("%WinDir%")
strDsk = WshShell.SpecialFolders("Desktop")
strshortcut = strDsk & "\CNN.lnk"
If Not fso.FileExists(strshortcut) Then
    SET oUrlLink = WshShell.CreateShortcut(strshortcut)
    oUrlLink.TargetPath = "http://www.cnn.com"
    oUrlLink.IconLocation = Windir & "\System32\moricons.dll,61"
    oUrlLink.Save
End If

I hope you find this post helpful.  

Regards,

Mark

Check out my scripting solutions at http://www.thespidersparlor.com/vbscript

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login


Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close