Skip to main content
Participant
April 12, 2024
Question

Indesign scripting - apply PDF Tags to styles

  • April 12, 2024
  • 3 replies
  • 312 views

Hello Team,
Im writing a script where, for an active indesign document, for each paragraph styles we need to add PDF - tags.
Data for parastyles to pdf tags will be supplied from csv file, dynamically.
For example, para style "1hd" will be added as "H1" - PDF tag.
Below is the code, I have tried.

// Define the CSV file path
var csvFilePath =
  "\path\csvFileName.csv";
// Initialize an array to store matching style names
var doc = app.activeDocument;
var matchingStyles = [];
var mappingMessages = [];
var message = "";
var csvRow = "";
var indesignStyle = "";
var pdfTag = "";
var csvRows = "";
var paraStyles = [];
var rowCount = "";
var style;

// Create UI with button to map styles
app.scriptPreferences.userInteractionLevel =
  UserInteractionLevels.interactWithAll;

var dialog = new Window("dialog", "PDF Tag testing");

var mapStylesButton = dialog.add("button", undefined, "Map styles");
mapStylesButton.onClick = function () {
  csvFile = File(csvFilePath);
  if (!csvFile.exists) {
    alert("CSV file not found!");
    return;
  }
  // Read CSV content
  csvFile.open("r");
  csvContent = csvFile.read();
  csvFile.close();
  //  alert("CSV Content "+csvContent);

  csvRows = csvContent.split("\n");
  rowCount = csvRows.length;
  //alert("Number of rows: " + rowCount);

  // Fetch paragraph styles in the active document
  paraStyles = doc.paragraphStyles.everyItem().getElements();
//   alert(
//     "Fetched " + paraStyles.length + " paragraph styles from the document."
//   );

  // Iterate through CSV rows to map styles to PDF tags
  for (var i = 0; i < csvRows.length; i++) {
    csvRow = csvRows[i].split(",");
    indesignStyle = csvRow[0];
    pdfTag = csvRow[1];
    for (var j = 0; j < paraStyles.length; j++) {
      if (paraStyles[j].name === indesignStyle) {
        style = doc.paragraphStyles.itemByName(paraStyles[j].name);
        //alert("Style  "+style.name);
        if (style.isValid) {
          //alert(style.isValid)
          style.styleExportTagMaps.add("PDF", pdfTag, "", "");
        }
        else {
            alert(error)
        }
      }
    }
  }
};
dialog.show();

I was able to get the required values to execute this line.

          style.styleExportTagMaps.add("PDF", pdfTag, "", "");

 But unable to update the tags when checking the Tag panel.
CSV Format :

Column1 | Column2
1hd     | H1
3hd     | H3
  

Can any helpme in this functionality.

This topic has been closed for replies.

3 replies

Anantha Prabu G
Legend
May 6, 2024

Hi @Subscribeid3625304837qp 

#target indesign

if(app.documents.length == 0)
{
alert("Hi User!! Please open the InDesign Document")
exit();
}

var myFile = File.openDialog("Choose a Remap csv file:");
if (!myFile)
{
exit();
}
myFile.open('r');
var myCounter = 0;
while (myFile.eof==false)
{
myCounter++
line=myFile.readln();
line = line.split(",");
//~ line = line.split("\t");
if(myCounter == 1)
{
continue;
}
myFind = line[0];
//alert("findWhat: " + myFind)
myChange = line[1];
//alert("myChange: " + myChange)

var myDoc = app.activeDocument;

var paraStyles = myDoc.paragraphStyles.everyItem().getElements();

while(Styles = paraStyles.pop()) 

{

   if (Styles.name === myFind) {
    Styles.styleExportTagMaps.add("PDF", myChange, "", "");
}       
}
}
Design smarter, faster, and bolder with InDesign scripting.
Community Expert
May 6, 2024

This should work. I have executed a simple one liner like

app.documents[0].paragraphStyles[2].styleExportTagMaps.add("PDF", "H1", "", "");

And it worked fine. Please check if the value of pdfTag is obtained correctly in the script. Your script could see some improvement as well in my opinion. Like you dont need the line

style = doc.paragraphStyles.itemByName(paraStyles[j].name);

You already have the style in paraStyles[j]

If you still can't make it work then share a sample CSV and INDD file to test this.

-Manan

-Manan
Mike Witherell
Community Expert
Community Expert
May 5, 2024

Why pipe bars instead of comma-delimited in your .csv file?

Mike Witherell