Skip to content

Commit 83b8238

Browse files
committed
Improve Shortcut creation
* Solves #1319, #1320, #1378, #1757, #1846, #1867 * Add WorkingDirectory * Add Icon * Add Arguments * Use FileInfo and DirectoryInfo
1 parent b223657 commit 83b8238

File tree

1 file changed

+29
-20
lines changed

1 file changed

+29
-20
lines changed

lib/shortcuts.ps1

+29-20
Original file line numberDiff line numberDiff line change
@@ -2,47 +2,56 @@
22
function create_startmenu_shortcuts($manifest, $dir, $global, $arch) {
33
$shortcuts = @(arch_specific 'shortcuts' $manifest $arch)
44
$shortcuts | ?{ $_ -ne $null } | % {
5-
$target = $_.item(0)
5+
$target = [System.IO.Path]::Combine($dir, $_.item(0))
6+
$target = New-Object System.IO.FileInfo($target)
67
$name = $_.item(1)
7-
try {
8+
$arguments = ""
9+
$icon = $null
10+
if($_.length -ge 3) {
811
$arguments = $_.item(2)
9-
} catch {
10-
$arguments = ""
1112
}
12-
startmenu_shortcut "$dir\$target" $name $global $arguments
13+
if($_.length -ge 4) {
14+
$icon = [System.IO.Path]::Combine($dir, $_.item(3))
15+
$icon = New-Object System.IO.FileInfo($icon)
16+
}
17+
startmenu_shortcut $target $name $arguments $icon $global
1318
}
1419
}
1520

1621
function shortcut_folder($global) {
22+
$directory = [System.IO.Path]::Combine([Environment]::GetFolderPath('startmenu'), 'Programs', 'Scoop Apps')
1723
if($global) {
18-
"$([environment]::getfolderpath('commonstartmenu'))\Programs\Scoop Apps"
19-
return
24+
$directory = [System.IO.Path]::Combine([Environment]::GetFolderPath('commonstartmenu'), 'Programs', 'Scoop Apps')
2025
}
21-
"$([environment]::getfolderpath('startmenu'))\Programs\Scoop Apps"
26+
return $(ensure $directory)
2227
}
2328

24-
function startmenu_shortcut($target, $shortcutName, $global, $arguments) {
25-
if(!(Test-Path $target)) {
29+
function startmenu_shortcut([System.IO.FileInfo] $target, $shortcutName, $arguments, [System.IO.FileInfo]$icon, $global) {
30+
if(!$target.Exists) {
2631
Write-Host -f DarkRed "Creating shortcut for $shortcutName ($(fname $target)) failed: Couldn't find $target"
2732
return
2833
}
29-
$scoop_startmenu_folder = shortcut_folder $global
30-
if(!(Test-Path $scoop_startmenu_folder)) {
31-
New-Item $scoop_startmenu_folder -type Directory
34+
if($icon -and !$icon.Exists) {
35+
Write-Host -f DarkRed "Creating shortcut for $shortcutName ($(fname $target)) failed: Couldn't find icon $icon"
36+
return
3237
}
33-
$dirname = [System.IO.Path]::GetDirectoryName($shortcutName)
34-
if ($dirname) {
35-
$dirname = [io.path]::combine($scoop_startmenu_folder, $dirname)
36-
if(!(Test-Path $dirname)) {
37-
New-Item $dirname -type Directory
38-
}
38+
39+
$scoop_startmenu_folder = shortcut_folder $global
40+
$subdirectory = [System.IO.Path]::GetDirectoryName($shortcutName)
41+
if ($subdirectory) {
42+
$subdirectory = ensure $([System.IO.Path]::Combine($scoop_startmenu_folder, $subdirectory))
3943
}
44+
4045
$wsShell = New-Object -ComObject WScript.Shell
4146
$wsShell = $wsShell.CreateShortcut("$scoop_startmenu_folder\$shortcutName.lnk")
42-
$wsShell.TargetPath = "$target"
47+
$wsShell.TargetPath = $target.FullName
48+
$wsShell.WorkingDirectory = $target.DirectoryName
4349
if ($arguments) {
4450
$wsShell.Arguments = $arguments
4551
}
52+
if($icon -and $icon.Exists) {
53+
$wsShell.IconLocation = $icon.FullName
54+
}
4655
$wsShell.Save()
4756
write-host "Creating shortcut for $shortcutName ($(fname $target))"
4857
}

0 commit comments

Comments
 (0)