1.11.0 • Published 4 months ago

create-desktop-shortcuts v1.11.0

Weekly downloads
91
License
MIT
Repository
github
Last release
4 months ago

create-desktop-shortcuts

Build Status Unit Test Coverage: 100% End-to-End Windows Passing End-to-End Linux Passing End-to-End OSX Passing Lint Coverage: 100% Compatible with Node 4+ Code of Conduct: No Ideologies MIT Licensed

Small, lightweight, cross-platform, built in validation!

100% Test Coverage, Automated Cross-Platform End-to-End tested

An easy, cross-platform, API to create desktop shortcuts with Node. (Works in NW.js too!)

This library is completely synchronous.

It uses 1 dependency, which has it's own single dependency

  • create-desktop-shortcuts - small - 100% test coverage
    • which - small - 100% test coverage - used by 9 million projects
      • isexe - very small - used by 9 million projects

Installation

npm install --save create-desktop-shortcuts

Examples

Simple example:

const createDesktopShortcut = require('create-desktop-shortcuts');

const shortcutsCreated = createDesktopShortcut({
  windows: { filePath: 'C:\\path\\to\\executable.exe' },
  linux:   { filePath: '/home/path/to/executable'     },
  osx:     { filePath: '/home/path/to/executable'     }
});

if (shortcutsCreated) {
  console.log('Everything worked correctly!');
} else {
  console.log('Could not create the icon or set its permissions (in Linux if "chmod" is set to true, or not set)');
}

Advanced Example:

Each OS handles the concept of a shortcut icon slightly differently. So they each have a slightly different API, but I tried to keep them similar when they overlap.

const createDesktopShortcut = require('create-desktop-shortcuts');

const shortcutsCreated = createDesktopShortcut({
  // OPTIONAL: defaults to true
  onlyCurrentOS: true,
  // OPTIONAL: defaults to true
  verbose: true,
  /**
   * OPTIONAL: console.error is called by default if verbose: true.
   *
   * Your own custom logging function called with helpful warning/error
   * messages from the internal validators. Only used if verbose: true.
   *
   * @param {string} message  The human readable warning/error message
   * @param {object} error    Sometimes an error or options object is passed
   */
  customLogger: function (message, error) {
    console.log(message, error);
  },
  windows: {
    // REQUIRED: Path must exist
    filePath: 'C:\\path\\to\\executable.exe',
    // OPTIONAL: Defaults to the Desktop of the current user (by asking Windows specifically where it is located)
    outputPath: 'C:\\some\\folder',
    // OPTIONAL: defaults to the filePath file's name (without the extension)
    name: 'My App Name',
    // OPTIONAL
    comment: 'My App description',
    // OPTIONAL: File must exist and be ICO, EXE, or DLL
    icon: 'C:\\path\\to\\file.ico',
    // OPTIONAL
    arguments: '--my-argument -f \'other stuff\'',
    // OPTIONAL: defaults to 'normal'
    windowMode: 'normal',
    // OPTIONAL
    hotkey: 'ALT+CTRL+F'
  },
  linux: {
    // REQUIRED: Path must exist
    filePath: '/home/path/to/executable',
    // OPTIONAL: Defaults to the Desktop of the current user
    outputPath: '/home/some/folder',
    // OPTIONAL: defaults to the filePath file's name (without the extension)
    name: 'My App Name',
    // OPTIONAL
    description: 'My app description',
    // OPTIONAL: File must exist and be PNG or ICNS
    icon: '/home/path/to/file.png',
    // OPTIONAL: 'Application', 'Directory', or 'Link' (must logically match filePath)
    type: 'Application',
    // OPTIONAL: defaults to false
    terminal: false,
    // OPTIONAL: defaults to true
    chmod: true,
    // OPTIONAL
    arguments: '--my-argument -f'
  },
  osx: {
    // REQUIRED: Path must exist
    filePath: '/Applications/My App.app',
    // OPTIONAL: Defaults to the Desktop of the current user
    outputPath: '/home/some/folder',
    // OPTIONAL: defaults to the filePath file's name (without the extension)
    name: 'My App Name',
    // OPTIONAL: defaults to false
    overwrite: false
  }
});

// returns true if everything worked correctly, or false if it could not create the icon or set its permissions
console.log(shortcutsCreated);

Documentation

Global Settings

KeyTypeAllowedDefaultDescription
onlyCurrentOSBooleantrue, falsetrueIf true and you pass in objects for multiple OS's, this will only create a shortcut for the OS it was ran on.
verboseBooleantrue, falsetrueIf true, consoles out helpful warnings and errors using customLogger or console.error.
customLoggerFunctionAny functionconsole.errorYou can pass in your own custom function to log errors/warnings to. When called the function will receive a message string for the first argument and sometimes an error object for the second argument. This is useful in NW.js to see the messages logged to the regular Chromium Developer Tools instead of the background page's developer tools. But this can also be useful in other scenarios, like adding in custom wrappers or colors in a command line/terminal. This function may be called multiple times before all synchronous tasks complete.

Windows Settings

KeyTypeAllowedDefaultDescription
filePathStringAny valid path or URLThis is a required fieldThis is the target the shortcut points to.
outputPathStringAny valid path to a folderSee description.Path where the shortcut will be placed. The default value is determined by using PowerShell to ask Windows specifically where the User's desktop is located. If PowerShell is not available we default to '%USERPROFILE%\\Desktop'. This means it supports One Drive accounts, and other oddball setups, too.
nameStringAny file system safe stringUses name from filePathThe name of the shortcut file.
commentStringAny stringNot used if not suppliedMetadata file "comment" property. Description of what the shortcut would open.
iconStringValid path to file (ICO, EXE, or DLL)Uses OS default iconThe image shown on the shortcut icon. You can also pass in an index if file contains multiple icons, like 'C:\\file.exe,0'
argumentsStringAny stringNoneAdditional arguments passed in to the end of your target filePath
windowModeString'normal', 'maximized', 'minimized''normal'How the window should be displayed by default
hotkeyStringAny stringNoneA global hotkey to associate to opening this shortcut, like 'CTRL+ALT+F'
workingDirectoryStringAny valid path to a folderNoneThe working directory for the shortcut when it launches
VBScriptPathStringPath to external VBS fileNoneThis is an advanced option specifically for projects packaged with pkg. You can create a copy of the windows.vbs file outside of your packaged executable and pass in the location of it. If your vbs file differs in any way to the version shipped with this library, it will have bugs. Ensure you are programmatically copying the shipped version, so if it changes in a future update, your code will still work. If you are not using pkg, you should not include VBScriptPath in your Windows settings.

Linux Settings

KeyTypeAllowedDefaultDescription
filePathStringAny valid path or URLThis is a required fieldThis is the target the shortcut points to. Must be a valid/existing folder if type: 'Directory', or file if type: 'Application'.
outputPathStringAny valid path to a folderCurrent user's desktopPath where the shortcut will be placed.
nameStringAny file system safe stringUses name from filePathThe name of the shortcut file.
commentStringAny stringNot used if not suppliedMetadata file "comment" property. Description of what the shortcut would open.
iconStringValid path to PNG or ICNS fileUses OS default iconThe image shown on the shortcut icon. Preferably a 256x256 PNG.
typeString'Application', 'Link', 'Directory'Based on filePathType of shortcut. Defaults to 'Link' if filePath starts with 'http://' or 'https://'. Defaults to 'Directory' if filePath exists and is a folder. Defaults to Application otherwise.
terminalBooleantrue, falsefalseIf true, will run in a terminal.
chmodBooleantrue, falsetrueIf true, will apply a chmod +x (755) to the shortcut after creation to allow execution permission.
argumentsStringAny stringNoneAdditional arguments passed in to the end of your target filePath

OSX Settings

OSX will automatically inherit the icon of the target you point to. It doesn't care if you point to a folder, file, or application.

NOTE: If overwrite is set to false and a matching file already exists, a console.error will occur to inform you of this, however create-desktop-shortcuts will still report successful (return true). This console.error can be hidden by setting verbose to false, or using a customLogger to intercept it.

KeyTypeAllowedDefaultDescription
filePathStringAny valid path or URLThis is a required fieldThis is the target the shortcut points to.
outputPathStringAny valid path to a folderCurrent user's desktopPath where the shortcut will be placed.
nameStringAny file system safe stringUses name from filePathThe name of the shortcut file.
overwriteBooleantrue, falsefalseIf true, will replace any existing file in the outputPath with matching name. See above note for more details.

Special paths

The filePath for all OS's can be set to an executable name directly if it is available in the user's PATH. For example, if you set it to 'node' or 'python' or 'cmd' and the user can access those from PATH variables, it will work automatically. But the file must exist and must be an actual executable, otherwise you should hard code the value.

