Simple tutorials: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
m (Reverted edits by 201.75.177.209 to last version by Anders)
(Delete uninstaller as the last operation)
 
(17 intermediate revisions by 13 users not shown)
Line 1: Line 1:
{{PageAuthor|eldri005}}
{{PageAuthor|eldri005}}
If this page is your first experience of NSIS, you will need the [http://nsis.sourceforge.net/Download NSIS compiler] to transform the following scripts, and any others you create, into functioning installers. makensisw.exe in the NSIS installation folder is the actual compiler.  It has a graphical front end that explains three ways to load scripts, so it's very easy to use.  Once you have installed NSIS, to create an installer, copy a script into a text editor, save the file with a .nsi extension, and load the file into the makensisw compiler.
==NSIS Setup==
If this page is your first experience of NSIS, you will need the [[Download|NSIS compiler]] to transform the following scripts, and any others you create, into functioning installers. You can use the NSIS Menu and under the Compiler section click <i>Compile NSI scripts</i> to start MakeNSISW.
 
The makensisw.exe in the NSIS installation folder is the actual compiler.  It has a graphical front end that explains three ways to load scripts, so it's very easy to use.  Once you have installed NSIS, to create an installer, copy a script into a text editor, save the file with a .nsi extension, and load the file into the makensisw compiler.


==The bare minimum==
==The bare minimum==
<highlight-nsis>
<highlight-nsis>
# name the installer
# name the installer
outFile "Installer.exe"
OutFile "Installer.exe"


# default section start; every NSIS script has at least one section.
# default section start; every NSIS script has at least one section.
section
Section


# default section end
# default section end
sectionEnd
SectionEnd
</highlight-nsis>
</highlight-nsis>


Line 19: Line 22:
<highlight-nsis>
<highlight-nsis>
# set the name of the installer
# set the name of the installer
outfile "hello world.exe"
Outfile "hello world.exe"


# create a default section.
# create a default section.
section
Section


# create a popup box, with an OK button and the text "Hello world!"
# create a popup box, with an OK button and the text "Hello world!"
messageBox MB_OK "Hello world!"
MessageBox MB_OK "Hello world!"


sectionEnd
SectionEnd
</highlight-nsis>
</highlight-nsis>


Line 35: Line 38:
<highlight-nsis>
<highlight-nsis>
# declare name of installer file
# declare name of installer file
outfile "hello world.exe"
Outfile "hello world.exe"


# open section
# open section
section
Section


# create a popup box, with an OK button and some text
# create a popup box, with an OK button and some text
messageBox MB_OK "Now We are Creating Hello_world.txt at Desktop!"
MessageBox MB_OK "Now We are Creating Hello_world.txt at Desktop!"


/* open an output file called "Hello_world.txt",  
/* open an output file called "Hello_world.txt",  
Line 47: Line 50:
before script is compiled and run */
before script is compiled and run */


fileOpen $0 "$DESKTOP\Hello_world.txt" w
FileOpen $0 "$DESKTOP\Hello_world.txt" w


# write the string "hello world!" to the output file
# write the string "hello world!" to the output file
fileWrite $0 "hello world!"
FileWrite $0 "hello world!"


# close the file
# close the file
fileClose $0
FileClose $0
# Show Success message.
# Show Success message.
messageBox MB_OK "Hello_world.txt has been created successfully at Desktop!"
MessageBox MB_OK "Hello_world.txt has been created successfully at Desktop!"




# end the section
# end the section
sectionEnd
SectionEnd
</highlight-nsis>
</highlight-nsis>


Line 69: Line 72:
<highlight-nsis>
<highlight-nsis>
# define the name of the installer
# define the name of the installer
outfile "simple installer.exe"
Outfile "simple installer.exe"


# define the directory to install to, the desktop in this case as specified   
# define the directory to install to, the desktop in this case as specified   
# by the predefined $DESKTOP variable
# by the predefined $DESKTOP variable
installDir $DESKTOP
InstallDir $DESKTOP


# default section
# default section
section
Section


# define the output path for this file
# define the output path for this file
setOutPath $INSTDIR
SetOutPath $INSTDIR


# define what to install and place it in the output path
# define what to install and place it in the output path
file test.txt
File test.txt


sectionEnd
SectionEnd
</highlight-nsis>
</highlight-nsis>


Line 92: Line 95:
<highlight-nsis>
<highlight-nsis>
# define installer name
# define installer name
outFile "installer.exe"
OutFile "installer.exe"


# set desktop as install directory
# set desktop as install directory
Line 98: Line 101:


# default section start
# default section start
section
Section


# define output path
# define output path
setOutPath $INSTDIR
SetOutPath $INSTDIR


# specify file to go in output path
# specify file to go in output path
file test.txt
File test.txt


# define uninstaller name
# define uninstaller name
writeUninstaller $INSTDIR\uninstaller.exe
WriteUninstaller $INSTDIR\uninstaller.exe




-------
#-------
# default section end
# default section end
sectionEnd
SectionEnd


# create a section to define what the uninstaller does.
# create a section to define what the uninstaller does.
# the section will always be named "Uninstall"
# the section will always be named "Uninstall"
section "Uninstall"
Section "Uninstall"


# Always delete uninstaller first
# Delete installed file
delete $INSTDIR\uninstaller.exe
Delete $INSTDIR\test.txt


# now delete installed file
# Delete the uninstaller
delete $INSTDIR\test.txt
Delete $INSTDIR\uninstaller.exe


sectionEnd
# Delete the directory
RMDir $INSTDIR
SectionEnd
</highlight-nsis>
</highlight-nsis>


Line 132: Line 137:
<highlight-nsis>
<highlight-nsis>
# Name the installer
# Name the installer
outFile "installer.exe"
OutFile "installer.exe"


# default section
# default section
section
Section


     # create a shortcut named "new shortcut" in the start menu programs directory
     # create a shortcut named "new shortcut" in the start menu programs directory
     # presently, the new shortcut doesn't call anything (the second field is blank)
     # presently, the new shortcut doesn't call anything (the second field is blank)
     createShortCut "$SMPROGRAMS\new shortcut.lnk" ""
     CreateShortcut "$SMPROGRAMS\new shortcut.lnk" ""


     # to delete shortcut, go to start menu directory and manually delete it
     # to delete shortcut, go to start menu directory and manually delete it


# default sec end
# default sec end
sectionEnd
SectionEnd
</highlight-nsis>
</highlight-nsis>


Line 152: Line 157:
<highlight-nsis>
<highlight-nsis>
# define name of installer
# define name of installer
outFile "installer.exe"
OutFile "installer.exe"


# define installation directory
# define installation directory
installDir $DESKTOP
InstallDir $DESKTOP
 
# For removing Start Menu shortcut in Windows 7
RequestExecutionLevel user


# start default section
# start default section
section
Section
    
    
     # set the installation directory as the destination for the following actions
     # set the installation directory as the destination for the following actions
     setOutPath $INSTDIR
     SetOutPath $INSTDIR
    
    
     # create the uninstaller
     # create the uninstaller
     writeUninstaller "$INSTDIR\uninstall.exe"
     WriteUninstaller "$INSTDIR\uninstall.exe"
    
    
     # create a shortcut named "new shortcut" in the start menu programs directory
     # create a shortcut named "new shortcut" in the start menu programs directory
     # point the new shortcut at the program uninstaller
     # point the new shortcut at the program uninstaller
     createShortCut "$SMPROGRAMS\new shortcut.lnk" "$INSTDIR\uninstall.exe"
     CreateShortcut "$SMPROGRAMS\new shortcut.lnk" "$INSTDIR\uninstall.exe"
sectionEnd
SectionEnd


# uninstaller section start
# uninstaller section start
section "uninstall"
Section "uninstall"
 
    # Remove the link from the start menu
    Delete "$SMPROGRAMS\new shortcut.lnk"


     # first, delete the uninstaller
     # Delete the uninstaller
     delete "$INSTDIR\uninstall.exe"
     Delete $INSTDIR\uninstaller.exe
 
    # second, remove the link from the start menu
    delete "$SMPROGRAMS\new shortcut.lnk"


    RMDir $INSTDIR
# uninstaller section end
# uninstaller section end
sectionEnd
SectionEnd
</highlight-nsis>
</highlight-nsis>


Line 189: Line 198:
<highlight-nsis>
<highlight-nsis>
# name the installer
# name the installer
outFile "installer.exe"
OutFile "installer.exe"


#default section start
#default section start
section
Section


     # read the value from the registry into the $0 register
     # read the value from the registry into the $0 register
     readRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" CurrentVersion
     ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" CurrentVersion
      
      
     # print the results in a popup message box
     # print the results in a popup message box
     messageBox MB_OK "version: $0"
     MessageBox MB_OK "version: $0"


# default section end
# default section end
sectionEnd
SectionEnd
</highlight-nsis>
</highlight-nsis>


Line 209: Line 218:
<highlight-nsis>
<highlight-nsis>
# name installer
# name installer
outFile "installer.exe"
OutFile "installer.exe"


# default section start
# default section start
section
Section
    
    
     # call userInfo plugin to get user info.  The plugin puts the result in the stack
     # call UserInfo plugin to get user info.  The plugin puts the result in the stack
     userInfo::getAccountType
     UserInfo::GetAccountType
    
    
     # pop the result from the stack into $0
     # pop the result from the stack into $0
     pop $0
     Pop $0
    
    
     # compare the result with the string "Admin" to see if the user is admin.
     # compare the result with the string "Admin" to see if the user is admin.
     # If match, jump 3 lines down.
     # If match, jump 3 lines down.
     strCmp $0 "Admin" +3
     StrCmp $0 "Admin" +3
    
    
     # if there is not a match, print message and return
     # if there is not a match, print message and return
     messageBox MB_OK "not admin: $0"
     MessageBox MB_OK "not admin: $0"
     return
     Return
    
    
     # otherwise, confirm and return
     # otherwise, confirm and return
     messageBox MB_OK "is admin"
     MessageBox MB_OK "is admin"
    
    
# default section end
# default section end
sectionEnd
SectionEnd


</highlight-nsis>
</highlight-nsis>


[[Category:Tutorials]]
[[Category:Tutorials]]

Latest revision as of 14:18, 23 March 2022

Author: eldri005 (talk, contrib)


NSIS Setup

If this page is your first experience of NSIS, you will need the NSIS compiler to transform the following scripts, and any others you create, into functioning installers. You can use the NSIS Menu and under the Compiler section click Compile NSI scripts to start MakeNSISW.

The makensisw.exe in the NSIS installation folder is the actual compiler. It has a graphical front end that explains three ways to load scripts, so it's very easy to use. Once you have installed NSIS, to create an installer, copy a script into a text editor, save the file with a .nsi extension, and load the file into the makensisw compiler.

The bare minimum

# name the installer
OutFile "Installer.exe"
 
# default section start; every NSIS script has at least one section.
Section
 
# default section end
SectionEnd

Simple hello world - popup box

This hello world script will create a popup box with the words "hello world" in it and an "OK" button, when the installer is run

# set the name of the installer
Outfile "hello world.exe"
 
# create a default section.
Section
 
# create a popup box, with an OK button and the text "Hello world!"
MessageBox MB_OK "Hello world!"
 
SectionEnd

Simple hello world - writing text to a file

This hello world script will write "hello world" to a text file when the installer is run

# declare name of installer file
Outfile "hello world.exe"
 
# open section
Section
 
# create a popup box, with an OK button and some text
MessageBox MB_OK "Now We are Creating Hello_world.txt at Desktop!"
 
/* open an output file called "Hello_world.txt", 
on the desktop in write mode. This file does not need to exist 
before script is compiled and run */
 
FileOpen $0 "$DESKTOP\Hello_world.txt" w
 
# write the string "hello world!" to the output file
FileWrite $0 "hello world!"
 
# close the file
FileClose $0
# Show Success message.
MessageBox MB_OK "Hello_world.txt has been created successfully at Desktop!"
 
 
# end the section
SectionEnd

Simply install a file

This installer script will copy the file "test.txt" to the installation directory. First, create the test.txt file in the same directory as the installer script below then compile the installer script. If the installer script is on the Desktop, delete the test.txt file before running the compiled installer. Running the simple installer installs the test.txt file to the Desktop.

# define the name of the installer
Outfile "simple installer.exe"
 
# define the directory to install to, the desktop in this case as specified  
# by the predefined $DESKTOP variable
InstallDir $DESKTOP
 
# default section
Section
 
# define the output path for this file
SetOutPath $INSTDIR
 
# define what to install and place it in the output path
File test.txt
 
SectionEnd

Install a file and create an uninstaller to remove it

This script will do the following: create an installer named "installer.exe"; install a file named "test.txt" to the desktop; create an uninstaller named "uninstaller.exe" on the desktop. The uninstaller will remove itself and the installed text file.

# define installer name
OutFile "installer.exe"
 
# set desktop as install directory
InstallDir $DESKTOP
 
# default section start
Section
 
# define output path
SetOutPath $INSTDIR
 
# specify file to go in output path
File test.txt
 
# define uninstaller name
WriteUninstaller $INSTDIR\uninstaller.exe
 
 
#-------
# default section end
SectionEnd
 
# create a section to define what the uninstaller does.
# the section will always be named "Uninstall"
Section "Uninstall"
 
# Delete installed file
Delete $INSTDIR\test.txt
 
# Delete the uninstaller
Delete $INSTDIR\uninstaller.exe
 
# Delete the directory
RMDir $INSTDIR
SectionEnd

Simply create a start menu item

This installer creates a start menu item, nothing more

# Name the installer
OutFile "installer.exe"
 
# default section
Section
 
    # create a shortcut named "new shortcut" in the start menu programs directory
    # presently, the new shortcut doesn't call anything (the second field is blank)
    CreateShortcut "$SMPROGRAMS\new shortcut.lnk" ""
 
    # to delete shortcut, go to start menu directory and manually delete it
 
# default sec end
SectionEnd

Simple installer and uninstaller with start menu item

This installer will do the following: create an installer named "installer.exe"; an uninstaller on the desktop; a shortcut in the start menu that points to the uninstaller.

# define name of installer
OutFile "installer.exe"
 
# define installation directory
InstallDir $DESKTOP
 
# For removing Start Menu shortcut in Windows 7
RequestExecutionLevel user
 
# start default section
Section
 
    # set the installation directory as the destination for the following actions
    SetOutPath $INSTDIR
 
    # create the uninstaller
    WriteUninstaller "$INSTDIR\uninstall.exe"
 
    # create a shortcut named "new shortcut" in the start menu programs directory
    # point the new shortcut at the program uninstaller
    CreateShortcut "$SMPROGRAMS\new shortcut.lnk" "$INSTDIR\uninstall.exe"
SectionEnd
 
# uninstaller section start
Section "uninstall"
 
    # Remove the link from the start menu
    Delete "$SMPROGRAMS\new shortcut.lnk"
 
    # Delete the uninstaller
    Delete $INSTDIR\uninstaller.exe
 
    RMDir $INSTDIR
# uninstaller section end
SectionEnd

Simply get current version of Java Runtime Environment

This installer just checks the value of the CurrentVersion string for the JRE in the local machine registry using readRegStr. If the result is blank, the JRE is probably not installed.

# name the installer
OutFile "installer.exe"
 
#default section start
Section
 
    # read the value from the registry into the $0 register
    ReadRegStr $0 HKLM "SOFTWARE\JavaSoft\Java Runtime Environment" CurrentVersion
 
    # print the results in a popup message box
    MessageBox MB_OK "version: $0"
 
# default section end
SectionEnd

Check if user is administrator

Sometimes, it's necessary to check if an installer's user has administrative privileges. This simple script checks for that using the "UserInfo" plugin. There's a more sophisticated alternative at http://forums.winamp.com/showthread.php?threadid=195020, but this method seems to work fine. Another example of UserInfo can be found in the NSIS installation directory under \Examples\UserInfo\UserInfo.nsi.

# name installer
OutFile "installer.exe"
 
# default section start
Section
 
    # call UserInfo plugin to get user info.  The plugin puts the result in the stack
    UserInfo::GetAccountType
   
    # pop the result from the stack into $0
    Pop $0
 
    # compare the result with the string "Admin" to see if the user is admin.
    # If match, jump 3 lines down.
    StrCmp $0 "Admin" +3
 
    # if there is not a match, print message and return
    MessageBox MB_OK "not admin: $0"
    Return
 
    # otherwise, confirm and return
    MessageBox MB_OK "is admin"
 
# default section end
SectionEnd