Skip to content

Commit

Permalink
Improve Shortcut creation
Browse files Browse the repository at this point in the history
* Solves #1319, #1320, #1378, #1757, #1846, #1867
* Add WorkingDirectory
* Add Icon
* Add Arguments
* Use FileInfo and DirectoryInfo
  • Loading branch information
r15ch13 committed Jan 8, 2018
1 parent b223657 commit 83b8238
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions lib/shortcuts.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
$scoop_startmenu_folder = shortcut_folder $global
if(!(Test-Path $scoop_startmenu_folder)) {
New-Item $scoop_startmenu_folder -type Directory
if($icon -and !$icon.Exists) {
Write-Host -f DarkRed "Creating shortcut for $shortcutName ($(fname $target)) failed: Couldn't find icon $icon"
return
}
$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
}

$scoop_startmenu_folder = shortcut_folder $global
$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))"
}
Expand Down

0 comments on commit 83b8238

Please sign in to comment.