Simple tutorials: Difference between revisions

From NSIS Wiki
Jump to navigationJump to search
(added section "Simply get current version...")
(Added section "Check if user is...")
Line 178: Line 178:
sectionEnd
sectionEnd
</highlight-nsis>
</highlight-nsis>
==Check if user is administrator==
Sometimes, it's necessary to check if an installer's user has administrative privilages.  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.
<highlight-nsis>
# 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
</highlight-nsis>
[[Category:Tutorials]]
[[Category:Tutorials]]

Revision as of 22:05, 4 June 2006

Author: eldri005 (talk, contrib)


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.  Every NSIS script has at least one 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 finstaller file
outfile "hello world.exe"
 
# open section
section
 
/* open an output file called "helloworld.txt", 
which must exist before script is compiled, 
on the desktop in write mode */
fileOpen $0 "$DESKTOP\helloworld.txt" w
 
# write the string "hello world!" to the output file
fileWrite $0 "hello world!"
 
# close the file
fileClose $0
 
# end the section
sectionEnd

Simply install a file

This installer script will copy the file "test.txt" to the installation directory

# 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"
 
# Always delete uninstaller first
delete $INSTDIR\uninstaller.exe
 
# now delete installed file
delete $INSTDIR\test.txt
 
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
 
# start default section
section
 
    # set the installation directory as the destination for the following actions
    setOutPath $INSTDIR
 
    # create the uninstaller
    writeUninstaller "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"
 
    # first, delete the uninstaller
    delete $INSTDIR\uninstall.exe
 
    # second, remove the link from the start menu
    delete "$SMPROGRAMS\new shortcut.lnk"
 
# 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 privilages. 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