Disable toolbar button if source path does not exist

Is there a way to disable a toolbar button following a source path change (or at the very least, a tab change) using the @ifexists modifier if a folder does not exist?
I've used global variables before to prevent double clicks but these reply on the button script running and changing a variable, e.g. @disableif:$glob:RunningTheScriptDisableButton

I want to be able to disable a menu button if there are no snapshots for the source path. This can easily be tested using a script as the path would have a suffixed, accessible tilde snapshot folder , for example:

set fsu = DOpus.FSUtil
set sourcetab = clickData.func.sourcetab
If not fsu.Exists(sourcetab.path+"~snapshot") then dlg.message = "There are no accessible snapshots"

Of course this is a test after the button has been clicked where as I would like to pre-empt the need to click with a sort of DISABLEIFEXISTS:! modifier command.

I've read a number of posts on this matter including the one below. It would not need to be dynamically triggered by filesystem change events, simply a check following a source path change:

Disable button if path don't exist - Help & Support - Directory Opus Resource Centre (dopus.com)

You could let an event do the check and set a variable:

function OnInit(initData) {
    initData.name = 'CheckPathAndSetVar';
    initData.version = '2023-08-31';
    initData.url = 'https://resource.dopus.com/t/disable-toolbar-button-if-source-path-does-not-exist/45640';
    initData.default_enable = true;
    initData.min_version = '12.0';
}

function OnAfterFolderChange(afterFolderChangeData) {
    if (!afterFolderChangeData.result) return;

    var tab = afterFolderChangeData.tab;
    if (!tab) return;

    var item = tab.path + '~snapshot';

    if (DOpus.FSUtil().Exists(item)) {
        tab.vars.Set('snapshotVar', 1);
    } else {
        tab.vars.Delete('snapshotVar');
    }
}

The button would start like this:

@disableif:!$tab:snapshotVar

Save EventCheckPathAndSetVar.js.txt to

%appdata%\GPSoftware\Directory Opus\Script AddIns

How to use buttons and scripts from this forum

Sorry my bad, I hadn't spotted in my post that the backslash had been filtered out.
The code needed a very minor tweak as it required a backslash between the tab.path and the suffix but I appreciate you were using my example as a reference. After modifying this line your solution worked a treat

var item = tab.path + '\\~snapshot';

Many thanks Ixp.

https://resource.dopus.com/t/formatting-tips-specific-to-the-opus-forum/24899

Hi lxp,

I've spotted a small issue with the script.
When in dual pane mode with navigation lock enabled, it triggers the script for both source and destination panes as each folder changes. This means the source pane could create the variable and the destination could then remove it.

What's the best way to tweak it so that the script only acts on the source pane?.

Are you sure it's an issue?

The variables' scope is on the tab level, so there shouldn't be a conflict. The tabs in source and destination are independent objects, each having its own snapshotVar, even when they point to the same folder in the file system.

I'd be surprised if locked tabs behaved differently, but I haven't looked into it, so let me know if they do.

I wasn't aware that " The tabs in source and destination are independent objects, each having its own snapshotVar".
I put a dopus.output message next to each tab.vars command for troubleshooting and both messages were outputted, giving the impression that a single variable was getting set then immediately deleted by the 2nd pane.
Only one of my dual pane paths has accessible snapshots so it now makes sense that it is correctly outputting the state of each independent snapshotVar".

Sorry for the confusion.