DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Include script files for DataPlugin

Hello,
 
I tried to include a script file containing sub-functions I need using "ScriptInclude" command in my main script file, but these functions are not accessible in my DataPlugin.
 
My script file is named "MyDataPlugin.bvs" and contains :
Option Explicit
Call ScriptInclude("D:\Subs\ArchInfo")
Sub ReadStore(File)
  DispInfo
  Dim Block : Set Block = File.GetBinaryBlock()
  ...  
  'Add the channel from above to the new channel group.
  ChannelGroup.Channels.AddDirectAccessChannel(Channel1)
End Sub
 
The file "ArchInfo.vbs" contains the sub "DispInfo" that actually just pops up a dialog box. This script works fine when I run it manually, but when I used it as a DataPlugin, it produces an error.
 
Is there a way to link sub-script files to a Dataplugin script ?
0 Kudos
Message 1 of 5
(3,596 Views)
Hi there,
 
The native commands of DIAdem does not gets executed in plugins. So you need to put those codes in the plugin file.
 
Priya
0 Kudos
Message 2 of 5
(3,584 Views)
Thanks for your answer Priya !
0 Kudos
Message 3 of 5
(3,577 Views)
It is possible to work around the fact that ScriptInclude is missing from the DataPlugin API.  You can read in the script file you want to include using the Windows Scripting Host file routines, and then run the script by calling the VBS command "Execute".
 
   Const ForReading = 1
   Dim fso : Set fso = CreateObject("Scripting.FileSystemObject")
   Dim ts : Set ts = fso.OpenTextFile("c:\tmp2.vbs", ForReading)
   Dim s : s = ts.ReadAll
   ts.Close
   Execute s
This has the disadvantage that the file you include this way does not get exported with the script.  So if you create a plugin this way, and then right click on it and choose "export", and then double click on the resulting *.uri file on a second computer that won't be enough to get the script running on the second computer.  You'll also have to go and copy the script you are including to the second computer to get it working.
 
This also has the disadvantage that the windows scripting host needs to be installed.  This is not necessarily the case on all DIAdem installations.
 
If you are only writing the script for yourself, then it's not likely that these disadvantages will bother you.
 
Hope that helps,
Myrle
 
P.S.  Information about the file manipulation objects available to VBScript can be found on the Microsoft website at: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/script56/html/sgWorkingWithFiles.as...
******************
For tips and tricks on creating VBScript DataPlugins go to http://dataplugins.blogspot.com.
0 Kudos
Message 4 of 5
(3,567 Views)

Hi Inpriser,

The short answer is that you can link in libraries of VBScript functions and Subroutines by using the built-in "Execute" command in VBScript.  As Priya says, you will NOT be able to use any DIAdem commands or variables in a DataPlugin, and including in a function with the "Execute" function will not change this. 

The long answer is the below function that wraps up the "Execute" functionality in a "ScriptInclude"-like Function:

'---------------------------------------------------------------------------------
'******* ScriptText() ***                                     *** NEW Function ***
'---------------------------------------------------------------------------------
Function ScriptText(Byval ScriptPath)
  Dim i, f, fso, Steps, ErrNum, ErrMsg, Msg
  Set fso = CreateObject("Scripting.FileSystemObject")
  Steps = Array("FIND", "OPEN", "READ", "CLOSE", "EXECUTE")
  FOR i = 0 TO 4
    On Error Resume Next ' suspend VBS error handling  
    Select Case i
      Case 0 : IF NOT fso.FileExists(ScriptPath) THEN Err.Raise -1,"","file not found"
      Case 1 : Set f = fso.OpenTextFile(ScriptPath, 1)
      Case 2 : ScriptText = f.ReadAll
      Case 3 : f.Close
      Case 4 : Execute ScriptText ' test if ScriptText executes without error
    End Select ' i
    ErrNum = Err.Number
    ErrMsg = Err.Description
    On Error Goto 0      ' resume VBS error handling
    IF ErrNum <> 0 THEN Exit For ' i
  NEXT ' i
  IF ErrNum <> 0 Then
    Msg = "ScriptText()   ERROR!!   Could not " & Steps(i) & " the file:"
    Msg =  Msg & vbCRLF & vbCRLF & Chr(34) & ScriptPath & Chr(34)
    Msg =  Msg & vbCRLF & vbCRLF & String(CInt(1.75*Len(ScriptPath)), "-")
    MsgBox Msg & vbCRLF & vbCRLF & "Error " & ErrNum & ": " & ErrMsg
    ScriptText = "Dialog.Cancel"
  End If ' ScriptPath is Executable
End Function ' ScriptText()

Ask if you have additional questions,
Brad Turpin
DIAdem Product Support Engineer
National Instruments

0 Kudos
Message 5 of 5
(3,555 Views)