Uninstall Renamed-Moved-Copied Shortcuts

From NSIS Wiki
Jump to navigationJump to search
Author: Red Wine (talk, contrib)


Description

The research on the subject uninstall shortcuts that have been possibly renamed and/or moved/copied started on this forum thread.
Requires RecFind.nsh by Afrow UK and ShellLink plug-in.

The Code

Name "My App"
OutFile "MyApp.exe"
InstallDir '$PROGRAMFILES\My App'
InstallDirRegKey HKCU "Software\My App" ""
ShowInstDetails show
ShowUninstDetails show
 
!include 'RecFind.nsh'
 
  !define ICON_SEARCH '$SMPROGRAMS$R0\$R1'
  !define DESK_SEARCH '$DESKTOP$R0\$R1'
  !define MyApp '$INSTDIR\makensisw.exe'
  !define Uninst '$INSTDIR\uninstall.exe'
 
  var SM_FOLDER
 
page directory
page instfiles
 
Section Install
 SetOutPath '$INSTDIR'
   File '${NSISDIR}\makensisw.exe'
   createshortcut '$DESKTOP\makensisw.lnk' '${MyApp}'
   createdirectory '$SMPROGRAMS\My App'
   createshortcut '$SMPROGRAMS\My App\makensisw.lnk' '${myApp}'
   writeuninstaller '${Uninst}'
   createshortcut '$SMPROGRAMS\My App\uninstall.lnk' '${Uninst}'
   WriteRegStr HKCU "Software\My App" "" "$INSTDIR"
sectionend
 
 # In order to examine the example uncomment the following section.
 # By doing this, shortcuts will be renamed and copied to All Users as well.
 # Of course you may manoually rename move copy the shortcuts the way you like.
/*
Section RenameCreatedShortCuts
   Rename '$SMPROGRAMS\My App' '$SMPROGRAMS\My Renamed App'
   Rename '$SMPROGRAMS\My Renamed App\makensisw.lnk' \
   '$SMPROGRAMS\My Renamed App\Renamed makensisw.lnk'
   Rename '$SMPROGRAMS\My Renamed App\uninstall.lnk' \
   '$SMPROGRAMS\My Renamed App\Renamed uninstall.lnk'
   CopyFiles '$DESKTOP\makensisw.lnk' '$DESKTOP\copied makensisw.lnk'
   StrCpy '$R9' '$SMPROGRAMS'
   StrCpy '$R8' '$DESKTOP'
   SetShellVarContext all
   CreateDirectory '$SMPROGRAMS\My ALL USERS App'
   CopyFiles '$R9\My Renamed App\*' '$SMPROGRAMS\My ALL USERS App'
   CopyFIles '$R8\makensisw.lnk' '$DESKTOP\ALL USERS makensisw.lnk'
   CopyFIles '$R8\copied makensisw.lnk' '$DESKTOP\copied ALL USERS makensisw.lnk'
SectionEnd
*/
 
Section Uninstall
   Delete ${MyApp}
   Delete ${Uninst}
   RmDir '$INSTDIR'
 
   Call un.StartMenuClean
   Call un.DesktopClean
 
   DeleteRegKey HKCU "Software\My App"
SectionEnd
 
Function un.StartMenuClean
  StrCpy '$R9' '0'
 start:
   ${RecFindOpen} '$SMPROGRAMS' '$R0' '$R1'
     ${RecFindFirst}
       ; DetailPrint '${ICON_SEARCH}'
	 ShellLink::GetShortCutTarget '${ICON_SEARCH}'
	 Pop $0
	 StrCmp '$0' '${MyApp}' 0 next
         MessageBox mb_ok '${ICON_SEARCH}'
	 StrCpy '$SM_FOLDER' '$R0'
         Delete '${ICON_SEARCH}'
       next:
	 StrCmp '$0' '${Uninst}' 0 end
	 MessageBox mb_ok '${ICON_SEARCH}'
         Delete '${ICON_SEARCH}'
       end:
     ${RecFindNext}
   ${RecFindClose}
 
   StrCmp '$SM_FOLDER' '' +3
   MessageBox mb_ok '$SMPROGRAMS$SM_FOLDER'
   RmDir '$SMPROGRAMS$SM_FOLDER'
 
  StrCmp '$R9' '1' func_end
  SetShellVarContext all
  StrCpy  '$SM_FOLDER' ''
  StrCpy '$R9' '1'
  goto start
 func_end:
FunctionEnd
 
Function un.DesktopClean
  StrCpy '$R9' '0'
 start:
   ${RecFindOpen} '$DESKTOP' '$R0' '$R1'
     ${RecFindFirst}
       ; DetailPrint '${DESK_SEARCH}'
	 ShellLink::GetShortCutTarget '${DESK_SEARCH}'
	 Pop $0
	 StrCmp '$0' '${MyApp}' 0 next
	 messagebox mb_ok '${DESK_SEARCH}'
         Delete '${DESK_SEARCH}'
         next:
     ${RecFindNext}
   ${RecFindClose}
  StrCmp '$R9' '1' func_end
  SetShellVarContext current
  StrCpy '$R9' '1'
  goto start
 func_end:
FunctionEnd

License

This script is provided 'as-is', without any express or implied warranty. In no event will the author be held liable for any damages arising from the use of this code.

Permission is granted to anyone to use this script for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:

  1. The origin of this script must not be misrepresented; you must not claim that you wrote the original code. If you use this script in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  2. Altered versions must be plainly marked as such, and must not be misrepresented as being the original script.
  3. This notice may not be removed or altered from any distribution.