From 83b823868f5ef5256d3dcfbecff278bb355fefc8 Mon Sep 17 00:00:00 2001 From: Richard Kuhnt Date: Mon, 8 Jan 2018 17:12:16 +0100 Subject: [PATCH] Improve Shortcut creation * Solves #1319, #1320, #1378, #1757, #1846, #1867 * Add WorkingDirectory * Add Icon * Add Arguments * Use FileInfo and DirectoryInfo --- lib/shortcuts.ps1 | 49 ++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/lib/shortcuts.ps1 b/lib/shortcuts.ps1 index df41c6928..544d45908 100644 --- a/lib/shortcuts.ps1 +++ b/lib/shortcuts.ps1 @@ -2,47 +2,56 @@ function create_startmenu_shortcuts($manifest, $dir, $global, $arch) { $shortcuts = @(arch_specific 'shortcuts' $manifest $arch) $shortcuts | ?{ $_ -ne $null } | % { - $target = $_.item(0) + $target = [System.IO.Path]::Combine($dir, $_.item(0)) + $target = New-Object System.IO.FileInfo($target) $name = $_.item(1) - try { + $arguments = "" + $icon = $null + if($_.length -ge 3) { $arguments = $_.item(2) - } catch { - $arguments = "" } - startmenu_shortcut "$dir\$target" $name $global $arguments + if($_.length -ge 4) { + $icon = [System.IO.Path]::Combine($dir, $_.item(3)) + $icon = New-Object System.IO.FileInfo($icon) + } + startmenu_shortcut $target $name $arguments $icon $global } } function shortcut_folder($global) { + $directory = [System.IO.Path]::Combine([Environment]::GetFolderPath('startmenu'), 'Programs', 'Scoop Apps') if($global) { - "$([environment]::getfolderpath('commonstartmenu'))\Programs\Scoop Apps" - return + $directory = [System.IO.Path]::Combine([Environment]::GetFolderPath('commonstartmenu'), 'Programs', 'Scoop Apps') } - "$([environment]::getfolderpath('startmenu'))\Programs\Scoop Apps" + return $(ensure $directory) } -function startmenu_shortcut($target, $shortcutName, $global, $arguments) { - if(!(Test-Path $target)) { +function startmenu_shortcut([System.IO.FileInfo] $target, $shortcutName, $arguments, [System.IO.FileInfo]$icon, $global) { + if(!$target.Exists) { Write-Host -f DarkRed "Creating shortcut for $shortcutName ($(fname $target)) failed: Couldn't find $target" return } + if($icon -and !$icon.Exists) { + Write-Host -f DarkRed "Creating shortcut for $shortcutName ($(fname $target)) failed: Couldn't find icon $icon" + return + } + $scoop_startmenu_folder = shortcut_folder $global - if(!(Test-Path $scoop_startmenu_folder)) { - New-Item $scoop_startmenu_folder -type Directory - } - $dirname = [System.IO.Path]::GetDirectoryName($shortcutName) - if ($dirname) { - $dirname = [io.path]::combine($scoop_startmenu_folder, $dirname) - if(!(Test-Path $dirname)) { - New-Item $dirname -type Directory - } + $subdirectory = [System.IO.Path]::GetDirectoryName($shortcutName) + if ($subdirectory) { + $subdirectory = ensure $([System.IO.Path]::Combine($scoop_startmenu_folder, $subdirectory)) } + $wsShell = New-Object -ComObject WScript.Shell $wsShell = $wsShell.CreateShortcut("$scoop_startmenu_folder\$shortcutName.lnk") - $wsShell.TargetPath = "$target" + $wsShell.TargetPath = $target.FullName + $wsShell.WorkingDirectory = $target.DirectoryName if ($arguments) { $wsShell.Arguments = $arguments } + if($icon -and $icon.Exists) { + $wsShell.IconLocation = $icon.FullName + } $wsShell.Save() write-host "Creating shortcut for $shortcutName ($(fname $target))" }