DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Can a script know it was called by another script?

Hello

 

I have two scripts A & B

 

When script A executes it calls script B using the "ScriptStart" command.

 

sometimes I execute script B on its own.

 

Is there anyway for script B to know it was called by script A without passing a global variable?

 

I am using DIAdem 2015

 

Thanks

Tim
0 Kudos
Message 1 of 9
(4,857 Views)

I have  figured out a way that get the job done but perhaps there are others?

 

Here is what I came up with

 

I was able to use the command  ScriptCmdAdd in script A and then the command ScriptCmdIsLoaded in script B to check if it has been loaded.

 

It is worth noting I also used the command ScriptCmdRemove at the end of script A

 

 

 

Tim
0 Kudos
Message 2 of 9
(4,853 Views)

My mistake.

 

The preceeding is not a desirable solution

Tim
0 Kudos
Message 3 of 9
(4,842 Views)

Hi smooth,

 

I don't know any way for script B to know it was called by script A via ScriptStart().

 

Brad Turpin

DIAdem Product Support Engineer

National Instruments

0 Kudos
Message 4 of 9
(4,823 Views)

Not a problem Brad

 

I was able to use as global variable to give me the desired result.

 

Basically I have a global variable in Script A , and Script B checks if this Global Variable exists by setting it to a local variable.  If an error is thrown then I know that the global variable was not initialized therefore Script A could not have called Script B.

 

Thanks for looking at it nonetheless.

Tim
0 Kudos
Message 5 of 9
(4,820 Views)

What is the proper way to use ScriptCmdIsLoaded ()?  I cannot come up with a way for it to detect a script that I have registered with ScriptCmdAdd() has been loaded.  

0 Kudos
Message 6 of 9
(1,916 Views)

Hi Mark,

 

One option for you to consider it to add a argument to the script that is called in "ScriptCmdAdd"  so that it will return some default value or boolean noting that it is actually present.

 

Alternatively, you could make a script  that is added to file that tries to run the main script, with default values, and returns a boolean saying that the main script does indeed exist.

 

That should allow you to determine if script exists and is loaded, and then load it if not present.

 

paul

0 Kudos
Message 7 of 9
(1,878 Views)

Below is a solution.  I ended up needed this capability for a large script based project.

 

 

 

'-------------------------------------------------------------------------------
'Script:  MyScriptThatCallsLibrary.vbs

'Normal execution of function sVersionLibMechatronicSolutionsLLC()..
Call LogFileWrite("sVersionLibMechatronicSolutionsLLC() = " & sVersionLibMechatronicSolutionsLLC())
Call LogFileWrite(vbTab)

'Testing if sub/function sVersionLibMechatronicSolutionsLLC() exists..
Dim sFnOrSubName
sFnOrSubName = "sVersionLibMechatronicSolutionsLLC"
If bUserScriptHasBeenIncluded(sFnOrSubName) Then
  Call LogFileWrite("Function/Sub '" & sFnOrSubName & "' exists in DIAdem script memory")
Else
  Call LogFileWrite("Function/Sub '" & sFnOrSubName & "' does NOT exist in DIAdem script memory")
End If

'Testing if sub/function MySubThatDoesNotExist() exists..
sFnOrSubName = "MySubThatDoesNotExist"
If bUserScriptHasBeenIncluded(sFnOrSubName) Then
  Call LogFileWrite("Function/Sub '" & sFnOrSubName & "' exists in DIAdem script memory")
Else
  Call LogFileWrite("Function/Sub '" & sFnOrSubName & "' does NOT exist in DIAdem script memory")
End If

  
Function bUserScriptHasBeenIncluded(ByVal sFnOrSubName)
  'Returns TRUE if function/sub sFnOrSubName exists in script memory.
  'Create a function that returns a string in the library you want to monitor 
  'and add it to the script library you want to monitor. 
  bUserScriptHasBeenIncluded = False
  If Not VarType(sFnOrSubName) = vbString Then Call Err.Raise(65535,,"ERROR - argument sFnOrSubName passed to bUserScriptHasBeenIncluded() is not a string")
  Dim lErr, sErr
  Dim sCmd, sVer
  sCmd = "sVer = " & sFnOrSubName & "()"
  On Error Resume Next
  Call Execute(sCmd)
  lErr = Err.number: sErr = Err.Description: On Error Goto 0
  If lErr = 0 Then bUserScriptHasBeenIncluded = True
End Function


'-------------------------------------------------------------------------------
'Script:  MyLibrary.vbs

Function sVersionLibMechatronicSolutionsLLC()
  'Returns the current version of this custom script library.
  '(Put this function into an included script library .. ScriptInclude("VbScriptLibWithThisFn.vbs") )
  sVersionLibMechatronicSolutionsLLC = "20201122B"
End Function  'sVersionLibMechatronicSolutionsLLC()
0 Kudos
Message 8 of 9
(1,785 Views)

Another approach:

 

'In the script where you want to check if the library has been included:

On Error Resume Next
Call Execute("T1=sVersionLibMechatronicSolutionsLLC()")
If Not Err.number = 0 Then Call ScriptInclude(CurrentScriptPath & "Lib_MechatronicSolutionsLLC.vbc")
On Error Goto 0

'Below put in the library to be checked:
Function sVersionLibMechatronicSolutionsLLC()
  'Returns the current version of this custom script library.
  '(Put this function into an included script library .. ScriptInclude("VbScriptLibWithThisFn.vbs") )
  sVersionLibMechatronicSolutionsLLC = "20201122B"
End Function  'sVersionLibMechatronicSolutionsLLC()
0 Kudos
Message 9 of 9
(1,770 Views)