Click to Rate and Give Feedback
MSDN
MSDN Library
User Interface
Windows Shell
Application Support
 Scriptable Shell Objects
Scriptable Shell Objects

The Microsoft Windows Shell provides a powerful set of automation objects that enable you to program the Shell with Microsoft Visual Basic and scripting languages such as Microsoft JScript (compatible with ECMA 262 language specification) and Visual Basic Scripting Edition (VBScript). You can use these objects to access many of the Shell's features and dialog boxes. For example, you can access the file system, launch programs, and change system settings.

This section introduces the scriptable Shell objects.

Shell Versions

Many of the Shell objects became available in version 4.71 of the Shell. Others are available in version 5.00 and later. This means that on computers running Windows 95 or Microsoft Windows NT 4.0, the Shell objects are available only if the integrated desktop is installed. Version 4.71 of the Shell is on all Windows 98 computers, and version 5.00 is on all Windows 2000 computers. The following table lists each Shell object under the version of the Shell in which the object became available.

Version 4.71Version 5.00
FolderDIDiskQuotaUser
FolderItemVerbDiskQuotaControl
FolderItemVerbsFolder2
ShellFolderItem
ShellFolderViewFolderItems
ShellUIHelperFolderItems2
ShellWindowsIShellDispatch2
WebViewFolderContentsIShellLinkDual2
ShellFolderItem
ShellFolderViewOC
ShellLinkObject

Instantiating Shell Objects

To instantiate the Shell objects in Visual Basic applications with early binding, add references to the following libraries in your project:

  • Microsoft Internet Controls (SHDocVw)
  • Microsoft Shell Controls and Automation (Shell32)

Late Binding

You can also instantiate many of the Shell objects with late binding. This approach works in Visual Basic applications and in script. The following example shows how to instantiate the Shell object in JScript.

<SCRIPT LANGUAGE="JScript">
<!--
  function fnCreateShell()    
  {
      // Instantiate the Shell object and invoke its FileRun method.
      var oShell = new ActiveXObject("Shell.Application");
      oShell.FileRun;
  }
-->
</SCRIPT>

The following example shows how to instantiate the Folder object in VBScript.

<SCRIPT LANGUAGE="VBScript">
<!--
  function fnCreateFolder()
      dim oShell    
      dim oFolder
      dim sDir

      sDir = "C:\SomePath" 
      set oShell = CreateObject("Shell.Application")
      set oFolder = oShell.NameSpace(sDir)  
  end function
-->  
</SCRIPT>

In the preceding example, sDir is the path to the Folder object. Note that the ShellSpecialFolderConstants enumeration values are not available in script.

The ProgID for each of the Shell objects is shown in the following table.

ObjectProgID
DIDiskQuotaUserMicrosoft.DiskQuota.1
DiskQuotaControlCannot late bind
FolderShell.Application.NameSpace("...")
Folder2Shell.Application.NameSpace("...")
FolderItemShell.Application.NameSpace("...").Self or Folder.Items.Item or Folder.ParseName
FolderItemsFolder.Items
FolderItems2Folder.Items
FolderItemVerbShell.NameSpace("...").Self.Verbs.Item()
FolderItemVerbsFolderItem.Verbs or Shell.NameSpace("...").Self.Verbs
IShellDispatch2Shell.Application
IShellLinkDual2Shell.NameSpace("...").Self.GetLink or Shell.NameSpace("...").Items().GetLink
ShellShell.Application
ShellFolderItemShell.NameSpace("...").Self or Shell.NameSpace("...").Items()
ShellFolderViewCannot late bind
ShellFolderViewOCCannot late bind
ShellLinkObjectShell.NameSpace("...").Self.GetLink or Shell.NameSpace("...").Items().GetLink
ShellUIHelperCannot late bind
ShellWindowsShell.Windows or ShellWindows._NewEnum
WebViewFolderContentsCannot late bind

HTML OBJECT Element

