Logo
programming4us
programming4us
programming4us
programming4us
Home
programming4us
XP
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server
programming4us
Windows Phone
 
Windows Vista

Programming the WshShell Object : Working with Shortcuts

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
3/4/2011 5:42:21 PM
The Windows Script Host enables your scripts to create and modify shortcut files. When writing scripts for other users, you might want to take advantage of this capability to display shortcuts for new network shares, Internet sites, instruction files, and so on.

Creating a Shortcut

To create a shortcut, use the CreateShortcut method:

WshShell.CreateShortcut(strPathname)

WshShellThe WshShell object.
strPathnameThe full path and filename of the shortcut file you want to create. Use the .lnk extension for a file system (program, document, folder, and so on) shortcut; use the .url extension for an Internet shortcut.

The following example creates and saves a shortcut on a user’s desktop:

Set WshShell = objWScript.CreateObject("WScript.Shell")
Set objShortcut = objWshShell.CreateShortcut("C:\Users\Paul\Desktop\test.lnk")
objShortcut.Save


Programming the WshShortcut Object

The CreateShortcut method returns a WshShortcut object. You can use this object to manipulate various properties and methods associated with shortcut files.

This object contains the following properties:

  • Arguments— Returns or sets a string that specifies the arguments used when launching the shortcut. For example, suppose that the shortcut’s target is the following:

    C:\Windows\Notepad.exe C:\Boot.ini

    In other words, this shortcut launches Notepad and loads the Boot.ini file. In this case, the Arguments property would return the following string:

    C:\Boot.ini
  • Description— Returns or sets a string description of the shortcut.

  • FullName— Returns the full path and filename of the shortcut’s target. This will be the same as the strPathname value used in the CreateShortcut method.

  • Hotkey— Returns or sets the hotkey associated with the shortcut. To set this value, use the following syntax:

    WshShortcut.Hotkey = strHotKey
    WshShortcutThe WshShortcut object.
    strHotKeyA string value of the form Modifier+Keyname, where Modifier is any combination of Alt, Ctrl, and Shift, and Keyname is one of A through Z or 0 through 12.

    For example, the following statement sets the hotkey to Ctrl+Alt+7:

    objShortcut.Hotkey = "Ctrl+Alt+7"
  • IconLocation— Returns or sets the icon used to display the shortcut. To set this value, use the following syntax:

    WshShortcut.IconLocation = strIconLocation
    WshShortcutThe WshShortcut object.
    strIconLocationA string value of the form Path,Index, where PathIndex is the position of the icon within the file (where the first icon is 0). is the full pathname of the icon file and

    Here’s an example:

    objShortcut.IconLocation = "C:\Windows\System32\Shell32.dll,21"
  • TargetPath— Returns or sets the path of the shortcut’s target.

  • WindowStyle— Returns or sets the window style used by the shortcut’s target. Use the same values outlined earlier for the Run method’s intWindowStyle argument.

  • WorkingDirectory— Returns or sets the path of the shortcut’s working directory.

Note

If you’re working with Internet shortcuts, bear in mind that they support only two properties: FullName and TargetPath (the URL target).


The WshShortcut object also supports two methods:

  • Save— Saves the shortcut file to disk.

  • Resolve— Uses the shortcut’s TargetPath property to look up the target file. Here’s the syntax:

    WshShortcut.Resolve = intFlag
    WshShortcutThe WshShortcut object.
    intFlagDetermines what happens of the target file is not found:

    intFlagWhat Happens
    1Nothing
    2Windows continues to search subfolders for the target file
    4Updates the TargetPath property if the target file is found in a new location

Listing 1 shows a complete example of a script that creates a shortcut.

Listing 1. A Script That Creates a Shortcut File
Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objShortcut = objWshShell.CreateShortcut("C:\Users\Paul\Desktop\Edit BOOT.INI.lnk")
With objShortcut
.TargetPath = "C:\Windows\Notepad.exe "
.Arguments = "C:\Boot.ini"
.WorkingDirectory = "C:\"
.Description = "Opens BOOT.INI in Notepad"
.Hotkey = "Ctrl+Alt+7"
.IconLocation = "C:\Windows\System32\Shell32.dll,21"
.WindowStyle = 3
.Save
End With

Other -----------------
- Programming the WshShell Object : Running Applications
- Programming the WshShell Object : Displaying Information to the User
- Programming the Windows Script Host : Programming the WScript Object
- Programming the Windows Script Host : Programming Objects
- Programming the Windows Script Host : Scripts and Script Execution
- Getting to Know the Windows Vista Registry - Finding Registry Entries
- Getting to Know the Windows Vista Registry - Working with Registry Entries
- Getting to Know the Windows Vista Registry - Keeping the Registry Safe
- Getting to Know the Windows Vista Registry - Understanding the Registry Files
- Getting to Know the Windows Vista Registry - A Synopsis of the Registry
 
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
 
programming4us
Windows Vista
programming4us
Windows 7
programming4us
Windows Azure
programming4us
Windows Server