How to get file name in jscript variable?

My Plan is Update Title from the File Name

@sync:dopusrt /cmd SetAttr {filepath} META "title:{file|noext}"

This button do this for now. But I want to do the same things with Jscript.
So I Need to get the file name in a variable. Can any one Help me please.

The default script shows you examples of how to do this. (As well as many of the scripts on the forum.)

The Default script does not show the file names only, it shows full path with the file name as i found.

Please mention some link will be helpful.

You might have to combine your brain with the documentation to work out minor differences/details. We cannot write every line of code for you for weeks on end. Good luck.

Also, you were using what you are now asking us to help you find in your previous question: How to get album name from the folder names part - #17 by khalidhosain

In previous question I want to get the parent folder name, now I want to get the selected file name, I do not ask you for

for me. I ask you help me to find out the file names only. Thank you

var test= clickData.func.sourcetab.selected_files
	DOpus.Output(test);

it's print 591978

They are the same thing. In both cases you have a Path object (or an object which can give you one). The Path object has a way to give you just the name of the file/folder without the full path, which is something you have already used.

All you need to do is look at the documentation for the objects you have to work out how to get the information you want from them. You can use the examples and code you're already using for clues about where to look.

Just look at the first line in your last reply in the other thread:

clickData.func.sourcetab.path.filepart

If you don't know what path.filepart is doing there, look it up in the manual so you understand it and can use it in other situations like this one.

I had tried some of the properties from the manual. But can't find the right one

Get a Path from whatever type of object you have (if it isn't already a Path). Call path.filepart on that.

path.filepart
Return me the current folder name. But I need the selected file name.
Try to understand please I have a button already for do the same job, so why I want same things to do in JavaScript? Because I love to learn JavaScript.

I understand Path is object. And file part is a properties that can return the current folder name as a string

filepart returns the name of whatever the path points to. That will be a filename if the path points to a file.

The manual tells you this, so I'm assuming you haven't looked at it.

It's really not that hidden in the manual.

1 Like

Hello Dear Ixp
I have tried to use the name Property but can't. May be I do not have clear concept about object and their Property
Do I have use every time the Object name before write their Property?

function OnClick(clickData) {
	DOpus.ClearOutput();
		var test = clickData.func.sourcetab.item.name
		DOpus.output(test);
	
}

give this error

Error at line 3, position 3
'func.sourcetab.item.name' is null or not an object (0x800a138f)

and

function OnClick(clickData) {
	DOpus.ClearOutput();
		var test = clickData.func.sourcetab.realpath.name
		DOpus.output(test);
	
}

give this error

Error at line 3, position 3
'func.sourcetab.realpath.name' is null or not an object (0x800a138f)

clickData.func.sourcetab gives you a Tab object.

(You can find that out from the OnClick, ClickData and Func documentation.)

Look at the documentation for the Tab object to understand why your code wasn't working:

clickData.func.sourcetab.item.name

Tab objects do not have an item property or method.

clickData.func.sourcetab.realpath.name

Tab objects do not have a realpath property or method.

You can't use random property names on objects that aren't documented as supporting them.

Let's look at the part of the Default Script which demonstrates how to list the paths of all selected files and folders:

function OnClick(clickData)
{
	// ...

	for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext())
	{
		if (eSel.item().is_dir)
		{
			DOpus.Output("  (d) " + eSel.item().realpath);
		}
		else
		{
			DOpus.Output("  (f) " + eSel.item().realpath);
		}
	}

	// ...
}

We can simplify that, since we don't care about treating directories and files differently:

function OnClick(clickData)
{
	for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext())
	{
		DOpus.Output(eSel.item().realpath);
	}
}

If you look at the documentation, clickData.func.sourcetab.selected gives you a list of all selected items.

The for loop in the Default Script takes that list and loops through each of those items. Unfortunately, JScript's syntax for enumerating a list is a little complex, but you can reuse the example in most situations.

Within that loop, eSel.item() gets you an Item object, one for each selected file or folder.

Look at the documentation for the Item object.

The Item object has a name property you can use to get just the filename. That's a shortcut, which gives you a quick way to get an item's name.

That is what Lxp was suggesting you use:

function OnClick(clickData)
{
	for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext())
	{
		DOpus.Output(eSel.item().name);
	}
}

Lxp's suggestion is easier, but I'll explain my alternative suggestion as well, for completeness:

The Default Script gave you an example using eSel.item().realpath which printed the full path to each item, and you wanted to modify that to print the names on their own.

Look at the documentation for the Item object again.

The Item object's realpath property is documented as returning a Path object.

Look at the documentation for the Path object.

The Path object has a filepart property which returns just the name at the end of the path.

That is what I was suggesting you use:

function OnClick(clickData)
{
	for (var eSel = new Enumerator(clickData.func.sourcetab.selected); !eSel.atEnd(); eSel.moveNext())
	{
		DOpus.Output(eSel.item().realpath.filepart);
	}
}
3 Likes

This is the biggest help for now for me. :heart_eyes: I don't have any word to express my happiness :smiling_face_with_three_hearts: Thank you so much Leo :heart:.

now I clearly Understand what is the relationship between them.
the very first is OnClick that's give me a argument called ClickData This Clickdata has only one property named func so when I join clickData.func This func Return me 9 Property. and 6 of them is return me another 6 object such as args, argsmap, command, desttab, sourcetab, . if I use one of them then I can use That's object's Property.

and lxp you are also so helpful.

1 Like