WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Shell
plus

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

Pin Items to the Start Menu or Windows Taskbar

 Keywords:  Pin Unpin ShortCut .LNK Items Start Menu Taskbar Verbs DoIt Method Windows 7 8 Shell.Application 

After some investigation, I found programmatically pinning a shortcut file (*.lnk) onto the Windows 7 Taskbar is "not permitted" by Windows.

A small set of applications are pinned by default for new installations. Other than these, only the user can pin further applications; programmatic pinning by an application is not permitted.

Reference: http://msdn.microsoft.com/en-us/library/dd378460(VS.85).aspx

However, an exception comes from the Windows Shell.

#DefineFunction ShortCutPin(file,type)
   ;***************************************************************************
   ;**          Pin Shortcut
   ;**
   ;** file: name of the file you would like to Pin
   ;** type: specifies where you would like to pin the Item
   ;**       0 = Taskbar
   ;**       1 = StartMenu
   ;***************************************************************************
   verblist = 0
   rslt = 0
   objShell = CreateObject("Shell.Application")
   objFolder = objShell.NameSpace(FilePath(file))
   objFolderItem = objFolder.ParseName(FileRoot(file):'.':FileExtension(file))
   colVerbs = objFolderItem.Verbs
   Switch Type
      Case 0 ; TaskBar
         verb = 'Pin to Tas&kbar'
         Break
      Case 1 ; Start Menu
         verb = 'Pin to Start Men&u'
         Break
      Case Type
         Pause('Notice','Unsupported Type')
         Return rslt
         Break
   EndSwitch
   ForEach objVerb In colVerbs
      If objVerb.name == verb
        objVerb.DoIt
        rslt = 1
        Break
      EndIf
   Next
   Return rslt
#EndFunction

#DefineFunction ShortCutUnPin(file,type)
   ;***************************************************************************
   ;**          UnPin   Shortcut
   ;**
   ;** file: name of the file you would like to Unpin
   ;** type: specifies where you would like to Unpin the Item
   ;**       0 = Taskbar
   ;**       1 = StartMenu
   ;***************************************************************************
   verblist = 0
   rslt = 0
   objShell = CreateObject("Shell.Application")
   objFolder = objShell.NameSpace(FilePath(file))
   objFolderItem = objFolder.ParseName(FileRoot(file):'.':FileExtension(file))
   colVerbs = objFolderItem.Verbs
   Switch Type
      Case 0 ; TaskBar
         verb = 'Unpin from Tas&kbar'
         Break
      Case 1 ; Start Menu
         verb = 'Unpin from Start Men&u'
         Break
      Case Type
         Pause('Notice','Unsupported Type')
         Return rslt
         Break
   EndSwitch
   ForEach objVerb In colVerbs
      If objVerb.name == verb
        objVerb.DoIt
        rslt = 1
        Break
      EndIf
   Next
   Return rslt
#EndFunction

#DefineFunction ShortCutListVerbs(file)
   ;***************************************************************************
   ;**          List Available Verbs
   ;**
   ;** file: name of the file you would like to Unpin
   ;**
   ;** NOTE: Verbs returned are dependant on the file type and current status
   ;**       of the file. For Example if the file is already pinned to the taskbar
   ;**       the file will nolonger have the Pin to Taskbar option but instead will
   ;**       have the Unpin from Tas&kbar option.
   ;***************************************************************************
   verblist = 0
   objShell = CreateObject("Shell.Application")
   objFolder = objShell.NameSpace(FilePath(file))
   objFolderItem = objFolder.ParseName(FileRoot(file):'.':FileExtension(file))
   colVerbs = objFolderItem.Verbs
   ForEach objVerb In colVerbs
      If verblist == "" Then  verblist = objVerb.name:@TAB
      Else verblist = verblist:objVerb.name:@TAB
   Next
   Return verblist
#EndFunction

