Check if selected file is an archive?

This button Smart archive extraction shall check with below code if the selected item is an archive.

But the warning does only appear if the selected item is a folder. If the selected item is a non-archive file the script cancels without warning/information.

Where is the mistake?

	var cmd = clickData.func.command;
	var tab = clickData.func.sourcetab;
	var isDual = clickData.func.command.IsSet("DUAL=on");
	var fsu = DOpus.FSUtil();
	var Dlg = DOpus.Dlg;
	var selWarning = false;
	var isTargetSet = false;
	var canceled = false;
	cmd.deselect = false;
	cmd.SetDestTab(tab);

	// cmd.RunCommand('Set UTILITY=otherlog');
	DOpus.ClearOutput();
	DOpus.Output("SMART EXTRACT STARTS\n");
	DOpus.Output("is dual : " + isDual);

		for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
			var item = e.item();

			if (!canceled) {
				// Skips selected item if it’s not an archive and warns user later.
				if (!item.InGroup("Archives")) {
					DOpus.Output("Error (not an archive file) : '" + item.name + "'");
					selWarning = true;
					continue;
				}
...

I assume that there is no similar command to @disablenosel:files which disable a button when it is not an archive?
That would be an alternative to the above mentioned check.

The original script has a check for when no files are selected and only folders are selected (which isn't visible in the fragment you've posted). It's probably that causing the difference.

You can pass wildcards to @disablenosel:

In most situations (including this one), wildcards can reference file type groups, to avoid having an explicit list of wildcard extensions:

So you can use this:

@disablenosel:type=grp:Archives

1 Like

Ok, thank you. I will try this. I saw the possibility to use wildcards, but did not know the group-function. Then the check via script should be not necessary at all.
I have to check what happens if I select an archive and another file...

With the original script I only get a warning when no file or a folder is selected. When I select e.g. a .txt nothing is happening. So therefore the check would have to be changed I understand.

The button will be enabled. @disablenosel only disables a button if no matching thing is selected. If extra, non-matching things are also selected, it won't disable the button.

I tried it now and is does not work. The button is disabled even when I select a zip file.

Regarding the script. As written the warning dialog regarding no archive selected does not appear when a non-archive file is selected. But Dopus.Output is writing the "not an archive file" to the log.

But why does the "warns user later" not work for files?

		DOpus.Output("Enumerating...\n");

		for (var e = new Enumerator(tab.selected_files); !e.atEnd(); e.moveNext()) {
			var item = e.item();

			if (!canceled) {
				// Skips selected item if it’s not an archive and warns user later.
				if (!item.InGroup("Archives")) {
					DOpus.Output("Error (not an archive file) : '" + item.name + "'");
					selWarning = true;
					continue;
				}

				// If it’s dual display, ask user where to extract
				if (isDual && !isTargetSet) {
					isTargetSet = true;
					Dlg.window = tab;
					Dlg.icon = "question";
					Dlg.top = true;
					Dlg.template = "destination";
					var retVal = Dlg.Show();
					// DOpus.Output("pressed button : "+retVal);

					switch (retVal) {
						case 1: // source
							// instruction
							DOpus.Output("pressed button : source");
							break;

						case 2: // destination
							DOpus.Output("pressed button : destination");
							cmd.SetDestTab(clickData.func.desttab);
							break;

						default:
							// cancel
							DOpus.Output("pressed button : cancel");
							canceled = true;
							DOpus.Output("\nSMART EXTRACT END\n");
							continue;
							break;
					}
				}

				var folderEnum = fsu.ReadDir(item);
				var folderItem = folderEnum.Next();
				if (folderItem.is_dir && folderEnum.complete) {
					var cmdLine = 'Copy FILE="' + item + '" WHENEXISTS=rename EXTRACT';
				} else {
					var cmdLine = 'Copy FILE="' + item + '" WHENEXISTS=rename EXTRACT=sub';
				}
				DOpus.Output(cmdLine);
				cmd.RunCommand(cmdLine);
			}

			if (selWarning) {
				Dlg.window = tab;
				Dlg.icon = "warning";
				Dlg.top = true;
				Dlg.template = "warning";
				Dlg.Show;
			}
			DOpus.Output("\nSMART EXTRACT END\n");
		}
	}
}

It works fine. Where did you put it? If it's a script, it has to go into the Modifiers tab, since it can't be part of the actual script code.

I don't know, and I can't debug a script fragment that won't run by itself.

If the problem is with script logic then you'll need to debug it yourself.

If you think there's an issue with Opus, break down the problem into a simple example which demonstrates the problem and we can look at it. But I suspect that if you debug, examine and simplify the script then you will discover the real issue somewhere in the script rather than Opus.

Ah, I use German language and have to use @disablenosel:type=grp:Archive
So without "s". I thought this group has a generic name independent from the language.

It also works if I put it in the first line of the scriptcode area of a button.

The full script is available via the thread I put into the start post. I have to check how to debug a script. :slight_smile: I'm an absolute beginner.

EDIT: Ah, the mentioned script area with !item.InGroup("Archives") does not work at all. (At least the dialog does not open. Dopus.Output works) Only the warning if no file is selected does work. That is the reason why this message appears for folders.

ok. debug via Dopus.Output.

Should the command @disablenosel:type=grp:Archives not be independent from the language?
!item.InGroup("Archives") does work also for German language setting.

And two groups seems to be not possible with the desribed syntax (grp:Images|grp:Documents)

Is it intended, that the command is language-sensitive?
Any possibility to define it independent? Would be relevant for sharing a button...

The manual’s description of the InGroup method you’re using explains how to make it language independent.

There is no issue with the ingroup command. It works in both languages with "Archives".

But the @disablenosel command does not work for the German display language with the internal name "Archives". It only works with the German display name "Archive". So it seems this command checks only the display name, but not the internal name.
Same for "pictures". Only "Bilder" does enable the button, but not "Pictures".
And it is not possible to use "name:" in this command as far as I see it.

According to the description for ingroup the name "Archives" is the internal name for Archives which is language independent.

Do I understand something wrong or is there the bug (or intention) that @disablenosel ignores the internal name? :slight_smile:

As written the command checks only the display name.

@disablenosel, and pattern matching in general, match on the display name, so they'll depend on the language you're using.

item.InGroup in the scripting API is a special case which checks for both the display name and the internal name, since scripts are more likely to be shared with other people.

ok.
Would it be high effort or not possible to implement also for @disablenosel/pattern machting a check for the internal name? At least it would make things easier. :slight_smile: Of course people could adapt this command when a button is shared.

Only a suggestion... :slight_smile: