Execute createShortcut in a Squirrel event, in an Electron app

2.8k views Asked by At

I'm trying to create shortcuts for my Electron app when I install it or update it, however I am having some trouble executing the command that is meant to create the shortcut. By default Electron apps are "SquirrelAware", therefore I have to specify where i would like to create shortcuts.

My question is in relation to the accepted answer to this question.

Handle Squirrel's event on an Electron app

I have tried to use the exec module and the child_process module, however both did not seem to work. I am now currently attempting (and failing) to launch PowerShell and run a script in there that will create shortcuts on my Start Menu and Desktop, however I feel this is rather long and that there must be an easier way.

Here is my current attempt using the child_process module and PowerShell:

 var spawn = require('child_process').spawn, child;
 child = spawn("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe",["C:\\ElectronSquirrelDemo\\AddAppShortcuts.ps1 -SourceExe "+ executionPath] );
                child.stdout.on('data', function(data){
                    console.log("PowerShell Data: " + data);
                });
                child.stdout.on('data', function(data){
                    console.log("PowerShell Error: " + data);
                });
                child.stdout.on('exit', function(){
                   console.log('PowerShell script finished'); 
                });

Any help on this would be much appreciated

1

There are 1 answers

4
Shawn Rakowski On BEST ANSWER

It took me awhile to understand how to do this myself. The Squirrel.Windows Update.exe has the ability to create shortcuts to your app for you. I wrote a blog post called Creating a Windows Distribution of an Electron App using Squirrel and in it I have Squirrel create the shortcuts for me. If you want to go this route, this is simplified version of how to have Squirrel create the shortcuts for you:

var cp = require('child_process');    
var updateDotExe = path.resolve(path.dirname(process.execPath), '..', 'update.exe');
var target = path.basename(process.execPath);
var child = cp.spawn(updateDotExe, ["--createShortcut", target], { detached: true });
child.on('close', function(code) {
    app.quit();
});

You need to hack the electron executable using Resource Hacker, rcedit, or another application to change the ProductName and Icon resources. You'll want to call the above code on both the install and updated Squirrel events.