AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1649349
Accepted
bgoodr
bgoodr
Asked: 2021-05-16 11:41:49 +0800 CST2021-05-16 11:41:49 +0800 CST 2021-05-16 11:41:49 +0800 CST

如何仅使用脚本创建桌面快捷方式并放置在桌面上的特定位置

  • 772

我知道如何使用 Windows Script Host (链接) 创建快捷方式。但我需要的是创建快捷方式并放置在特定位置,而无需手动使用鼠标来放置它们。这可以通过脚本(例如,PowerShell 等)实现吗?

原因是我喜欢编写 Windows 设置脚本,以便设置脚本可以受到更改控制(例如,Git)。

windows-10 windows
  • 1 1 个回答
  • 597 Views

1 个回答

  • Voted
  1. Best Answer
    Ben N
    2021-05-17T12:47:49+08:002021-05-17T12:47:49+08:00

    这是相当复杂的,因为使用像桌面这样的资源管理器视图需要使用几个不支持脚本的 COM 对象,但可以在 PowerShell 中通过在某些嵌入式 C# 中声明本机函数和结构的 .NET 表示来完成。基于这篇 Raymond Chen 的文章,我写了这个脚本:

    Param(
        [Parameter(Mandatory)][string]$ShortcutTitle,
        [Parameter(Mandatory)][int]$IconX,
        [Parameter(Mandatory)][int]$IconY,
        [Parameter(Mandatory)][string]$TargetPath,
        [Parameter()][string]$Arguments = '',
        [Parameter()][string]$WorkingDirectory = ''
    )
    
    # Declarations of COM interfaces, functions, and structures for interfacing with the shell
    Add-Type -TypeDefinition @"
    using System;
    using System.Runtime.InteropServices;
    
    [StructLayout(LayoutKind.Sequential)]
    public struct POINT {
        public int x;
        public int y;
    }
    
    [StructLayout(LayoutKind.Sequential)]
    public struct STRRET {
        int uType;
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = 260)]
        byte[] cStr;
    }
    
    public class PInvoke {
        [DllImport("Shlwapi.dll")]
        public static extern int StrRetToStrW(ref STRRET pstr, IntPtr pidl, [MarshalAs(UnmanagedType.LPWStr)] ref string ppsz);
    }
    
    [ComImport]
    [Guid("6d5140c1-7436-11ce-8034-00aa006009fa")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface ComServiceProvider {
        [return: MarshalAs(UnmanagedType.IUnknown)]
        Object QueryService(ref Guid guidService, ref Guid riid);
    }
    
    [ComImport]
    [Guid("000214e2-0000-0000-c000-000000000046")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IShellBrowser {
        IntPtr GetWindow();
        void UnusedContextSensitiveHelp();
        void UnusedInsertMenusSB();
        void UnusedSetMenuSB();
        void UnusedRemoveMenusSB();
        void UnusedSetStatusTextSB();
        void UnusedEnableModelessSB();
        void UnusedTranslateAcceleratorSB();
        void UnusedBrowseObject();
        void UnusedGetViewStateStream();
        void UnusedGetControlWindow();
        void UnusedSendControlMsg();
        [return: MarshalAs(UnmanagedType.IUnknown)]
        Object QueryActiveShellView();
        void UnusedOnViewWindowActive();
        void UnusedSetToolbarItems();
    }
    
    [ComImport]
    [Guid("1af3a467-214f-4298-908e-06b03e0b39f9")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IFolderView2 {
        void UnusedGetCurrentViewMode();
        void UnusedSetCurrentViewMode();
        [return: MarshalAs(UnmanagedType.IUnknown)]
        Object GetFolder(ref Guid riid);
        IntPtr Item(int iItemIndex);
        int ItemCount(int uFlags);
        void UnusedItems();
        void UnusedGetSelectionMarkedItem();
        void UnusedGetFocusedItem();
        POINT GetItemPosition(IntPtr pidl);
        void UnusedGetSpacing();
        void UnusedGetDefaultSpacing();
        void UnusedGetAutoArrange();
        void UnusedSelectItem();
        void SelectAndPositionItems(int cidl, [MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, [MarshalAs(UnmanagedType.LPArray)] POINT[] apt, int dwFlags);
        void UnusedSetGroupBy();
        void UnusedGetGroupBy();
        void UnusedSetViewProperty();
        void UnusedGetViewProperty();
        void UnusedSetTileViewProperties();
        void UnusedSetExtendedTileViewProperties();
        void UnusedSetText();
        void SetCurrentFolderFlags(int dwMask, int dwFlags);
        int GetCurrentFolderFlags();
        void UnusedGetSortColumnCount();
        void UnusedSetSortColumns();
        void UnusedGetSortColumns();
        void UnusedGetItem();
        void UnusedGetVisibleItem();
        void UnusedGetSelectedItem();
        void UnusedGetSelection();
        void UnusedGetSelectionState();
        void UnusedInvokeVerbOnSelection();
        void UnusedSetViewModeAndIconSize();
        void UnusedGetViewModeAndIconSize();
        void UnusedSetGroupSubsetCount();
        void UnusedGetGroupSubsetCount();
        void UnusedSetRedraw();
        void UnusedIsMoveInSameFolder();
        void UnusedDoRename();
    }
    
    [ComImport]
    [Guid("000214e6-0000-0000-c000-000000000046")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IShellFolder {
        void UnusedParseDisplayName();
        void UnusedEnumObjects();
        void UnusedBindToObject();
        void UnusedBindToStorage();
        void UnusedCompareIDs();
        void UnusedCreateViewObject();
        void UnusedGetAttributesOf();
        void UnusedGetUIObjectOf();
        STRRET GetDisplayNameOf(IntPtr pidl, int uFlags);
        void UnusedSetNameOf();
    }
    "@
    
    # Find the desktop window
    $shellWindowsType = [type]::GetTypeFromCLSID('9ba05972-f6a8-11cf-a442-00a0c90a8f39')
    $shellWindows = [activator]::CreateInstance($shellWindowsType)
    $csidlDesktop = 0
    $empty = $null
    $desktopWindow = $shellWindows.FindWindowSW([ref]$csidlDesktop, [ref]$empty, 8, 0, 1)
    $shellBrowser = [ComServiceProvider].GetMethod('QueryService').Invoke($desktopWindow, @([guid]'4c96be40-915c-11cf-99d3-00aa004ae837', [guid]'000214e2-0000-0000-c000-000000000046'))
    $folderView = [IShellBrowser].GetMethod('QueryActiveShellView').Invoke($shellBrowser, @())
    # Uncheck "auto arrange icons" so icons can be moved
    [IFolderView2].GetMethod('SetCurrentFolderFlags').Invoke($folderView, @(1, 0))
    # Note how many icons were displayed before so we can know when the new one appeared
    $iconCount = [IFolderView2].GetMethod('ItemCount').Invoke($folderView, @(0))
    
    # Determine the path for the new shortcut
    $wshShell = New-Object -ComObject WScript.Shell
    $desktopPath = $wshShell.SpecialFolders('Desktop')
    $lnkPath = "$desktopPath\$ShortcutTitle.lnk"
    If (-not (Test-Path $lnkPath)) {
        # If it doesn't already exist, create it with the WScript API
        $shortcut = $wshShell.CreateShortcut($lnkPath)
        $shortcut.TargetPath = $TargetPath
        $shortcut.Arguments = $Arguments
        $shortcut.WorkingDirectory = $WorkingDirectory
        $shortcut.Save()
        # Refresh the desktop and wait for Explorer to add an icon for the new shortcut
        $desktopWindow.Refresh()
        $newIconCount = $iconCount + 1
        $tries = 0
        While ($iconCount -lt $newIconCount -and $tries -lt 30) {
            $iconCount = [IFolderView2].GetMethod('ItemCount').Invoke($folderView, @(0))
            $tries += 1
            Start-Sleep -Milliseconds 100
        }
    }
    
    # Get a representation of the folder that allows reading item names
    $shellFolder = [IFolderView2].GetMethod('GetFolder').Invoke($folderView, @([guid]'000214e6-0000-0000-c000-000000000046'))
    # Iterate over the icons to find the new one
    0..($iconCount - 1) | % {
        # Get the ID and name of this icon
        $pidl = [IFolderView2].GetMethod('Item').Invoke($folderView, @([int]$_))
        $strret = [IShellFolder].GetMethod('GetDisplayNameOf').Invoke($shellFolder, @($pidl, 0x1000))
        $name = ''
        If ([PInvoke]::StrRetToStrW([ref]$strret, $pidl, [ref]$name) -eq 0) {
            If ($name -eq $ShortcutTitle) {
                # If it's the one we made, set its position
                $point = [POINT]::new()
                $point.x = $IconX
                $point.y = $IconY
                [IFolderView2].GetMethod('SelectAndPositionItems').Invoke($folderView, @(1, [IntPtr[]]@($pidl), [POINT[]]@($point), 128))
                # No need to look at the remaining icons - exit the script
                Break
            }
        }
    }
    

    WScript.Shell如果快捷方式不存在,它会使用经典脚本组件来创建快捷方式,然后使用 shell COM API 来查找和重新定位快捷方式。

    例如,如果那个大脚本被保存为positionedshortcut.ps1,下面的命令会在 (1000, 400) 附近创建一个名为“Friendly Shell”的有点傻的快捷方式,它会启动一个带有问候语的命令提示符。如果已经存在具有该名称的快捷方式,它将被移回该位置,但不会更改。

    powershell -ExecutionPolicy Bypass -c .\positionedshortcut.ps1 -ShortcutTitle 'Friendly Shell' -IconX 1000 -IconY 400 -TargetPath 'C:\Windows\System32\cmd.exe' -Arguments '/k echo Hi there!'
    

    要设置其他快捷方式属性,请像平常一样操作$shortcut对象。新坐标以$point像素为单位;如果您需要适应不同的屏幕尺寸,您也可以从脚本中获取屏幕分辨率。

    • 1

相关问题

  • 如何在域和 Linux 活动目录中启用指纹传感器

  • 资源管理器侧面板中的桌面外壳快捷方式

  • 为什么我不能将文件从 Android 发送到 Windows 10?

  • 在多个文件上打开方式?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve