- 1 year ago
Electron Builder NSIS Create a shortcut in startup
To create a shortcut in the startup folder using Electron Builder with NSIS, you can modify your Electron app's package.json
and NSIS script. Here's how you can achieve that:
Modify package.json:
Open yourpackage.json
file and add an"nsis"
configuration section to customize the NSIS installer behavior. Inside the"nsis"
section, you can use the"runAfterFinish"
option to specify a script to run after the installation finishes. In this script, you can use Node.js to create a shortcut in the startup folder.json{
"name": "your-electron-app",
"version": "1.0.0",
// ... other configurations ...
"build": {
// ... other build configurations ...
"nsis": {
"runAfterFinish": "scripts/createShortcut.js"
}
}
}
Create Shortcut Script:
Create a new file namedcreateShortcut.js
in a subdirectory namedscripts
(you need to create this directory). In this script, you can use Node.js'sfs
module to create a shortcut in the startup folder.javascriptconst fs = require('fs');
const os = require('os');
const path = require('path');
const startupFolder = path.join(os.homedir(), 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup');
const shortcutName = 'Your App Name.lnk';
const appPath = path.join(process.env.APPDIR, 'YourApp.exe'); // Replace with your actual app's exe path
const shortcutPath = path.join(startupFolder, shortcutName);
// Create a shortcut
const shortcutContent = `[InternetShortcut]
URL=file://${appPath}
IconIndex=0
IconFile=${appPath}
`;
fs.writeFileSync(shortcutPath, shortcutContent);
Update NSIS Script:
If you want to customize the shortcut behavior further, you can modify the NSIS script. The NSIS script is generated automatically by Electron Builder. To customize it, you can create a file namedinstaller.nsh
in the.electron-builder
directory of your project (create it if it doesn't exist). Then, you can add your custom NSIS script code to this file.For example, you could add the following code to customize the shortcut's icon and description:
nsis!include "MUI.nsh"
!define MUI_ICON "path/to/your/icon.ico"
!define MUI_HEADERIMAGE_BITMAP "path/to/your/banner.bmp"
!define MUI_WELCOMEFINISHPAGE_BITMAP "path/to/your/background.bmp"
# Your custom NSIS script code
!insertmacro MUI_PAGE_WELCOME
!insertmacro MUI_PAGE_DIRECTORY
!insertmacro MUI_PAGE_INSTFILES
!insertmacro MUI_PAGE_FINISH
!insertmacro MUI_LANGUAGE "English"
# Other NSIS script code
Section "Shortcut" SEC01
SetOutPath "$SMPROGRAMS\$StartMenuFolder"
CreateDirectory "$SMPROGRAMS\$StartMenuFolder"
CreateShortCut "$SMPROGRAMS\$StartMenuFolder\Your Shortcut.lnk" "$INSTDIR\YourApp.exe" "" "" 0
SectionEnd
Remember to replace
"path/to/your/icon.ico"
and other placeholders with actual paths and values.Run Your Build:
With the modifications made to yourpackage.json
, script file, and optional NSIS script, you can now run your Electron app build process using Electron Builder. The script increateShortcut.js
will be executed after the installation is complete, creating the startup shortcut.
Remember that Electron Builder and NSIS may have updates, so ensure you're following the latest documentation and guidelines when configuring your setup process.