/****************************************************************************/ /* Name: Application Launcher Template */ /* Version: 1.2 */ /* Website: http://usb.smithtech.us */ /* Credits, Acknowledgements, History, Etc.: EOF */ /****************************************************************************/ /******************** The Stuff You Need To Fill In *************************/ !define APP "" ;Application Name !define EXE "" ;Application Executable Name /****************** The Additional / Optional Stuff *************************/ ;-- uncomment when needed ;!define REGKEYS "" ;insert regkeys separated by "||" ;-- edit if needed !define AUTHOR "SmithTech" ;Author's Name !define VER "1.0.0.0" ;Launcher Version !define DEFAULTARG "" !define PNAME "${App}Portable" !define ICON "${NSISDIR}\Contrib\Graphics\Icons\modern-install.ico" !define ENABLEEXIT "no" !define ENABLEKILL "no" /****************************************************************************/ /*********************** Launcher Logic and Code ****************************/ /****************** Intermediate/Advanced Users Only ************************/ /****************************************************************************/ ;*** Version Information VIProductVersion "${VER}" VIAddVersionKey ProductName "${APP} Portable" VIAddVersionKey Comments "Allows ${APP} to be run from a removable drive." VIAddVersionKey CompanyName "by ${AUTHOR}" VIAddVersionKey LegalCopyright "Launcher created by ${AUTHOR}" VIAddVersionKey FileDescription "${APP} Portable" VIAddVersionKey FileVersion "${VER}" VIAddVersionKey ProductVersion "${VER}" ;VIAddVersionKey InternalName "" ;VIAddVersionKey LegalTrademarks "" VIAddVersionKey OriginalFilename "${PNAME}.exe" ;VIAddVersionKey PrivateBuild "" ;VIAddVersionKey SpecialBuild "" ;*** Runtime Switches CRCCheck On WindowIcon Off SilentInstall Silent AutoCloseWindow True ;*** Includes !include "FileFunc.nsh" !insertmacro "GetParameters" !ifdef REGKEYS !include "Registry.nsh" !include "WordFunc.nsh" !insertmacro "WordFind" !endif !include WinMessages.nsh ;*** Compiler Flags SetDatablockOptimize on SetCompress force SetCompressor /SOLID /FINAL lzma ;*** Working Variables Var PROGRAMEXE Var PROGRAMPATH Var PROGRAMARGS Var DATADIR Var SUBLAUNCH ;*** Set Basic Information Name "${APP} Portable" Icon "${ICON}" Caption "${APP} Portable - ${VER}" OutFile "${PNAME}.exe" ;*** Language LoadLanguageFile "${NSISDIR}\Contrib\Language files\English.nlf" Section "Main" Call Init ;Initialize Launcher - absolute first things to do Call InitVars ;Initialize Variables and Paths Call InitINI ;Initialize INI, read and apply settings Call InitChecks ;Initialize checks for subsequent launch, folder existance, etc. Call InitReg ;Initialize Registry Settings - backup current reg, apply portable reg Call InitFiles ;Initialize Files - rename current files, apply portable files Call InitFolders ;Initialize Folders - rename current folders, apply portable folders Call InitApp ;Initialize Application - run app Call CleanFolders ;Cleanup Portable Folders - copy and delete portable folders, restore original folders Call CleanFiles ;Cleanup Portable Files - copy and delete portable files, restore original files Call CleanReg ;Cleanup Portable Registry Settings - copy reg, restore original reg Call Clean ;Final Cleanup - any additional cleanup SectionEnd Function Init ; -- Initialize Launcher - absolute first things to do FunctionEnd Function InitVars ; -- Initialize Variables and Paths StrCpy $PROGRAMPATH "" StrCpy $PROGRAMEXE "${EXE}" StrCpy $PROGRAMARGS "${DEFAULTARG}" ;CheckAppAppName IfFileExists "$EXEDIR\App\${APP}\${EXE}" 0 CheckAppName StrCpy "$PROGRAMPATH" "$EXEDIR\App\${APP}" StrCpy "$DATADIR" "$EXEDIR\Data" Goto EndInitVar CheckAppName: IfFileExists "$EXEDIR\${APP}\${EXE}" 0 CheckApp StrCpy "$PROGRAMPATH" "$EXEDIR\${APP}" StrCpy "$DATADIR" "$EXEDIR\Data" Goto EndInitVar CheckApp: IfFileExists "$EXEDIR\App\${EXE}" 0 EndInitVar StrCpy "$PROGRAMPATH" "$EXEDIR\App" StrCpy "$DATADIR" "$EXEDIR\Data" Goto EndInitVar EndInitVar: FunctionEnd Function InitINI ; -- Initialize INI, read and apply settings FunctionEnd Function InitChecks ; -- Initialize checks for subsequent launch, folder existance, etc. ;Check for subsequent launch FindProcDLL::FindProc "$PROGRAMEXE" StrCmp $R0 "1" 0 EndSubCheck StrCpy $SUBLAUNCH "true" EndSubCheck: FunctionEnd Function InitApp ; -- Initialize Application - run app ${GetParameters} $R1 StrCmp $R1 "" 0 +2 StrCpy $R1 $PROGRAMARGS StrCmp $SUBLAUNCH "true" SubsequentLaunch ;Initial Launch ExecWait '"$PROGRAMPATH\$PROGRAMEXE" $R1' Goto EndInitApp SubsequentLaunch: StrCmp ${ENABLEEXIT} "yes" 0 InitAppKillProc StrCmp $R1 "-exit" 0 SubsequentLaunchRun ;SendMessage $0 ${WM_CLOSE} 0 0; close window gracefully Goto EndInitApp InitAppKillProc: StrCmp ${ENABLEKILL} "yes" 0 SubsequentLaunchRun StrCmp $R1 "-exit" 0 SubsequentLaunchRun Sleep 2000 KillProcDLL::KillProc "$PROGRAMEXE" FindProcDLL::FindProc "$PROGRAMEXE" StrCmp $R0 "1" InitAppKillProc Goto EndInitApp SubsequentLaunchRun: Exec '"$PROGRAMPATH\$PROGRAMEXE" $R1' EndInitApp: FunctionEnd Function InitFolders ; -- Initialize Folders - rename current folders, apply portable folders FunctionEnd Function CleanFolders ; -- Cleanup Portable Folders - copy and delete portable folders, restore original folders FunctionEnd Function InitFiles ; -- Initialize Files - rename current files, apply portable files FunctionEnd Function CleanFiles ; -- Cleanup Portable Files - copy and delete portable files, restore original files FunctionEnd Function InitReg ; -- Initialize Registry Settings - backup current reg, apply portable reg !ifdef REGKEYS StrCmp $SUBLAUNCH "true" EndInitReg StrCpy "$R6" "${REGKEYS}" StrCpy "$R8" "0" InitRegLoop: ;--- Seperate registry entries in string StrCmp "$R6" "" EndInitReg IntOp $R8 $R8 + 1 ${WordFind} "$R6" "||" "+01" $R9 ${WordFind} "$R6" "||" "+02*}" $R6 StrCmp "$R6" "$R9" LastInitRegLoop Goto DoInitReg LastInitRegLoop: StrCpy "$R6" "" DoInitReg: ;--- Backup desktop registry settings ${registry::KeyExists} "$R9" $R7 StrCmp "$R7" "0" 0 InitRegApply ${registry::MoveKey} "$R9" "$R9_BAK" $R7 ;--- Apply portable registry settings InitRegApply: IfFileExists "$DATADIR\Registry\RegKey$R8.reg" 0 InitRegLoop ${registry::RestoreKey} "$DATADIR\Registry\RegKey$R8.reg" $R7 Goto InitRegLoop EndInitReg: !endif FunctionEnd Function CleanReg ; - Cleanup Portable Registry Settings - copy reg, restore original reg !ifdef REGKEYS StrCmp $SUBLAUNCH "true" EndCleanReg IfFileExists "$DATADIR\Registry\*.*" +2 0 CreateDirectory "$DATADIR\Registry" StrCpy "$R6" "${REGKEYS}" StrCpy "$R8" "0" CleanRegLoop: ;--- Seperate registry entries in string StrCmp "$R6" "" EndCleanReg IntOp $R8 $R8 + 1 ${WordFind} "$R6" "||" "+01" $R9 ${WordFind} "$R6" "||" "+02*}" $R6 StrCmp "$R6" "$R9" LastCleanRegLoop Goto DoCleanRegWork LastCleanRegLoop: StrCpy "$R6" "" DoCleanRegWork: ;--- Save portable registry settings ${registry::KeyExists} "$R9" $R7 StrCmp "$R7" "0" 0 CleanRegRestore ${registry::SaveKey} "$R9" "$DATADIR\Registry\RegKey$R8.reg" "/G=1 /D=2" $R7 Sleep 50 ;--- Delete portable registry settings ${registry::DeleteKey} "$R9" $R7 Sleep 50 ;--- Restore desktop registry settings CleanRegRestore: ${registry::KeyExists} "$R9_BAK" $R7 StrCmp "$R7" "0" 0 CleanRegLoop ${registry::MoveKey} "$R9_BAK" "$R9" $R7 Goto CleanRegLoop EndCleanReg: !endif FunctionEnd Function Clean ; - Final Cleanup - any additional cleanup StrCmp $SUBLAUNCH "true" EndClean !ifdef REGKEYS ${registry::Unload} !endif EndClean: FunctionEnd /************************* Revision History *********************************************/ /* 1.2.0 - Fixed compile warning when REGKEYS not defined */ /* Added kill process option */ /* Added framework for close window option */ /* 1.1.1 - Added subsiquent launch to InitApp */ /* 1.1 - Added registry handling */ /* Added subsequent launch handling */ /* Added revision history */ /* Added Credits & Acknowledgements */ /* 1.0 - First Draft */ /****************************************************************************************/ /********************* Credits & Acknowledgements ***************************************/ /* John T. Haller - http://www.portableapps.com */ /* All of the launcher on portableapps.com that were used as examples */ /* */ /* Karl Loncarek - http://www.loncarek.de */ /* The PortableApplicationTemplate on which this template is largely based */ /****************************************************************************************/