Access platform and environment variables
Info
This how-to assumes you are familiar with setting up a project either manually or via VoltScript Dependency Management. If you are not, follow the Intro to VoltScript Tutorials.
Environ()
Function
The Environ()
function has always been available in LotusScript, and is also available in VoltScript. However, it only allows reading previously set environment variables. Nonetheless, it's useful if read-only access is sufficient, if you aren't already including the OSUtils VoltScript Extension in your project and would prefer not to.
Retrieve home directory with GetThreadInfo() and Environ()
On Windows, the environment variable for the user's directory is "USERPROFILE". On Linux, it is "HOME". GetThreadInfo
provides access to the current platform at index 13.
Function getHomeDirTraditional() as String
Dim platform as String
Dim envVar as String
Dim homeDir as String
platform = GetThreadInfo(13)
Select Case platform
Case "W64":
envVar = "USERPROFILE"
Case "LINX":
envVar = "HOME"
Case Else
Error 1001, "Unexpected platform - " & platform
End Select
Return Environ$(envVar)
End Function
VoltScript dependencies
Incorporating OSUtilsVSE is straightforward. You just need to add the following JSON object to the vsesDependencies
element in your atlas.json
.
"OSUtilsVSE": {
"library": "25282",
"version": "1.0.1",
"module": "25886/20852",
"repository":"volt-mx-marketplace"
}
You'll need to add to your repositories object in the atlas.json of your project:
{
"id": "volt-mx-marketplace",
"type": "marketplace",
"url": "https://community.demo-hclvoltmx.com/marketplace/asset"
}
To use the extension in your script, enter UseVSE "*OSUtilsVSE"
.
Retrieve home directory with OSUtils and Environ()
OSUtils allows the developer to reproduce the same functionality.
Function getHomeDirOS() as String
Dim OSUtils as New OSUtils()
Dim platform as String
Dim envVar as String
Dim homeDir as String
platform = OSUtils.platform
Select Case platform
Case "Windows64":
envVar = "USERPROFILE"
Case "LINUX":
envVar = "HOME"
Case Else
Error 1001, "Unexpected platform - " & platform
End Select
Return OSUtils.getEnvironment(envVar)
End Function
Info
OSUtils VoltScript Extension and the VoltScript runtime each take a separate snapshot of environment variables at the start of execution. This means an environment variable set with OSUtils.setEnvironment()
will be available to OSUtils.getEnvironment()
but not to Environ$()
.
Retrieve home directory with OSUtils.HomeDir
Alternatively, the user's directory can be retrieved from OSUtils, without needing to check the platform or environment variables.
Function getHomeDirOSProperty() as String
Dim OSUtils as New OSUtils()
Return OSUtils.homeDir
End Function
Retrieve temp dir
Retrieving the temp directory via environment variables across platforms using Environ$()
is less straightforward. But OSUtils offers an easy property to get the temp directory.
Set environment variables
Note
Environment variables set via VoltScript won't persist outside the current process call.
Warning
You should be careful to only set environment variables that won't have deleterious impacts.
Sub setEnvironmentVariable()
Dim OSUtils as New OSUtils()
Call OSUtils.setEnvironment("VOLTSCRIPT_HELLO","Hello World")
End Sub
The complete implementations of the code snippets are available on GitHub.