ubuntu 创建桌面快捷方式

make-shortcut.sh

#!/bin/bash

# 初始命令
COMMAND="pwsh /opt/bin/make-shortcut.ps1"

# 如果有参数,则追加参数
if [ "$#" -gt 0 ]; then
    COMMAND="$COMMAND $@"
fi

# 执行命令
eval "$COMMAND"

make-shortcut.ps1

# /opt/bin/make-shortcut.ps1

# 优先使用传入的路径参数
param(
    [string]$rawPath
)

if(-not ($rawPath -and (Test-Path $rawPath))){

    # 从剪贴板读取路径
    $rawPath = xclip -o -selection clipboard
}

# 处理 file:// URI 格式
if ($rawPath -like "file:///*") {
    # 去掉 file:// 并解码 URL 编码(例如空格 %20 → 空格)
    $targetPath = [uri]::UnescapeDataString($rawPath.Substring(7))
} else {
    $targetPath = $rawPath
}

# 确保路径是绝对路径
$targetPath = [System.IO.Path]::GetFullPath($targetPath) 
Write-Host  "目标路径: $targetPath"

# 检查路径是否存在
if (-not (Test-Path $targetPath)) {
    Write-Host "错误:剪贴板中的路径无效或不存在。" -ForegroundColor Red
    exit
}

# 图标 
$iconPath = 'application-x-executable-symbolic'
$iconExts = @('.png', '.svg') #  
foreach ($ext in $iconExts) {
    $iconFile = "$targetPath$ext"
    if (Test-Path $iconFile) {
        $iconPath = $iconFile
        break
    }
}

# 获取应用程序名称(去掉扩展名)
$appName = [System.IO.Path]::GetFileNameWithoutExtension($targetPath)

# 获取桌面路径
$desktopPath = [Environment]::GetFolderPath("Desktop")
$shortcutFile = Join-Path $desktopPath "$appName.desktop"

# 创建 .desktop 内容
$desktopEntry = @"
[Desktop Entry]
Name=$appName
Exec=`"$targetPath`"
Icon=$iconPath
Terminal=false
Type=Application
Categories=Utility;
"@

# 写入文件
Set-Content -Path $shortcutFile -Value $desktopEntry

# 添加可执行权限
chmod +x $shortcutFile

# 可选地设置为受信任的应用程序, 有时有效,有时无效
#  gio info pycharm.desktop  | grep trusted # 查看是否受信任
Start-Process -Wait gio -ArgumentList "set `"$shortcutFile`" `"metadata::trusted`" yes"

Write-Host "快捷方式已创建于桌面: $shortcutFile" -ForegroundColor Green
上一篇
下一篇