Skip to main content
Participating Frequently
June 28, 2022
Question

Java script to pull link names and character style

  • June 28, 2022
  • 2 replies
  • 282 views

Could someone please advise why I can't get this script to work?

 

// Set Global Variables.
var myarr = [];
var PATH = "/Users/UserName/Desktop/InDesign/";

// Write Data to CSV File.
function writeToFile(myarr){
var s = "LinkName, StyleNumber, Page, X, Y, ImgWidth, ImgHeight, X-Scale, Y-Scale, Path";
var outFolder = Folder(PATH);
var tempstring = "";

// Concatenate All Array Data.
for(j = 0; j < myarr.length; j++) {
var x = String(myarr[j])+"\n";
tempstring = tempstring.concat(x);
}

/**
* TODO Automate the generation of the name so it matches text file name.
* Possibly get month and bookname from file path.
**/
var bookName = prompt("Please enter book name (without extension)", "Bookname_Month");

// Write Data
if(outFolder){
var myCSVFile = new File(outFolder+"/"+bookName+".csv");
myCSVFile.open("w");
myCSVFile.write(s + "\n"+tempstring);
}
}

// Added Function Structure.
function Main(){
// Open Folder and Gather InDesign Files.
var myIndsnFiles = Folder(PATH).getFiles("*.indd");

// Open All InDesign Files.
for(k=0; k < myIndsnFiles.length; k++) {
// Add Check For Hidden Files.
if (String(myIndsnFiles[k]).split('/').reverse()[0].charAt(0) == "."){
continue
} else {
// https://forums.adobe.com/thread/2146874
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.NEVER_INTERACT;
app.open(myIndsnFiles[k]);

if(app.documents.length != 0) {
if(app.activeDocument.links.length!=0) {
// Sets Columns without X-Res and Y-Res.
var doc = app.activeDocument;
var doc_name = doc.name;
var shortdocname = doc_name.split(".indd")[0];
var lks = doc.links;
var link_name;
var style_number; // ADDED
var pg_nb;
var img_x;
var img_y;
var img_width;
var img_height;
var x_scale;
var y_scale;
var lk;
var feedAline;
var lkPath;

// Loop Through Document Links.
for(i=0; i < lks.length; i++) {
lk = lks[i];
link_name = lk.name.replace(",","_");
x_scale = lk.parent.horizontalScale;
y_scale = lk.parent.verticalScale;
lkPath = lk.filePath.split(link_name)[0];
try {
// Set Page Number.
if(lk.parent.parent.parentPage.constructor.name == "Page") {
pg_nb = lk.parent.parent.parentPage.name;
} else {
pg_nb = "N/A";
}
} catch(e) {
pg_nb = "N/A";
}

// Getting Image Coordinates.
var gb = lk.parent.parent.geometricBounds;
img_x = gb[1];
img_y = gb[0];
img_width = Math.round((gb[3]-gb[1])*100) / 100;
img_height = Math.round((gb[2]-gb[0])*100) / 100;

if(i < lks.length-1) {
feedAline = "\n";
} else {
feedAline ="";
}

// Finding character style information on the page
// ADDED
// https://community.adobe.com/t5/indesign-discussions/finding-manually-applied-character-styles/m-p/2890863
app.findTextPreference = null;
app.findTextPreference.appliedCharacterStyle = "[Style Number DASH]"; //ADD CHARACTER STLYE

// If there is no style number on the page with the image, fill cell with "N/A"
try {
if (app.findTextPreference.appliedCharacterStyle == "[Style Number DASH]"){ // ADD CHARACTER STYLE
style_number ==""; // ADD VARIABLE FOR STYLE NUMBER
}else{
style_number == "N/A";
}

}catch(e){
style_number = "N/A";
}
// End of style number additions

// Add Data To Array.
// MADE CHANGE
myarr.push(link_name + "," + style_number + "," + pg_nb + "," + img_x + "," + img_y + "," + img_width + "," + img_height+ "," + x_scale + "," + y_scale + "," + lkPath);
}
} else {
//alert("No pictures to work on!")
continue;
}
} else {
alert("No document open!");
}
// Reset Interaction Levels.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
}
}
}

Main() // Run The Script.
writeToFile(myarr) // Write to File.
//app.quit(SaveOptions.NO);//Quit InDesign.

This topic has been closed for replies.

2 replies

Community Expert
June 28, 2022

Well, on with the next issue.

This line cannot work:

app.findTextPreference = null;

It must be:

app.findTextPreferences = null;

 

The same here:

app.findTextPreference.appliedCharacterStyle = "[Style Number DASH]";

Plus this: InDesign does not allow custom style names enclosed in brackets!

 

So you could try:

app.findTextPreferences.appliedCharacterStyle = "Style Number DASH";

Check that line separately in the GUI where you can see if it is working for you.

Also note that to make this work you need a character style with exactly that name in your active document.

 

FWIW: If you are on Windows install the ExtendScript Toolkit to debug your code.

https://github.com/Adobe-CEP/CEP-Resources/tree/master/ExtendScript-Toolkit

 

How to configure it after you installed it on Windows:

https://extendscript.docsforadobe.dev/extendscript-toolkit/configuring-the-toolkit-window.html

 

This leads you to the DOM documentation:

https://www.indesignjs.de/extendscriptAPI/indesign-latest/#about.html

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Community Expert
June 28, 2022

Hi

this line of code is not functional:

var PATH = "/Users/UserName/Desktop/InDesign/";

 

If you want to target a folder on the current user's desktop with name "InDesign" write it like that:

var PATH = "~/desktop/InDesign";

 

Also test if the target folder exists at all, before you do something with it:

 

var PATH = "~/desktop/InDesign";
var outputFolder = Folder( PATH );

// Test before you are using the folder.

//Folder exists:
if( outputFolder.exists )
{
	// do something.
};

// Folder does NOT exist:
if( !outputFolder.exists )
{
	// do something else
};

 

Regards,
Uwe Laubender
( Adobe Community Professional )

Participating Frequently
June 28, 2022

i have everyting working but the following:

// Finding character style information on the page
// ADDED
// https://community.adobe.com/t5/indesign-discussions/finding-manually-applied-character-styles/m-p/2890863
app.findTextPreference = null;
app.findTextPreference.appliedCharacterStyle = "[Style Number DASH]"; //ADD CHARACTER STLYE

// If there is no style number on the page with the image, fill cell with "N/A"
try {
if (app.findTextPreference.appliedCharacterStyle == "[Style Number DASH]"){ // ADD CHARACTER STYLE
style_number ==""; // ADD VARIABLE FOR STYLE NUMBER
}else{
style_number == "N/A";
}

}catch(e){
style_number = "N/A";
}
// End of style number additions

// Add Data To Array.
// MADE CHANGE
myarr.push(link_name + "," + style_number + "," + pg_nb + "," + img_x + "," + img_y + "," + img_width + "," + img_height+ "," + x_scale + "," + y_scale + "," + lkPath);
}
} else {
//alert("No pictures to work on!")
continue;
}
} else {
alert("No document open!");
}
// Reset Interaction Levels.
app.scriptPreferences.userInteractionLevel = UserInteractionLevels.INTERACT_WITH_ALL;
}
}
}

Main() // Run The Script.
writeToFile(myarr) // Write to File.
//app.quit(SaveOptions.NO);//Quit InDesign.

 

 

Maybe you can help, need to be able to export the Character style an excel document.