Loading blog entries.. loading

Programmatically PIN shortcut onto Taskbar on Win7

Saturday, July 17, 2010 10:07 PM
Written by Wayne Ye
Font Size: S  M  L 

Background

During my work I got one requirement of pinning a specific shortcut file (*.lnk) onto the Windows 7 Taskbar, after investigating I found programmtically achive this is “not permitted”, refer MSDN article: http://msdn.microsoft.com/en-us/library/dd378460(VS.85).aspx

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.

However, the exception comes from Windows Script Hosting, a snippet of VBscript can achieve my requirement, I read several articles (refer the end of this post) and wrote two VBS scripts showing below:

Pin to Taskbar

Option Explicit

'Const CSIDL_COMMON_PROGRAMS = &H17
Dim ShellApp, FSO, Desktop
Set ShellApp = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")

'Set StartMenuFolder = ShellApp.NameSpace(CSIDL_COMMON_PROGRAMS)
Set Desktop =  ShellApp.NameSpace("C:\Users\Wayne\Desktop")

Dim LnkFile
LnkFile = Desktop.Self.Path&"\ScheduleNotifier.lnk"

If(FSO.FileExists(LnkFile)) Then
    Dim tmp, verb
    'For Each verb in Desktop.ParseName("ScheduleNotifier.lnk").Verbs
        'tmp = tmp&verb&chr(13)
    'Next
    'MsgBox(tmp)

    Dim desktopImtes, item
    Set desktopImtes = Desktop.Items()

    For Each item in desktopImtes
        If (item.Name = "ScheduleNotifier") Then
            'MsgBox(item.Name)
            For Each verb in item.Verbs
                If (verb.Name = "Pin to Tas&kbar") Then 'If (verb.Name = "锁定到任务栏(&K)")
                    verb.DoIt
                End If
            Next
        End If
    Next

End If

Set FSO = Nothing
Set ShellApp = Nothing

Unpin from Taskbar

Option Explicit
'Const CSIDL_COMMON_PROGRAMS = &H17
Dim ShellApp, FSO, Desktop
Set ShellApp = CreateObject("Shell.Application")
Set FSO = CreateObject("Scripting.FileSystemObject")

'Set StartMenuFolder = ShellApp.NameSpace(CSIDL_COMMON_PROGRAMS)
Set Desktop =  ShellApp.NameSpace("C:\Users\Wayne\Desktop")

Dim LnkFile
LnkFile = Desktop.Self.Path&"\ScheduleNotifier.lnk"

If(FSO.FileExists(LnkFile)) Then
    Dim tmp, verb
    'For Each verb in Desktop.ParseName("ScheduleNotifier.lnk").Verbs
        'tmp = tmp&verb&chr(13)
    'Next
    'MsgBox(tmp)

    Dim desktopImtes, item
    Set desktopImtes = Desktop.Items()

    For Each item in desktopImtes
        If (item.Name = "ScheduleNotifier") Then
            'MsgBox(item.Name)
            For Each verb in item.Verbs
                If (verb.Name = "Unpin from Tas&kbar") Then 'If (verb.Name = "从任务栏脱离(&K)")
                    verb.DoIt
                End If
            Next
        End If
    Next

End If

Set FSO = Nothing
Set ShellApp = Nothing

Although two snippets of VBS code are simple and stright-forward, they has on serious limitation, it only support one specified language, attention of this line of code:

If (verb.Name = "Pin to Tas&kbar") Then 'If (verb.Name = "锁定到任务栏(&K)")

On Chinese locale, I have to replace “Pin to Tas&kbar” with “锁定到任务栏(&K)”, so in order to support more languages I need to know each exact {Pin sentence} to do the pinning, you know, google translation won’t always work in this case:), so I definitely don’t want to do that…

You probably ask WHY??? Because FolderItemVerb object only has one property: Name, and Name is definitely “language sensitive“, MSDN does mention there are two more properties “Application” and “Parent” however they are “Not implemented”.

Several tips

  • For a specific Windows user, all shortcuts pinned onto taskbar store the shortcuts files under
    %AppData%\Microsoft\Internet Explorer\Quick Launch\User Pinned\TaskBar
  • OEM manufacturers could “pre-pin” some shortcut onto the Taskbar before shipping the PC/laptops to the consumers, simply run batch command below during the DASH (see Desktop and mobile Architecture for System Hardware) process:
    Rem Pining OEM Welcome Center app on task barReg add HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\TBDEn /v SBOEM0 /t REG_EXPAND_SZ /d “SomeFile.lnk” /f

     

    References

    Pin Items to the Start Menu or Windows 7 Taskbar via Script
    http://blogs.technet.com/b/deploymentguys/archive/2009/04/08/pin-items-to-the-start-menu-or-windows-7-taskbar-via-script.aspx

    Pin and Un-pin items to/from the Windows 7 taskbar
    http://blog.ananthonline.net/?p=37

  •  

    Permalink: http://wayneye.com/Blog/Programmatically-PIN-shortcut-Onto-Taskbar-on-Win7 13443 Views  3 Comments
    Tag: Category:Programming»Windows Development»CMD/PowerShell

     

     


    ↓Comments↓

      questionTuesday, February 21, 2012 10:37 PM
    这是什么方法做的? 有什么api吗? vc怎么实现这个?
     
      dgFriday, September 6, 2013 10:15 AM
    hey I have been running this (a similar) script in English as part of a new user logon on our systems for two years without issue. now I am building in lang options during the deployment phase and ran into the same issue you have with the verb language. I didn't my original script get it from your site (got it from scripting guys too), but you site is the only one I found that explicitly mentions the lang limitation so I thought I'd share how I got around it. the verb 'position' or 'number' should be pretty consistent for each shortcut. I did a loop count in the verb cycle and when it got to 4 or 5 or 6 depending on the shortcut, I execute. tested on three differ langs with diff KB layouts and even different alphabets and tis working. since this only runs once, and the shortcuts themselves are the same on each system, I figured the verb 'order' would be the same
     
      Wayne YeSunday, September 8, 2013 2:09 AM
    @dg Thanks for the updated information:)
     

    Your view point or opinion?
    Nickname *
     
    Gravatar *
    Required (not shown), used only for displaying Gravatar and receiving future notification when new comment(s) posted on this blog.
     
    Website/Blog
     
    Content *
    Current length:     Maximum allowed: charactors