Skip to content

electron builder installer.nsh

Windows 把应用添加到 右键菜单中

1. 创建 installer.nsh

2. installer.nsh 内容如下

txt
!define APP_NAME "videovlc" ; 应用名称
!define APP_EXECUTABLE "videovlc.exe" ; 调用应用

!macro customHeader
  !system "echo '' > ${BUILD_RESOURCES_DIR}/customHeader"
!macroend

!macro preInit
  ; This macro is inserted at the beginning of the NSIS .OnInit callback
  !system "echo '' > ${BUILD_RESOURCES_DIR}/preInit"
!macroend

!macro customInit
  !system "echo '' > ${BUILD_RESOURCES_DIR}/customInit"
!macroend

!macro customUnInstall
  !system "echo '' > ${BUILD_RESOURCES_DIR}/customUnInstall"
  ; 卸载时删除右键菜单相关的注册表项
  DeleteRegKey HKCR "*\shell\${APP_NAME}\command"
  DeleteRegKey HKCR "*\shell\${APP_NAME}\Icon"
  DeleteRegKey HKCR "*\shell\${APP_NAME}"

  ; 从OpenWithProgids列表中移除ProgID
  DeleteRegValue HKCR "*\OpenWithProgids" " ${APP_NAME}File"

  ; 删除ProgID相关的注册表项
  DeleteRegKey HKCR "${APP_NAME}File\shell\open\command"
  DeleteRegKey HKCR "${APP_NAME}File\DefaultIcon"
  DeleteRegKey HKCR "${APP_NAME}File"
!macroend
!macro customInstall
  !system "echo '' > ${BUILD_RESOURCES_DIR}/customInstall"
  ; MessageBox MB_OK "安装路径为:$INSTDIR" IDOK
  ; This macro is inserted at the beginning of the NSIS .onInstSuccess callback
  ReadRegStr $0 HKCU "${INSTALL_REGISTRY_KEY}" InstallLocation
  StrCmp $0 "" 0 +3
    Goto skipSaveUserLocation
    WriteRegExpandStr HKCU "${INSTALL_REGISTRY_KEY}" InstallLocation "$0"
    SetRegView 64
    WriteRegExpandStr HKCU "${INSTALL_REGISTRY_KEY}" InstallLocation "$0"
    SetRegView 32
    WriteRegExpandStr HKCU "${INSTALL_REGISTRY_KEY}" InstallLocation "$0"
  skipSaveUserLocation:

  ; 添加注册表项到右键菜单
  WriteRegStr HKCR "*\shell\${APP_NAME}" "" "通过 ${APP_NAME} 打开"
  WriteRegStr HKCR "*\shell\${APP_NAME}\command" "" '"$INSTDIR\${APP_EXECUTABLE}" "%1"'
  ; 设置右键菜单图标
  WriteRegStr HKCR "*\shell\${APP_NAME}\Icon" "" "$INSTDIR\${APP_EXECUTABLE},0"

  ; 注意前面的空格 - 空格可以让应用排列在最前面(不生效)
  ; 创建ProgID
  WriteRegStr HKCR "${APP_NAME}File" "" "通过 ${APP_NAME} 打开"
  WriteRegStr HKCR "${APP_NAME}File\DefaultIcon" "" "$INSTDIR\${APP_EXECUTABLE},0" ; 假设图标是应用程序的第一个资源图标
  WriteRegStr HKCR "${APP_NAME}File\shell\open\command" "" '"$INSTDIR\${APP_EXECUTABLE}" "%1"'

  ; 将ProgID添加到OpenWithProgids列表中
  WriteRegStr HKCR "*\OpenWithProgids" " ${APP_NAME}File" ""
!macroend

!macro customInstallMode
  # set $isForceMachineInstall or $isForceCurrentInstall
  # to enforce one or the other modes.
!macroend

!macro customWelcomePage
  # 欢迎页面默认情况下不会添加到安装程序中。
  ; 注释掉下面一行以禁用欢迎页面
  ; !insertMacro MUI_PAGE_WELCOME
!macroend

!macro customUnWelcomePage
  ; !define MUI_WELCOMEPAGE_TITLE "custom title for uninstaller welcome page"
  ; !define MUI_WELCOMEPAGE_TEXT "custom text for uninstaller welcome page $\r$\n more"
  !insertmacro MUI_UNPAGE_WELCOME
!macroend

3. 如何验证是否添加和删除

手动在注册表中查找和验证上述命令添加的内容,你可以按照以下步骤进行:

  1. 打开注册表编辑器:

    • 按下 Win + R 组合键打开“运行”窗口。
    • 输入 regedit,然后按下 Enter 键。
  2. 导航到对应的键:

    • 在注册表编辑器左侧面板中,展开 HKEY_CLASSES_ROOT (HKCR) 键。
    • 之后依次展开 * -> shell -> Open with... -> shell -> ${APP_NAME}。这里的 ${APP_NAME} 是你在脚本中设置的应用程序名称的占位符,你应该查找实际的应用程序名称。
  3. 验证值:

    • 选中 ${APP_NAME} 键后,在右侧面板中,你应该可以看到默认值设置为 "Open with ${APP_NAME}"。
    • 接着展开 ${APP_NAME} 下的 command 键。在右侧面板中的默认值,你应该可以看到 "${INSTDIR}\${APP_EXECUTABLE}" "%1",其中 ${INSTDIR} 是安装目录,${APP_EXECUTABLE} 是应用程序的可执行文件名称。实际查看时,这两个变量应该被实际路径和文件名替换。

这样,你就在注册表中找到并验证了上述命令所添加的内容。

参考地址