It looks like several people, including me, have asked recently how  
to create an optional shortcut -- that is, how to create a shortcut  
that is only installed if a checkbox is checked in the UI.  Thanks to  
some private help from another member of the list (who can identify  
himself if he'd like to), I now have a solution that seems to be  
working.  I thought I would share it with the list.

Please understand that I am fairly new to both WiX and Windows  
Installer, so some of this information may be inaccurate.  I welcome  
corrections to my understanding of what's going on and improvements  
to the technique.

The basic idea is to place the shortcut in a separate component, and  
then conditionalize the component.  There are a couple of tricks,  
however.

First, choose a unique property name that will control installation  
of the shortcut.  You probably want to name this property in all  
capital letters so that it is a public property, and can be set on  
the command line.  I chose INSTALLDESKTOPSHORTCUT.  If you want the  
shortcut to be installed by default, then include the line:

        <Property Id="INSTALLDESKTOPSHORTCUT" Value="1"/>

If you don't want the shortcut to be installed by default, then leave  
this property out.  Note that setting the property to "0" is NOT  
equivalent to leaving it unset.

Next, add a user interface for setting the property.  If you are  
using the standard WiX UI library, you may need to create a local  
copy of your chosen dialog in order to modify it.  In my case, I  
modified the InstallDirDlg and added:

        <Control Id="DesktopShortcutCheckBox" Type="CheckBox"
                 X="20" Y="160" Width="290" Height="17"
                 Property="INSTALLDESKTOPSHORTCUT" CheckBoxValue="1"
                 Text="Create a shortcut for this program on the desktop."/>

If you are generating installers in multiple languages, you may want  
to use a localization variable rather than hard-wiring the text of  
the control.

Next, if you don't already have a Directory element for the folder  
where the shortcut will be placed, create one.  In my case, I want  
the shortcut on the desktop so I created the element:

        <Directory Id="DesktopFolder" Name="Desktop"/>

directly under the toplevel Directory element of my installer.

Next, add a new conditional component for your shortcut.  This is one  
of the tricky parts to get right, both because the shortcut needs to  
point to a file in a different component, and because you need to  
create an artificial object to act as the KeyPath of the component.   
In this case, we create an otherwise unnecessary registry key to act  
as the KeyPath of the component, but an empty file would probably  
also work.  The exact path to the registry key is not important, but  
it should be unique and be in the conventional HKCU/Software/Company/ 
Product area of the registry.  This component should be an XML  
sibling to the component that it will be targeting.  In my case, it  
looks like this:

        <Component Id="DesktopShortcut" Guid="...">
          <Condition>INSTALLDESKTOPSHORTCUT</Condition>
          <CreateFolder/>
          <RegistryKey Root="HKCU" Key="Software\Llamagraphics\Life Balance 
\Install"
                       Action="createAndRemoveOnUninstall">
            <RegistryValue Name="DTSC" Value="1" Type="integer" KeyPath="yes"/>
          </RegistryKey>
          <Shortcut Id="DesktopShortcut" Directory="DesktopFolder"
                    Name="Life Balance" WorkingDirectory="INSTALLDIR"
                    Icon="Application.ico" Target="[#Life_Balance.exe]"/>
        </Component>

Note that "Life_Balance.exe" is the Id of the File element that I  
want the shortcut to point to.  Of course, you should substitute your  
own company, product, and file ids for the ones I have used here.

Lastly, you need to add the new component to the same feature that  
installs the target of the shortcut:

        <ComponentRef Id="DesktopShortcut"/>

When you run Light to link the installer, you will get an ICE69  
warning that your shortcut targets a file in a different component.   
You can safely ignore this warning, since both components are in the  
same feature and will always be installed together.

I hope this information will save somebody out there some time and  
trouble.


Best wishes,

--Stuart A. Malone
   Llamagraphics, Inc.
   Makers of Life Balance personal coaching software
   http://www.llamagraphics.com/



-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
WiX-users mailing list
WiX-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/wix-users

Reply via email to