Creates a Microsoft Windows Shortcut (.lnk file).

# S3 method for default
createWindowsShortcut(pathname, target, overwrite=FALSE, mustWork=FALSE, ...)

Arguments

pathname

The pathname (with file extension *.lnk) of the link file to be created.

target

The target file or directory to which the shortcut should point to.

overwrite

If TRUE, an existing link file is overwritten, otherwise not.

mustWork

If TRUE, an error is produced if the Windows Shortcut link is not created, otherwise not.

...

Not used.

Value

Returns (invisibly) the pathname.

Required privileges on Windows

In order for this method, which utilizes Windows Script Host a VBScript, to succeed on Windows, the client/R session must run with sufficient privileges (it has been reported that Administrative rights are necessary).

Author

Henrik Bengtsson

See also

References

[1] Create a windows shortcut (.LNK file), SS64.com, https://ss64.com/nt/shortcut.html

Examples

# Create Windows Shortcut links to a directory and a file
targets <- list(
  system.file(package="R.utils"),
  system.file("DESCRIPTION", package="R.utils")
)

for (kk in seq_along(targets)) {
  cat("Link #", kk, "\n", sep="")

  target <- targets[[kk]]
  cat("Target: ", target, "\n", sep="")

  # Name of *.lnk file
  pathname <- sprintf("%s.LNK", tempfile())

  tryCatch({
    # Will only work on Windows systems with support for VB scripting
    createWindowsShortcut(pathname, target=target)
  }, error = function(ex) {
    print(ex)
  })

  # Was it created?
  if (isFile(pathname)) {
    cat("Created link file: ", pathname, "\n", sep="")

    # Validate that it points to the correct target
    dest <- filePath(pathname, expandLinks="any")
    cat("Available target: ", dest, "\n", sep="")

    res <- all.equal(tolower(dest), tolower(target))
    if (!isTRUE(res)) {
      msg <- sprintf("Link target does not match expected target: %s != %s", dest, target)
      cat(msg, "\n")
      warning(msg)
    }

    # Cleanup
    file.remove(pathname)
  }
}
#> Link #1
#> Target: /tmp/henrik/RtmpsaLUi7/temp_libpath230bf3b96e771/R.utils
#> [2023-11-17 17:03:36.787394] Exception: An error occurred when calling VBScript (‘cscript "/tmp/henrik/RtmpAr1qq8/file2313928d22ebd.vbs"’) to create Windows Shortcut link ‘/tmp/henrik/RtmpAr1qq8/file23139530bffed.LNK’. The reason was: could not find function "shell"
#> 
#>   at #47. createWindowsShortcutViaVBScript(pathname, target = target, mustWork = mustWork)
#>           - createWindowsShortcutViaVBScript() is local of the calling function
#> 
#>   at #46. createWindowsShortcut.default(pathname, target = target)
#>           - createWindowsShortcut.default() is in environment 'R.utils'
#> 
#>   at #45. createWindowsShortcut(pathname, target = target)
#>           - createWindowsShortcut() is in environment 'R.utils'
#>           - originating from '<text>'
#> 
#> Link #2
#> Target: /tmp/henrik/RtmpsaLUi7/temp_libpath230bf3b96e771/R.utils/DESCRIPTION
#> [2023-11-17 17:03:36.810313] Exception: An error occurred when calling VBScript (‘cscript "/tmp/henrik/RtmpAr1qq8/file2313942ff81dd.vbs"’) to create Windows Shortcut link ‘/tmp/henrik/RtmpAr1qq8/file2313974d6499f.LNK’. The reason was: could not find function "shell"
#> 
#>   at #47. createWindowsShortcutViaVBScript(pathname, target = target, mustWork = mustWork)
#>           - createWindowsShortcutViaVBScript() is local of the calling function
#> 
#>   at #46. createWindowsShortcut.default(pathname, target = target)
#>           - createWindowsShortcut.default() is in environment 'R.utils'
#> 
#>   at #45. createWindowsShortcut(pathname, target = target)
#>           - createWindowsShortcut() is in environment 'R.utils'
#>           - originating from '<text>'
#>