New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add shell.writeShortcutLink/readShortcutLink APIs #6623

Merged
merged 9 commits into from Jul 27, 2016

Conversation

Projects
None yet
6 participants
@zcbenz
Contributor

zcbenz commented Jul 27, 2016

### `shell.writeShortcutLink(shortcutPath[, operation], options)` _Windows_

* `shortcutPath` String
* `operation` String (optional) - Default is `create`, can be one of followings:
  * `create` - Creates a new shortcut, overwriting if necessary.
  * `update` - Updates specified properties only on an existing shortcut.
  * `replace` - Overwrites an existing shortcut, fails if the shortcut doesn't
    exist.
* `options` Object
  * `target` String - The target to launch from this shortcut.
  * `cwd` String (optional) - The target to launch from this shortcut. Default
    is empty.
  * `args` String (optional) - The arguments to be applied to `target` when
    launching from this shortcut. Default is empty.
  * `description` String (optional) - The description of the shortcut. Default
    is empty.
  * `icon` String (optional) - The path to the icon, can be a DLL or EXE. `icon`
    and `iconIndex` have to be set together. Default is empty, which uses the
    target's icon.
  * `iconIndex` Integer (optional) - The resource ID of icon when `icon` is a
    DLL or EXE. Default is 0.
  * `appUserModelId` String (optional) - The Application User Model ID. Default
    is empty.

Creates or updates a shortcut link at `shortcutPath`. On success `true` is
returned, otherwise `false` is returned.

### `shell.readShortcutLink(shortcutPath)` _Windows_

Resolves the shortcut link at `shortcutPath`, an object is returned with the
fields described in the `options` of `shell.writeShortcutLink`.

An exception will be thrown when any error happens.

Close #4414.

@zcbenz zcbenz merged commit bf20bbc into master Jul 27, 2016

8 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
electron-linux-arm Build #3711376 succeeded in 48s
Details
electron-linux-ia32 Build #3711377 succeeded in 44s
Details
electron-linux-x64 Build #3711378 succeeded in 78s
Details
electron-mas-x64 Build #2028 succeeded in 6 min 18 sec
Details
electron-osx-x64 Build #2024 succeeded in 7 min 2 sec
Details
electron-win-ia32 Build #1040 succeeded in 6 min 23 sec
Details
electron-win-x64 Build #1023 succeeded in 6 min 29 sec
Details

@zcbenz zcbenz deleted the shortcut branch Jul 27, 2016

@jorangreef

This comment has been minimized.

jorangreef commented Jul 28, 2016

Thanks @zcbenz.

@MarshallOfSound

This comment has been minimized.

Member

MarshallOfSound commented Aug 3, 2016

@KochiyaOcean That's what the docs say. Personally I just point do

{
  icon: process.execPath,
  iconIndex: 0
}

That makes the shortcut the same icon as your program

@KochiyaOcean

This comment has been minimized.

KochiyaOcean commented Aug 3, 2016

@MarshallOfSound thanks, it works. (I havn't set iconIndex as 0 before)

properties.set_arguments(str);
if (options.Get("description", &str))
properties.set_description(str);
if (options.Get("icon", &path) && options.Get("iconIndex", &index))

This comment has been minimized.

@MarshallOfSound

MarshallOfSound Aug 3, 2016

Member

@zcbenz The docs indicate that iconIndex has a default of 0 but this appears to not set the icon if iconIndex is not set.

Which behavior is expected / wanted and I will fix whichever end is wrong 😄

@paulcbetts

This comment has been minimized.

Contributor

paulcbetts commented Aug 3, 2016

Remember that Squirrel.Windows always writes its shortcut targets as trampolines - i.e. it's not a shortcut to MyApp.exe, it's a shortcut to Update.exe --processStart MyApp.exe - this means that even if an update fails to fix the shortcut, it still is clickable.

https://github.com/Squirrel/Squirrel.Windows/blob/cedf4c296c830fcea09ff4ffca7cc1083b4ede67/src/Squirrel/UpdateManager.ApplyReleases.cs#L228

@MarshallOfSound

This comment has been minimized.

Member

MarshallOfSound commented Aug 3, 2016

@paulcbetts Yeah, I just updated my shortcut code to be something like this

import { app, shell } from 'electron';
import fs from 'fs';
import path from 'path';

const packageJSON = require('./package.json');

const startPath = path.resolve(
  app.getPath('appData'), '..', 'Roaming', 'Microsoft', 'Windows', 'Start Menu', 'Programs',
  packageJSON.author.name, `${packageJSON.productName}.lnk`
);

const desktopPath = path.resolve(
  app.getPath('home'), 'Desktop', `${packageJSON.productName}.lnk`
);

const squirrelPath = path.resolve(path.dirname(process.execPath), '..', 'update.exe');

const shortcutAtPath = (targetPath) => {
  shell.writeShortcutLink(targetPath, fs.existsSync(targetPath) ? 'update' : 'create', {
    target: squirrelPath,
    args: `--processStart "${path.basename(process.execPath)}"`,
    icon: process.execPath,
    iconIndex: 0,
    appUserModelId: ID,
  });
};

export const createShortcut = () => {
  shortcutAtPath(desktopPath);
  shortcutAtPath(startPath);
};

export const removeShortcut = () => {
  if (fs.existsSync(startPath)) fs.unlinkSync(startPath);
  if (fs.existsSync(desktopPath)) fs.unlinkSync(desktopPath);
};

Gotta use them trampolines 😄

@KochiyaOcean

This comment has been minimized.

KochiyaOcean commented Aug 3, 2016

Seems this api cant access icons in the asar achive :-(

@MarshallOfSound

This comment has been minimized.

Member

MarshallOfSound commented Aug 3, 2016

@KochiyaOcean That is correct, although you could at runtime copy the icons out of the ASAR onto the appData section of the users computer.

@SivaThiyagarajan

This comment has been minimized.

SivaThiyagarajan commented Aug 16, 2018

@paulcbetts I have tried the above Shortcut code , but while running i got the below mentioned issue.
untitled

@daviwil daviwil referenced this pull request Sep 21, 2018

Open

Duplicate icons when pinning in taskbar - Windows 10 #14813

1 of 1 task complete
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment