Locked tabs which can only navigate to sub folders

I have create a script which have similar functionality like "Locked (allow folder changes)" of the locked tabs.

The different is that you can navigate only to sub folders, not to super folders. Example: When you locked the tab at c:\myfolder\superfolder\, you can navigate to c:\myfolder\superfolder\subfolder, but not to c:\myfolder\. In the last case a new tab opens with c:\myfolder. The other behavior is same to "Locked (allow folder changes)".

But I am not really satisfy with my solution, because I need for my script to create a new button. But I like more to integrate the script into the menu of lock tabs. But I not found something to do that. Maybe another have a solution for that.

Comments and suggestions for improvement are welcome.

Here now the code of the two script files and the button:


ScriptCommand_LockFolder.vbs.txt

ScriptCommand_LockFolder.vbs.txt (2.9 KB)

Option Explicit
' Lock states:
' 0=lock off; 3=lock, allow only navigate to sub folders

' -----------------------------------
' Event handler before folder change
' -----------------------------------
Function OnInit(ByRef oInitData)
   Dim oCmd

   DOpus.Output("Initializing...")

   ' Provide basic information about the Script
   oInitData.name = "ScriptCommand_LockFolder"
   oInitData.desc = "Sets the LockTab state to Directory Opus."
   oInitData.copyright = "(c) 2014 BitStreamWorld"
   oInitData.version = "v1.0.0 (vbs)"
   oInitData.default_enable = True

   ' Create new ScriptCommand
   set oCmd = oInitData.AddCommand
   oCmd.name = "LockFolderChangesOnlyDown"
   oCmd.method = "OnLockFolderChangesOnlyDown"
   oCmd.desc = oInitData.desc
   oCmd.label = "Lock folder allow changes only go folder down"
   oCmd.template = "LOCKSTATE/N"

   ' 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 to lock a tab folder
' -----------------------------------
Function OnLockFolderChangesOnlyDown(oFuncData)
  Dim iLockState
  Dim oCurrentTab

  logMsg("OnLockFolderChangesOnlyDown")

  iLockState = 0

  If oFuncData.func.args.got_arg.lockstate Then ' lock state of tab given by parameter
    iLockState = oFuncData.func.args.lockstate ' set to the current lock state which is set by a button
  End If

  logMsg("lock state set to " + CStr(iLockState))

  ' unlock or lock to the current path of the tab
  set oCurrentTab = oFuncData.func.sourcetab
  SetTabLockStateToCurrentPath oCurrentTab, iLockState

End Function

' -----------------------------
' Event handler when tab opens
' -----------------------------
Function OnOpenTab(oOpenTabData)
  'init to not lock
  SetTabLockState oOpenTabData.tab, 0
  SetTabLockPath oOpenTabData.tab, ""
End Function

' ----------------------------
' 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

Sub SetTabLockPath(ByRef oCurrentTab, sLockPath)
  If IsNull(oCurrentTab) Then
    Exit Sub
  End If

  oCurrentTab.Vars.Set "LockPath", sLockPath
End Sub

'------------------------------------------------------------------------------------------------------------
' 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

LockFolder.vbs.txt

LockFolder.vbs.txt (5.6 KB)

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

Button:

LockTabNavSub.osp (2.73 KB)

@toggle:if $glob:lockstate

@ifset:$glob:lockstate
@set $glob:lockstate
LockFolderChangesOnlyDown LOCKSTATE=0

@ifset:else
@set $glob:lockstate=true

LockFolderChangesOnlyDown LOCKSTATE=3

2 Likes

Hello Tangy,

many thanks for the script

working this script with Opus 12

+1 to integrate this function to native lock tabs (context menue)


Greetings Lezgus

Hi,

How are these scripts set up and used?

  1. Do I create two scripts?
  2. How do I initialize them?

Thanks

Changes to the forum had messed up the root post, which made things look a lot more complicated. I've fixed that.

For how to install scripts and buttons, please see here:

I've never seen support this great, ever. Not only have you made a great product, but you provide superb support.

Thank you!

1 Like

Any chance to implement this native in Opus? I think this is a great and important feature.

If I want to create the button I only need the Button Script file and the blue marked text for the button, right?

But it seems to work only for one tab? The setting is only saved for the last tab, where I use the button.
And it does not survive opus restart.

Is there an alternative? Lezgus seems to have done something else.

Is there a chance that this function will be integrated natively via the context menu (not via script) in DOpus 13.

The screenshot from post #2 is from the SpeedCommander file manager

https://www.speedproject.de/

Thank you

You can edit the tab context menus in Opus 13, so you can put the script there if you want.

What is visible if this is installed correctly?

I installed the two script files. I put LockTabNavSub.osb on to a toolbar and ran it. It seems to only install the scripts which I had done. I created another button for the folder tabs context menu with this code from the forum:
@toggle:if $glob:lockstate
@ifset:$glob:lockstate
@set $glob:lockstate
LockFolderChangesOnlyDown LOCKSTATE=0
@ifset:else
@set $glob:lockstate=true
LockFolderChangesOnlyDown LOCKSTATE=3

All I see is a checkmark or not in front of LockTabNavSub right clicking on a tab and clicking on LockTabNavSub. It does not seem to do anything. It does not limit going to a superfolder. I do not see a dialog box as shown in the forum for this.

What do I do to get something useful? TIA