#DefineFunction ShortCutInvokeVerb(file,verbnum)
   ;***************************************************************************
   ;**          Invoke verb
   ;**
   ;** file: name of the file you would like to Unpin
   ;** verbnum: Zero-based
   ;**
   ;** NOTE: Invokes the verb by the verb number. Verb numbers can be different on
   ;**       each system depending on what right click menu options are installed.
   ;**       For Example if the system has a file comparison utility installed then
   ;**       they may have the option/Verb "Select Left Side Compare". This will change
   ;**       the position of verb numbers.
   ;***************************************************************************
   objShell = CreateObject("Shell.Application")
   objFolder = objShell.NameSpace(FilePath(file))
   objFolderItem = objFolder.ParseName(FileRoot(file):'.':FileExtension(file))
   objVerb = objFolderItem.Verbs.Item(verbnum)
   rslt = AskYesNo( 'Are you sure', 'Do you want to execute the verb ':objVerb.name :'?' )
   If rslt == 0 Then objVerb.DoIt
   Return rslt
#EndFunction



file = 'C:\Program Files (x86)\WinBatch\System\browser.exe'

;List Available Verbs
verblist = ShortCutListVerbs(file)
daVerbName = AskItemlist('Available Verbs - Select Properties', verblist, @TAB, @UNSORTED, @SINGLE )

;Invoke that verb by its zero based verb numer
daVerbNum = ItemLocate( daVerbName, verblist, @TAB )-1  ; subtract one for the zero-based number to pass.
ShortCutInvokeVerb(file,daVerbNum)
Pause('Invoked ': daVerbName,file)

;PIN
ShortCutPin(file, 0) ;Taskbar
ShortCutPin(file, 1) ;Start Menu
Pause('Pinned',file)

;UNPIN
ShortCutUnPin(file, 0) ;Taskbar
ShortCutUnPin(file, 1) ;Start Menu
Pause('Unpinned',file)

Exit



More code samples:

;The verbs Pin to Tas&kbar and Pin to Start Men&u are available as verbs for Calculator on my Windows 7 machine.
;(The & in the verb precedes the letter that can be used to select that verb from the menu using the keyboard.)
; You can use the Shell Objects for Scripting to programmatically execute these verbs.


; Pin Calculator to the Start Menu:
verb = "Pin to Start Menu"
CSIDL_COMMON_PROGRAMS = 23 ;&H17
CSIDL_PROGRAMS = 2; &H2
objShell = CreateObject("Shell.Application")
objAllUsersProgramsFolder = objShell.NameSpace(CSIDL_COMMON_PROGRAMS)
strAllUsersProgramsPath = objAllUsersProgramsFolder.Self.Path
strAccessoriesPath = strAllUsersProgramsPath  : "\Accessories"
objFolder = objShell.Namespace(strAccessoriesPath)
objFolderItem = objFolder.ParseName("Calculator.lnk")
colVerbs = objFolderItem.Verbs
ForEach objVerb In colVerbs
    If StrReplace(objVerb.name, "&", "") == verb Then objVerb.DoIt
Next



; Pin Calculator to the TaskBar:
verb = "Pin to Task"
CSIDL_COMMON_PROGRAMS = 23 ;&H17
CSIDL_PROGRAMS = 2; &H2
objShell = CreateObject("Shell.Application")
objAllUsersProgramsFolder = objShell.NameSpace(CSIDL_COMMON_PROGRAMS)
strAllUsersProgramsPath = objAllUsersProgramsFolder.Self.Path
strAccessoriesPath = strAllUsersProgramsPath  : "\Accessories"
objFolder = objShell.Namespace(strAccessoriesPath)
objFolderItem = objFolder.ParseName("Calculator.lnk")
colVerbs = objFolderItem.Verbs
ForEach objVerb In colVerbs
    If StrReplace(objVerb.name, "&", "") == verb Then objVerb.DoIt
Next
Exit




; Get a list of supported  Verbs
CSIDL_COMMON_PROGRAMS = 23 ;&H17
CSIDL_PROGRAMS = 2; &H2
objShell = CreateObject("Shell.Application")
objAllUsersProgramsFolder = objShell.NameSpace(CSIDL_COMMON_PROGRAMS)
strAllUsersProgramsPath = objAllUsersProgramsFolder.Self.Path
strAccessoriesPath = strAllUsersProgramsPath  : "\Accessories"
objFolder = objShell.Namespace(strAccessoriesPath)
objFolderItem = objFolder.ParseName("Calculator.lnk")
colVerbs = objFolderItem.Verbs
ForEach objVerb In colVerbs
    verb = objVerb.name
    Pause('Verb', verb )
Next

Article ID:   W18189
Filename:   Pin Items to the Start Menu or Windows 7 Taskbar .txt
File Created: 2013:01:22:12:53:16
Last Updated: 2013:01:22:12:53:16