Option Explicit ' Lock states: ' 0=lock off, 3=lock, allow only navigate to sub folders Const vbQuote = """" ' ----------------------------------- ' Event handler before folder change ' ----------------------------------- Function OnInit(ByRef oInitData) DOpus.Output("Initializing...") ' Provide basic information about the Script oInitData.name = "OnBeforeFolderChange_lockFolder" oInitData.desc = "Adds an 'OnBeforeFolderChange' event handler to Directory Opus to lock folder tabs." oInitData.copyright = "(c) 2014 BitStreamWorld" oInitData.version = "v1.0.0 (vbs)" oInitData.default_enable = True ' Set DEBUG flag below to true in order to enable logging messages to the Opus Output Window oInitData.config.DEBUG = True End Function ' ----------------------------------------------- ' Event handler when the folder change ' decide to change the folder or open new folder ' ----------------------------------------------- Function OnBeforeFolderChange(oBeforeFolderChangeData) Dim sCurrentPath Dim oCurrentTab Dim iCurrentLockState Dim sCurrentLockPath Dim iCmpResult Dim bLockNow Dim sCmd logMsg("OnBeforeFolderChange") sCurrentPath = oBeforeFolderChangeData.path set oCurrentTab = oBeforeFolderChangeData.tab iCurrentLockState = GetTabLockState(oCurrentTab) bLockNow = False If iCurrentLockState = 3 Then ' tab is locked sCurrentLockPath = GetTabLockPath(oCurrentTab) iCmpResult = InStr(1, sCurrentPath, sCurrentLockPath) ' check if the currentPath is sub path of the currentLockPath If iCmpResult > 0 Then bLockNow = False ' current Path contains lock Path Else bLockNow = True ' current Path is not sub path of current lock tab => not change the folder, open folder in new tab End If End If ' tab is locked => open folder in new tab If bLockNow Then sCmd = "Go " & vbQuote & sCurrentPath & vbQuote & " NEWTAB NOSCRIPT" RunSingleCommand sCmd End If OnBeforeFolderChange = bLockNow ' not change the folder of the tab End Function ' -------------------------------------- ' Event handler when the tab get active ' go back to the lock folder of the tab ' -------------------------------------- Function OnActivateTab(oActivateTabData) Dim sCurrentPath Dim iCurrentLockState Dim sCmd logMsg("OnActivateTab") If IsNull(oActivateTabData.new) Then Exit Function End If iCurrentLockState = GetTabLockState(oActivateTabData.new) logMsg("active tab lock state = "+CStr(iCurrentLockState)) If iCurrentLockState = 3 Then ' tab is locked sCurrentPath = GetTabLockPath(oActivateTabData.new) sCmd = "Go " & vbQuote & sCurrentPath & vbQuote ' go back to the lock folder of the tab RunSingleCommand sCmd End If SetLockIcon oActivateTabData.new End Function ' ----------------------------- ' Event handler when tab opens ' init new tab ' ----------------------------- Function OnOpenTab(oOpenTabData) If IsNull(oOpenTabData.tab) Then Exit Function End If SetTabLockState oOpenTabData.tab, 0 SetTabLockPath oOpenTabData.tab, "" SetLockIcon(oOpenTabData.tab) End Function ' ---------------------- ' Execute Dopus Command ' ---------------------- Sub RunSingleCommand(sCmd) Dim oDopusCmd logMsg(sCmd) Set oDopusCmd = DOpus.CreateCommand oDopusCmd.RunCommand(sCmd) End Sub ' ------------------------------------ ' Execute command to change lock icon ' ------------------------------------ Sub SetLockIcon(currentTab) Dim oDopusCmd dim iLockState iLockState = GetTabLockState(currentTab) If iLockState = 0 Then RunSingleCommand "@set glob:lockstate=true" End If Set oDopusCmd = DOpus.CreateCommand oDopusCmd.AddLine "@toggle:if $glob:lockstate" oDopusCmd.AddLine "@ifset:$glob:lockstate" oDopusCmd.AddLine "@set glob:lockstate" oDopusCmd.AddLine "LockFolderChangesOnlyDown LOCKSTATE=0" oDopusCmd.AddLine "@ifset:else" oDopusCmd.AddLine "@set glob:lockstate=true" oDopusCmd.AddLine "LockFolderChangesOnlyDown LOCKSTATE=3" oDopusCmd.Run End Sub ' ---------------------------- ' Set and get tab lock states ' ---------------------------- Sub SetTabLockStateToCurrentPath(ByRef oCurrentTab, iLockState) SetTabLockState oCurrentTab, iLockState SetTabLockPath oCurrentTab, oCurrentTab.path End Sub Sub SetTabLockState(ByRef oCurrentTab, iLockState) If IsNull(oCurrentTab) Then Exit Sub End If oCurrentTab.Vars.Set "LockState", iLockState End Sub Function GetTabLockState(ByRef oCurrentTab) ' As Integer If oCurrentTab.Vars.Exists("LockState") Then GetTabLockState = oCurrentTab.Vars.Get("LockState") Else GetTabLockState = 0 End If End Function ' -------------------------------------- ' Set and get lock path of the lock tab ' -------------------------------------- Sub SetTabLockPath(ByRef oCurrentTab, sLockPath) If IsNull(oCurrentTab) Then Exit Sub End If oCurrentTab.Vars.Set "LockPath", sLockPath End Sub Function GetTabLockPath(ByRef oCurrentTab) 'As String If oCurrentTab.Vars.Exists("LockPath") Then GetTabLockPath = oCurrentTab.Vars.Get("LockPath") End If End Function '------------------------------------------------------------------------------------------------------------ ' Subroutine to allow toggling of the global VT_BOOL variable to control whether logging is performed or not '------------------------------------------------------------------------------------------------------------ Sub logMsg(sMessage) If (Script.config.DEBUG) Then DOpus.Output(sMessage) End If End Sub