You can also use the OBJECT element to instantiate Shell objects on an HTML page. To do this, set the OBJECT element's ID attribute to the variable name you will use in your scripts, and identify the object using its registered number (CLASSID). The following HTML creates an instance of the ShellFolderItem object using the OBJECT element.

<OBJECT ID="oShFolderItem" 
    NAME="Shell Folder Item Object"
    CLASSID="clsid:2fe352ea-fd1f-11d2-b1f4-00c04f8eeb3e">
</OBJECT>

The following table lists each Shell object and its respective CLASSID.

DIDiskQuotaUser7988B571-EC89-11cf-9C00-00AA00A14F56
DiskQuotaControl7988B571-EC89-11cf-9C00-00AA00A14F56
FolderBBCBDE60-C3FF-11CE-8350-444553540000
Folder2f0d2d8ef-3890-11d2-bf8b-00c04fb93661
FolderItem744129E0-CBE5-11CE-8350-444553540000
FolderItems744129E0-CBE5-11CE-8350-444553540000
FolderItems2C94F0AD0-F363-11d2-A327-00C04F8EEC7F
FolderItemVerb08EC3E00-50B0-11CF-960C-0080C7F4EE85
FolderItemVerbs1F8352C0-50B0-11CF-960C-0080C7F4EE85
IShellDispatch2A4C6892C-3BA9-11d2-9DEA-00C04FB16162
IShellLinkDual2317EE249-F12E-11d2-B1E4-00C04F8EEB3E
Shell13709620-C279-11CE-A49E-444553540000
ShellFolderItem2fe352ea-fd1f-11d2-b1f4-00c04f8eeb3e
ShellFolderView62112AA1-EBE4-11cf-A5FB-0020AFE7292D
ShellFolderViewOC4a3df050-23bd-11d2-939f-00a0c91eedba
ShellLinkObject11219420-1768-11d1-95BE-00609797EA4F
ShellUIHelper64AB4BB7-111E-11D1-8F79-00C04FC2FBE1
ShellWindows9BA05972-F6A8-11CF-A442-00A0C90A8F39
WebViewFolderContents1820FED0-473E-11D0-A96C-00C04FD705A2

Shell Object

The Shell object represents the objects in the Windows Shell. You can use the methods exposed by the Shell object to:

  • Open, explore, and browse for folders.
  • Minimize, restore, cascade, or tile open windows.
  • Launch Control Panel applications.
  • Display system dialog boxes.

Users are perhaps most familiar with the commands they access from the Start menu and the taskbar's shortcut menu. The taskbar's shortcut menu appears when users right-click the taskbar. The following HTML Application (HTA) produces a start page with buttons that implement many of the Shell object's methods. Some of these methods implement features on the Start menu and the taskbar's shortcut menu.

<HTML>
<HEAD>
<TITLE>Start Page</TITLE>

<OBJECT ID="oShell"
    CLASSID="clsid:13709620-C279-11CE-A49E-444553540000">
</OBJECT>

<STYLE>
    INPUT {width: 200} 
</STYLE>  

<SCRIPT LANGUAGE="VBScript">
<!--
  function fnStart(sMethod)
    select case sMethod
      case 0    
          'Minimizes all windows on the desktop
        oShell.MinimizeAll
      case 1  
          'Displays the Run dialog box
        oShell.FileRun
      case 2  
          'Displays the Shut Down Windows dialog box
        oShell.ShutdownWindows
      case 3  
          'Displays the Find dialog box
        oShell.FindFiles
      case 4  
          'Displays the Date/Time dialog box
        oShell.SetTime 
      case 5  
          'Displays the Internet Properties dialog box
        oShell.ControlPanelItem "INETCPL.cpl"
      case 6  
          'Explores the My Documents folder
        oShell.Explore "C:\My Documents"
      case 7  
          'Enables user to select folder from Program Files
        oShell.BrowseForFolder 0, "My Programs", 0, "C:\Program Files" 
      case 8  
          'Opens the Favorites folder
        oShell.Open "C:\WINDOWS\Favorites"
      case 9  
          'Displays the Taskbar Properties dialog box
        oShell.TrayProperties
    end select  
  end function      
