Select FROMSCRIPT

Trying to get a basic script working which selects files based on filenames in the clipboard:

scriptCmdData.func.command.ClearFiles();

var clip = DOpus.GetClip("text");
var path = scriptCmdData.func.sourcetab.path;
var splits = clip.split('\n');

for (var i = 0; i < splits.length; i++)
{
	var pathAndFileName = path + "\\" + splits[i];
	scriptCmdData.func.command.AddFile(pathAndFileName);
}

var strCommand = "Select FROMSCRIPT DESELECTNOMATCH";
scriptCmdData.func.command.RunCommand(strCommand);

Unfortunately it's only selecting the last filename in the clipboard.

eg if the clipboard is

temp.txt
temp2.txt
temp3.txt

Only the file temp3.txt gets selected.

Would appreciate any tips to push me in the right direction! :slight_smile:

Thanks!

The split() is leaving the \n character at the end of each string. There's probably a better way to do this but one way is a regex which removes it:

for (var i = 0; i < splits.length; i++)
{
   var pathAndFileName = path + "\\" + splits[i].replace(/^\s+|\s+$/gm,'');
   scriptCmdData.func.command.AddFile(pathAndFileName);
}

Ah, makes sense.

Thank you! :smiley:

Do I need to do anything special to cater for files in libraries?

eg test.txt is in lib://Documents but it won't get selected.

Using the regex change you noted above, my pathAndFileName variable contains "lib://Documents\test.txt".

Swapping the slash (ie to "lib://Documents/test.txt") makes no difference (file still won't be selected).

Do I need to get the 'full' path to the folder (eg C:\Users...), rather than the library?

Well, that's embarrassing. I'd done a search of the forums for a similar question and didn't see anything, yet when I ran it again (with slightly different parameters) I found exactly what I needed:

DOpus.FSUtil.Resolve("your lib path here");

Apologies, please ignore the question in the previous post.

There are some cases where Select FROMSCRIPT is picky when it comes to Libraries. (I ran into this myself for the first time a few days ago.) We've got an improvement coming for that.

Another issue is that a path like "lib://Documents/test.txt" could refer to multiple different test.txt files at the top-level of the library's member folders.

Thanks Leo.

My script only needs to work in some very specific scenarios so I think I should be good for now. Once again I'm very impressed with what can be done with DOpus, even if I'm having to figure out javascript as I go... :slight_smile:

Okay, looks like I spoke to soon. I'm certain I saw it working with libraries after adding the FSUtil.Resolve command, but now it's not.

I've managed to work around it in a bit of a kludgy way - forcing the tab to go to the 'full' path first so the directory in the location bar changes from "lib://Documents/Test" to "c:\users\myname\Documents\Test":

scriptCmdData.func.command.ClearFiles();

var forceDirChangeForLibraries = "Go {sourcepath$|noterm}";
DOpus.OutputString(forceDirChangeForLibraries);
scriptCmdData.func.command.RunCommand(forceDirChangeForLibraries);

var clip = DOpus.GetClip("text");
var path = DOpus.FSUtil.Resolve(scriptCmdData.func.sourcetab.path);
var splits = clip.split('\n');

for (var i = 0; i < splits.length; i++)
{
	var pathAndFileName = path + "\\" + splits[i].replace(/^\s+|\s+$/gm,'');
	scriptCmdData.func.command.AddFile(pathAndFileName);
	DOpus.OutputString(pathAndFileName);
}

var strCommand = "Select FROMSCRIPT DESELECTNOMATCH";
scriptCmdData.func.command.RunCommand(strCommand);

Works, but not particularly elegant (screen refresh).

Leo - is this one of the cases you were referring to when you said Select FROMSCRIPT can be picky? I'm hopeful that the improvement that's coming will help with my particular situation.

Otherwise, is there a different way I should be going about this? Essentially what I want to be able to do is have a filename (or set of filenames) in the clipboard (with no path information), and have those files selected in the current tab if they are present.

Unfortunately a lot of work I do is in folders that DOpus recognises as in the Documents library, so a solution that works (in a cleaner way than my current script!) would be ideal.

Thanks. :slight_smile:

That should work better with the update, but if you still have problems after it's out then please let us know.

Thanks Leo. Will stick with what I have and keep an eye out for the update. :slight_smile:

Can confirm that the latest release (12.2) now handles libraries much better. I've been able to remove the workaround code from my script.

Thanks again! :smiley: