Example Code

Run a Script When DIAdem Loads a Data File

Code and Documents

Attachment

Overview

This example shows how to set up DIAdem to run a script when DIAdem loads a file.

Description

The example sets up DIAdem to change the default save directory to be the same as the location that the data file is loaded from.

After saving the files, we need to set the setup script to run every time DIAdem starts.  This script will load the second script into memory, then set an event callback for the NAVIGATOR.  This makes it so every time a data file is loaded (which fires the OnLoaded event), the SetDefaultSaveDirectoryFromData function (defined in SetDefaultSaveDirectoryFromData.VBS) will run.  This function reads a property of the first channel to find the path where the file is located.  Then, it sets LayoutWritePath, which is a DIAdem variable that stores the default file path location for layout files (VIEW and REPORT files).  This variable is only valid at runtime, so when you load up DIAdem again and don't load a new file, the paths will still be the default values (defined in Settings»Options»General, Select Path).

Code Snippets:

Call ScriptCmdAdd(AutoActPath & "SetDefaultSaveDirectoryFromData.VBS")
Navigator.Events.OnLoaded = "SetDefaultSaveDirectoryFromData"

Function SetDefaultSaveDirectoryFromData()

   Dim sourceDataFilePath

   'Get the file path of the source file that the first channel came from.

   'If we assume that all channels come from the same file, this will behave as expected

   sourceDataFilePath = ChnPropValGet("[1]/[1]", "sourcedatafilepath")

   'As long as the path is not empty and we haven't already set the LayoutPath, then set

   'LayoutWritePath to be the same as the source file data path.  LayoutWritePath is a variable

   'that manages the default location to save layout (VIEW and REPORT) files to.

   'Other variables that monitor other file types are ScriptReadPath, ScriptWritePath, DataReadPath and DataWritePath

   If ((sourceDataFilePath <> "") And (LayoutReadPath <> sourceDataFilePath)) Then

     LayoutWritePath = ChnPropValGet("[1]/[1]", "sourcedatafilepath")

   End If

End Function


Steps to Implement or Execute Code

1. Download the two VBS files: SetDefaultSaveDirectoryFromData.VBS and SetDefaultSaveDirectoryFromData_Setup.VBS. 

2. Save them to a common location.  They must be next to each other to work properly.

3. Go to Settings>>Options>>General... on the DIAdem Menu Toolbar.

4. In the Start script file selection text box, click the ... button and browse to "SetDefaultSaveDirectoryFromData_Setup.VBS".

5. Select OK.

6. Close DIAdem and relaunch DIAdem.

7. Load a data file in the NAVIGATOR Panel.

8. Select the VIEW Panel and click Open File in the toolbar.

9. Notice how the start directory for file selection will now be located in the same directory as the data file instead of the default DIAdem directory.


Requirements

Software

DIAdem

**This document has been updated to meet the current required format for the NI Code Exchange. For more details visit this discussion thread**

Eric B.
National Instruments

Example code from the Example Code Exchange in the NI Community is licensed with the MIT license.

Contributors