-->
</SCRIPT>

</HEAD>

<BODY>

<H1>Start...</H1>
<INPUT type="button" value="Edit Taskbar Properties" onclick="fnStart(9)"><br>
<INPUT type="button" value="Open Favorites Folder" onclick="fnStart(8)"><br>
<INPUT type="button" value="Browse Program Files" onclick="fnStart(7)"><br>
<INPUT type="button" value="Explore My Documents" onclick="fnStart(6)"><br>
<INPUT type="button" value="Modify Internet Properties" onclick="fnStart(5)"><br>
<INPUT type="button" value="Set System Time" onclick="fnStart(4)"><br>
<INPUT type="button" value="Find a File or Folder" onclick="fnStart(3)"><br>
<INPUT type="button" value="Shut Down Windows" onclick="fnStart(2)"><br>
<INPUT type="button" value="Run" onclick="fnStart(1)">     
<INPUT type="button" value="Minimize All Windows" onclick="fnStart(0)">     
</BODY>
</HTML>

Security

As an application, an HTA runs under a different security model than a Web page. To interact with a Web page that implements the functionality of the Shell objects, users must enable the Initialize and script ActiveX Controls not marked as safe option for the security zone in which they are viewing the page.

Folder Objects

The Folder object represents a Shell folder. You can use the methods exposed by the Folder object to:

  • Get information about a folder.
  • Create subfolders.
  • Copy and move file objects into the folder.

The FolderItem object represents an item in a Shell folder. Its properties enable you to retrieve information about the item. You can use the methods exposed by this object to run an item's verbs, or to retrieve information about an item's FolderItemVerbs object.

The FolderItems object represents a collection of items in a Shell folder. Its methods and properties enable you to retrieve information about the collection.

The following Visual Basic example shows the relationship between several of the folder objects and how they can be used together. When the user clicks the command button called cmdGetPath, the program displays a dialog box that enables the user to select a folder from My Computer, where ssfDRIVES is the ShellSpecialFolderConstants enumeration value for My Computer. When the user chooses a folder, the folder's path is displayed in the text box called txtPath.

Private Sub cmdGetPath_Click()
  Dim oShell As New Shell
  Dim oFolder As Folder
  Dim oFolderItem As FolderItem
 
  Set oFolder = oShell.BrowseForFolder(Me.hWnd, "Select a Folder", 0, ssfDrives)
   
  Set oFolderItem = oFolderItems.Item

  txtPath.Text = oFolderItem.Path
End Sub

In VBScript, this function is slightly different because the ShellSpecialFolderConstants enumeration values are not available in script. The following example shows the VBScript equivalent of the previous example.

<SCRIPT LANGUAGE="VBScript">
<!--
  function fnGetMyPathVB() 
    dim oShell
    dim oFolder
    dim oFolderItem
		
    set oShell = CreateObject("Shell.Application")		
    set oFolder = oShell.BrowseForFolder(0, "Choose a Folder", 0)        		
    set oFolderItem = oFolder.Items.Item		 
		
    document.all.item("myPath").innerText = oFolderItem.Path                                
  end function
-->
</SCRIPT>

In the following JScript example, which is a direct translation of the preceding VBScript example, note how the empty parentheses '()' are used to invoke the Items and Item methods.

<SCRIPT LANGUAGE="JavaScript">
<!--
  function fnGetMyPathJ() 
  {		
    var oShell = new ActiveXObject("Shell.Application");
    			
    var oFolder = new Object;					
    oFolder = oShell.BrowseForFolder(0, "Choose a folder", 0);
    						
    var oFolderItem = new Object;		
    oFolderItem = oFolder.Items().Item();								
    
    document.all.item("myPath").innerText = oFolderItem.Path;
  }    
-->
</SCRIPT>
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker