' $SetGlob ' ' v2.1 (2015/04/28) List/Set/Delete Global Variables - Sample calls.. ' 1. $SetGlob ' 2. $SetGlob "Source C:\Windows" Test1 Test2 ' 3. $SetGlob One Two Three Msg="Set some global switches.." ' ' Use option (1) to list existing global variables and values. Use the dialog and uncheck items for deletion. ' Use option (2) to set/replace one or more global variables. ' Option (3) is the same as (2) but the default dialog message is replaced with a user supplied one. ' ' V2.0 Handles all global variables, not just (TRUE/FALSE) switches. ' V2.1 Fixed to handle existing vector global variables. option explicit Function OnInit(ByRef initData) ' OnInit is called by DOpus to initialize the script initData.name = "Set Global Variable(s)" initData.version= "2.0" initData.desc = "Adds the $SetGlob command to Directory Opus" initData.copyright = "(c) 2014 AussieBoykie" initData.default_enable = true dim cmd : set cmd = initData.AddCommand() cmd.name = "$SetGlob" ' This is the name of the command being added cmd.method = "Main" ' This is the routine to execute when the command is invoked cmd.desc = initData.desc cmd.label = initdata.name cmd.template = "TGT/M,MSG/O" End Function Function Main(ByRef scriptCmdData) ' This is the executable code ' Define some key variables here Dim dlg ' DOpus dialog object Dim cdf ' DOpus scriptCmdData.func object Dim fsu ' DOpus fsutil object Dim dbg ' Switch used to control debug tracing Dim i, j, n, s, title, var, glob, globvar, cx, checked ' Initialise some key variables Set dlg = DOpus.Dlg ' DOpus dialog object Set fsu = DOpus.fsutil ' DOpus fsutil object Set cdf = scriptCmdData.func ' DOpus scriptCmdData.func object ' *** For testing purposes, use a lister level variable to activate debug trace dbg = cdf.command.IsSet("$lst:dbg") ' *** To unilaterally activate debug trace, uncomment the following statement ' dbg = True If dbg Then DOpus.output "$SetGlob: Starting" With dlg Dim choices : Set choices = DOpus.NewVector() Dim settings : Set settings = DOpus.NewVector() dim varval n = 0 title = "Uncheck and click OK to delete global variable(s)." If NOT cdf.args.got_arg.TGT Then ' No global variable names were supplied so list current variables For Each Var in DOpus.Vars ' Associated value is normally a string but could be something else, e.g. a vector Set varval = DOpus.Vars(Var.name) s = "Var.name = " & Var.name If VarType(varval) = vbString Then ' varval is a string, no action needed s = " String: " & s ElseIf VarType(varval) = vbObject Then varval = "*Vector Object*" s = " Object: " & s Else varval = "*Type = " & VarType(varval) & "*" s = " ??????: " & s End If n = n + 1 If dbg Then DOpus.output Right("000" & n, 4) & s choices.push_back Var.name & " = " & varval settings.push_back "True" ' All currently defined variables are active by definition Next Else If cdf.args.got_arg.MSG Then title = cdf.args.MSG ' If there is a user supplied dialog title then use it Else title = "Click OK to set checked variables" ' Otherwise supply a default title = title & vbCrLf & "Uncheck and click OK to delete existing or ignore new." End If For i = 1 to cdf.args.tgt.count ' The user has supplied one or more global variable names glob = Trim(cdf.args.tgt(i-1)) globvar = "" j = Instr(2, glob, " ", 1) settings.push_back "True" ' Assume the user wants to set/reset this variable If Not j = 0 Then ' User has specified a variable and a value string. e.g. "NewVar New Value String" globvar = Trim(Mid(glob,j+1)) ' Split off the value glob = Left(glob,j-1) ' Split off the variable choices.push_back glob & " = " & globvar ' Use the supplied value string ElseIf DOpus.vars.exists(glob) Then ' No value supplied but the user specified variable already exists choices.push_back glob & " = " & DOpus.vars.get(glob) ' Use the existing value string Else ' No value supplied and the user specified variable is new choices.push_back glob & " = True" ' Default the value string to TRUE End If n = n + 1 Next End If If n > 0 Then .choices = choices .list = settings .buttons = "OK|Cancel" .message = title .show cx = .result = 0 ' Result is ZERO if the user pressed Cancel End If End With If Not cx Then ' User did not press Cancel With cdf.command For i = 1 to n glob = Trim(dlg.choices(i-1)) j = Instr(2, glob, "=", 1) ' The variable and its value (if a value is defined) are separated by "=" If j > 0 Then glob = Trim(Left(glob,j-1)) ' Isolate the global variable name checked = dlg.list(i-1) s = "@set glob!:" If checked Then s = s & Trim(dlg.choices(i-1)) Else s = s & glob End If If dbg Then DOpus.output "Checked = " & checked & " " & s .addline s Next .deselect = false .run End With End If If dbg Then DOpus.output "$SetGlob: Ending" End Function