IT tutorials
 
Technology
 

Scripting Windows Home Server : Programming the WshShell Object (part 2) - Running Applications, Working with Shortcuts

9/1/2013 9:40:12 AM
- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. Running Applications

When you need your script to launch another application, use the Run method:

WshShell.Run strCommand[, intWindowStyle][, bWaitOnReturn]

WshShellThe WshShell object.
strCommandThe name of the file that starts the application. Unless the file is in the Windows folder, you should include the drive and folder to make sure that the script can find the file.
intWindowStyleA constant or number that specifies how the application window will appear:
 intWindowStyleWindow Appearance
 0Hidden
 1Normal size with focus
 2Minimized with focus (the default)
 3Maximized with focus
 4Normal without focus
 6Minimized without focus
bWaitOnReturnA logical value that determines whether the application runs asynchronously. If this value is True, the script halts execution until the user exits the launched application; if this value is False, the script continues running after it has launched the application.

Here’s an example:

Set objWshShell = WScript.CreateObject("WScript.Shell")
objWshShell.Run "Control.exe Inetcpl.cpl", 1, True

This Run method launches the Control Panel’s Internet Properties dialog box.

4. Working with Shortcuts

The WSH 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\" & _
"Administrator\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:\ToDoList.txt

    In other words, this shortcut launches Notepad and loads a file named ToDoList.txt. In this case, the Arguments property would return the following string:

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

  • FullName— Returns the full path and filename of the shortcut’s target. This is 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 23.

    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 Path is the full pathname of the icon file and Index is the position of the icon within the file (where the first icon is 0).

    Here’s an example:

    objShortcut.IconLocation = "C:\Windows\System32\Shell32.dll,18"
    TargetPathReturns or sets the path of the shortcut’s target.
    WindowStyleReturns or sets the window style used by the shortcut’s target. Use the same values outlined earlier for the Run method’s intWindowStyle argument.
    WorkingDirectoryReturns 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, as follows:

SaveSaves the shortcut file to disk.
ResolveUses the shortcut’s TargetPath property to look up the target file. Here’s the syntax:
WshShortcut.Resolve = intFlag

WshShortcutThe WshShortcut object.
intFlagDetermines what happens if the target file is not found:
 intFlagWhat Happens
 1Nothing.
 2Windows continues to search subfolders for the target file.
 4Windows updates the TargetPath property if the target file is found in a new location.

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

Listing 3. A Script That Creates a Shortcut File
Option Explicit
Dim objWshShell, objShortcut
Set objWshShell = WScript.CreateObject("WScript.Shell")
Set objShortcut = objWshShell.CreateShortcut("C:\Users\" & _
"Administrator\Desktop\Edit Hosts File.lnk")
With objShortcut
.TargetPath = "C:\Windows\Notepad.exe"
.Arguments = "C:\Windows\System32\Drivers\etc\hosts"
.WorkingDirectory = "C:\"
.Description = "Opens the hosts file in Notepad"
.Hotkey = "Ctrl+Alt+7"
.IconLocation = "C:\Windows\System32\Shell32.dll,18"
.WindowStyle = 3
.Save
End With
 
Others
 
- Scripting Windows Home Server : Programming the WshShell Object (part 1) - Displaying Information to the User
- Developer Tooling for Sharepoint 2013 : Developing SharePoint Applications Using SharePoint Designer
- Developer Tooling for Sharepoint 2013 : Site Settings
- Developer Tooling for Sharepoint 2013 : SharePoint Development Across Developer Segments, Web-Based Development in SharePoint
- Windows Small Business Server 2011 : Configuring the Windows Update Client Using Group Policy
- Windows Small Business Server 2011 : Configuring WSUS Using the Windows SBS Console
- Windows Server 2008 : Using the Integrated Scripting Environment - Executing Commands in the ISE, Creating and Saving a Script in the ISE
- Windows Server 2008 : Using the Integrated Scripting Environment - Launching the ISE, Exploring the ISE
- Windows 7 : Scanning Documents - Scanning with Windows Fax and Scan, Using scanned documents
- Windows 7 : Receiving Faxes, Working with Faxes
 
 
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
Technology FAQ
- Is possible to just to use a wireless router to extend wireless access to wireless access points?
- Ruby - Insert Struct to MySql
- how to find my Symantec pcAnywhere serial number
- About direct X / Open GL issue
- How to determine eclipse version?
- What SAN cert Exchange 2010 for UM, OA?
- How do I populate a SQL Express table from Excel file?
- code for express check out with Paypal.
- Problem with Templated User Control
- ShellExecute SW_HIDE
programming4us programming4us