Script/Command for copying Shortcut (.lnk) targets - Including Arguments

This is a VBScript for copying the target string used in a shortcut / .link file and including any arguments to the clipboard.

This can be used either as a User Command or placed directly as a button. (I personally have it added to the context menu for .LNK files via the File Types menu.

How to add it just to .lnk file context menu:


(I have it as a User Command called "Copy_Shortcut_Target_With_Arguments" but the name doesn't matter)

The Script:

'Version: 1.0.1 - 7/1/24
Option Explicit

Function OnClick(ByRef clickData)
    Dim WShell, fs, selectedItems, item, path, fExt, oSHLink, targetPath, arguments, resolvedPaths, itemIndex
    Set WShell = CreateObject("WScript.Shell")
    Set fs = CreateObject("Scripting.FileSystemObject")
    resolvedPaths = ""

    Set selectedItems = clickData.func.sourcetab.selected
    
    If selectedItems.count = 0 Then
        DOpus.Output "No files selected."
        Exit Function
    End If
    
    itemIndex = 0
    For Each item In selectedItems
        If itemIndex > 0 Then
			' Add newline character for each subsequent item added
            resolvedPaths = resolvedPaths & vbCrLf
        End If
        
        path = item.realpath
        fExt = fs.GetExtensionName(path)
        
        If UCase(fExt) = "LNK" Then
            Set oSHLink = WShell.CreateShortcut(path)
            targetPath = oSHLink.TargetPath
            arguments = oSHLink.Arguments
            resolvedPaths = resolvedPaths & targetPath & " " & arguments
        Else
            resolvedPaths = resolvedPaths & path
        End If
        
        itemIndex = itemIndex + 1
    Next

    ' Copy the resolved paths to the clipboard
    DOpus.SetClip resolvedPaths
    
    Set oSHLink = Nothing
    Set fs = Nothing
    Set WShell = Nothing
End Function

Additional Notes:

  • If the script is called while a file/folder other than a shortcut is selected, it will just copy the regular path to that.
  • It will work with multiple .LNK files selected - it will add each result to its own line on the clipboard.

Changes:

  • 1.0.1 - Fixed it so there's not an extra newline at the end of the list on the clipboard
7 Likes

Very nice! I already added the open target in new tab item.
Certainly putting this beside it.

Thanks for sharing!

1 Like