The filePath and outputPath can use environment variables on Windows, like '%appData%'. See the "Add to start menu" section below for more examples.

The filePath and outputPath can start with ~ or / on Linux or OSX and the paths will be resolved automatically.

Add to start menu

Here are the documented paths you can use for the outputPath to create a shortcut in the start menu.

OSTypeoutputPathNote
Win 7+All Users%AllUsersProfile%\Microsoft\Windows\Start Menu\ProgramsC:\ProgramData\Microsoft\Windows\Start Menu\Programs
Win 7+All Users%ProgramData%\Microsoft\Windows\Start Menu\ProgramsShould be exactly same as above
Win 7+Current User%AppData%\Microsoft\Windows\Start Menu\ProgramsShould work fine\reliably
LinuxAll Users/usr/share/applicationsAccording to Arch and Gnome
LinuxAll Users/usr/local/share/applications/According to Arch
LinuxCurrent User~/.local/share/applicationsAccording to Gnome docs

OSX doesn't have a "Start menu" nor an official means of adding items to the dock.

In Windows 8+ it might be possible to pin a "tile" in the start menu, but that looks difficult, and out of scope for this library. If someone else created a Node library for pinning tiles to the start menu this line of the README should be updated to link to it.


OS Support

End-to-end tests run on every commit using:

  • Ubuntu latest with Node v8.3.0
  • Ubuntu latest with Node v18.x.x
  • Windows latest with Node v8.3.0
  • Windows latest with Node v18.x.x
  • OSX latest with Node v8.3.0
  • OSX latest with Node v18.x.x

Manually Tested on:

  • Windows XP with Node v4.4.5
  • Windows 7 with Node v4.0.0, v8, v10, v12, v13.13.0 (last supported Node version for Win 7)
  • Windows 10 with Node v12, v14, v15
  • Ubuntu 16, 20 with Node v12
  • OSX 10.11 with Node v7.3.0
  • NW.js v0.14.7 (LTS), v0.30.0, v0.50.0

Credits

Author: The Jared Wilcurt

Parts of the windows.vbs were copied/modified based on:


How can you help improve this repo?

  • Report bugs in the GitHub issues
  • Request features
  • Fix reported bugs with a PR
  • Offer an async and sync mode, instead of just sync.
    • Note: Make sure it can still run in older versions of NW.js
  • Help with any of the known issues listed below
  • This repo is written using require for imports. I would endorse anyone that wants to create a fork of this repo written in ESM that drops support for older Node versions and is written with async support (maybe promises?). Maybe call it create-desktop-shortcuts-esm or something.

Running locally for development

  1. Install Volta (Runs the Node/npm version based on the repo's package.json automatically)
  2. Fork/Clone the repo with git
  3. npm install
  4. You can run npm t to run all unit tests
  5. You can edit the manual-testing.js file to try things out locally with npm run manual-test
  6. npm run lint to see linting errors, npm run fix to attempt to fix them automatically
  7. npm run debug to run Node's debugger tool with your manual test
  8. npm run debug-test to run the debugger against a specific test

Known issues & limitations

  1. Windows: If WScript does not like what is passed to it, it displays a Windows Dialog with an error on the line that failed. I have no idea how to turn that off, so I've just added in tons of validation checks to prevent anything from being passed in to it that could cause this. But may still occur if you pass in junk that gets by the validation checks.
  2. Linux: No real recourse if the script does not have permission to run chmod on Linux. You would just need to run it again with sudo or something. If you have ideas, create an issue or PR.
  3. OSX: I know of no way to set a custom icon image on OSX. It will just always use the same icon the executable had (or file type if linking to an .html file for example)
  4. Windows/Linux: May want to add in overwrite option for Windows and Linux too. This would require deleting the existing shortcut. Deleting files is something that each OS sucks at in different ways and would require pulling in something like fs-extra or a similar dependency. If you need this, you can reference this implementation code example.
  5. OSX: To my knowledge there is no way to pass in arguments with a shortcut. If anyone knows how, make an issue with details.

Related Libraries

1.11.0

4 months ago

1.9.0

2 years ago

1.8.0

2 years ago

1.10.1

2 years ago

1.10.0

2 years ago

1.7.0

2 years ago

1.6.1

2 years ago

1.6.0

2 years ago

1.5.0

3 years ago

1.4.0

3 years ago

1.3.0

3 years ago

1.2.1

3 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago