Friday, March 14, 2014

Don't forget to toString() or fsName your file paths

I've had this problem several times recently, where I was trying to use a string function on a file path, and kept getting an error that something "was not an object." I was trying to use lastIndexOf("/") on the path so I could grab just the file name from the end of the path.

var myFileName = fileList[i];       
  var myIndex = myFileName.lastIndexOf('/');

//error


Anyway, the problem was that the file path as I had grabbed it was not really a string, even though it looks like one. I needed to use toString() on it before using lastIndexOf() and then all was well.

var myFileName = fileList[i].toString();       
 var myIndex = myFileName.lastIndexOf('/');

Then I remembered "fsName." I've used it in my other scripts, but I thought it just changed the ugly "%20" code back into spaces. It turns out that it also turns the path into a string. So you get two for one.

var myFileName = fileList[i].fsName();       
 var myIndex = myFileName.lastIndexOf('/');

Thursday, March 13, 2014

How to deselect all Indesign objects in Javascript

It took me a while the other day to find this. How do you deselect all selected items in Indesign using javascript?

app.activeDocument.select(NothingEnum.NOTHING);

thanks to Martin Rohart

http://stackoverflow.com/users/444208/martinrohart

Wednesday, March 12, 2014

Make a dialog that allows you to select multiple files

myFiles = select_multiple_files();

function select_multiple_files(){
var myFiles = File.openDialog("Please select the files you desire",true);
return myFiles;
}

// it's the 'true' option in the parentheses that allows multiple files to be selected.