How to tell not enough files are selected in a script?

When I don't Select any files & Click the button, then show this error.

item().realpath' is null or not an object (0x800a138f)

How to fix that?

function OnClick(clickData)
{
	var eSel = new Enumerator(clickData.func.sourcetab.selected);
	var getfoldername = eSel.item().realpath.filepart;
}

Read the manual and examples. :slight_smile:

  • The enumerator has a method that tells you if any items are left. It's used in literally every example that uses an enumerator.

  • The collection you're enumerating has a count property which tells you how many items are in it. It's used in the example default script you get when you create a new script button.

Also, if you just want the first file (or first two files, or similar), you can get those directly from the collection without using an enumerator, which makes things a lot easier. Enumerators are generally only needed when you want to loop through the whole list of files.

if (clickData.func.sourcetab.selected.count !== 0)
	{

//Code

}
else
{
return;
}

One cool thing to try: if you go to a "Modifiers" tab in the Command Editor of your script, you can put @disablenosel there and then the button will not be enabled at all unless something is selected.

It's cool (and I often forget it too!) that we can use modifiers even for the jscript/vbscript buttons (we just have to specify them in a separate place)!

modifiers

1 Like