Pages: 1
Author Topic: Script Example: Reading Windows Environment Variables  (Read 15515 times)
Derek Read
Program Manager (XMetaL)

Posts: 2551

« on: October 31, 2008, 04:30:20 PM »

XMetaL Author, XMetaL Developer

You may find you need to read certain Windows environment variables when writing a script for use with XMetaL Author.

Windows environment variables are normally set in the Windows Systems Properties dialog. This dialog is accessible by right clicking on My Computer, selecting the Advanced tab and clicking on the Environment Variables button. These variables may also be set by the IT department at your company and various applications may insert their own environment variables during installation.

    * Obtain the folder designated as the TEMP folder so you can write temporary files to it for manipulation on disk before a transformation or other similar functionality.
    * Find out where JAVA considers "home" by reading the CLASSPATH environment variable.
    * Read an author's USERDOMAIN and / or USERNAME so you can include them automatically in an XML document as meta data describing who created or last made changes to the document.

* Licensed Materials - Property of JustSystems, Canada, Inc.
* (c) Copyright JustSystems Canada, Inc. 2008
* All rights reserved.
* The sample contained herein is provided to you "AS IS".
* It is furnished by JustSystems Corporation as a simple example and has not been
* thoroughly tested under all conditions. JustSystems Canada, Inc., therefore, cannot
* guarantee its reliability, serviceability or functionality.
* This sample may include the names of individuals, companies, brands and products
* in order to illustrate concepts as completely as possible. All of these names are
* fictitious and any similarity to the names and addresses used by actual persons or
* business enterprises is entirely coincidental.

Example 1:
The following JScript example demonstrates how to instantiate the Windows Script Host and use its Environment object to read environment variables. The following variables are typically set on versions of Windows that XMetaL Author can be installed to. This example simply displays a message box reporting the values of the variables listed below.

This list is not meant to be a complete reference and may have omitted some variables. Please refer to Microsoft's documentation if you need to obtain the value of a setting not listed here.

//Instantiate the WSH COM control's Shell object
  var wsh     = new ActiveXObject("Wscript.Shell");

//Get the list of environment variables
  var envVars = wsh.Environment("PROCESS");

//Build one big string containing a single environment variable value per line
  var msg     = "";
  msg += "\nALLUSERSPROFILE:\n " + envVars.Item("ALLUSERSPROFILE");  //Location of the All Users Profile
  msg += "\nAPPDATA:\n " + envVars.Item("APPDATA");  //Default location where applications may store data
  msg += "\nCOMPUTERNAME:\n " + envVars.Item("COMPUTERNAME");  //Name of the computer
  msg += "\nCOMSPEC:\n " + envVars.Item("COMSPEC");  //Path to the command shell executable
  msg += "\nCOMMONPROGRAMFILES:\n " + envVars.Item("COMMONPROGRAMFILES");  //Location of the default directory for shared program files
  msg += "\nHOMEDRIVE:\n " + envVars.Item("HOMEDRIVE");  //Local workstation drive letter connected to the user's home directory
  msg += "\nHOMEPATH:\n " + envVars.Item("HOMEPATH");  //Full path to the user's home directory
  msg += "\nHOMESHARE:\n " + envVars.Item("HOMESHARE");  //Network path to the user's shared home directory
  msg += "\nLOGONSERVER:\n " + envVars.Item("LOGONSERVER");   //Name of the domain controller that validated the current logon session
  msg += "\nNUMBER_OF_PROCESSORS:\n " + envVars.Item("NUMBER_OF_PROCESSORS");  //Number of processors installed on the computer
  msg += "\nOS:\n " + envVars.Item("OS");  //Returns the operating system name. Windows 2000 and XP return Windows_NT as the OS
  msg += "\nPATH:\n " + envVars.Item("PATH");  //Search path for executable files
  msg += "\nPATHEXT:\n " + envVars.Item("PATHEXT");  //List of the file extensions that the operating system considers to be executable
  msg += "\nPROCESSOR_ARCHITECTURE:\n " + envVars.Item("PROCESSOR_ARCHITECTURE");  //Chip architecture of the processor
  msg += "\nPROCESSOR_IDENTIFIER:\n " + envVars.Item("PROCESSOR_IDENTIFIER");  //Description of the processor
  msg += "\nPROCESSOR_LEVEL:\n " + envVars.Item("PROCESSOR_LEVEL");  //Model number of the processor
  msg += "\nPROCESSOR_REVISION:\n " + envVars.Item("PROCESSOR_REVISION");  //Revision number of the processor
  msg += "\nPROGRAMFILES:\n " + envVars.Item("PROGRAMFILES");  //Location of the default program installation directory
  msg += "\nSYSTEMDRIVE:\n " + envVars.Item("SYSTEMDRIVE");  //Drive containing the Windows root directory (the system root)
  msg += "\nSYSTEMROOT:\n " + envVars.Item("SYSTEMROOT");  //Location of the Windows root directory
  msg += "\nTEMP:\n " + envVars.Item("TEMP");  //Default temporary directory; some apps require TEMP and others require TMP
  msg += "\nTMP:\n " + envVars.Item("TMP");  //Default temporary directory; some apps require TEMP and others require TMP
  msg += "\nUSERDOMAIN:\n " + envVars.Item("USERDOMAIN");  //Name of the domain that contains the user's account
  msg += "\nUSERNAME:\n " + envVars.Item("USERNAME");  //Name of the user who is currently logged on
  msg += "\nUSERPROFILE:\n " + envVars.Item("USERPROFILE");  //Location of the profile for the current user
  msg += "\nWINDIR:\n " + envVars.Item("WINDIR");  //Location of the operating system directory

//Display the string

//Kill the WSH control to be sure Windows Script Host (JScript) retains no references to it and will shut down cleanly
  wsh         = null;

Example 2:
The following JScript example demonstrates how to instantiate the Windows Script Host and use its Environment object to read environment variables that may be set by the user or applications.

You will need to make sure that you have set an environment variable called CLASSPATH in order to obtain a return value that is not null. Setting this value is most easily done in the Windows System Properties dialog. CLASSPATH is very often defined for systems that have JAVA installed.

Note the use of the keyword SYSTEM in this example rather than PROCESS as in Example 1.
var wsh     = new ActiveXObject("Wscript.Shell");
var envVars = wsh.Environment("SYSTEM");
var msg     = "";
msg += "\nCLASSPATH:\n " + envVars.Item("CLASSPATH");
wsh         = null;

See also "Script Example: Finding the Paths to Windows "Special Folders"":,3677

External References:
« Last Edit: September 03, 2015, 02:29:56 PM by Derek Read » Logged
Pages: 1
Jump to: