• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers

need script to export pages with field from data merge

Community Beginner ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

I am trying to save 800+ certificates from a data merge with the first data cell (full name). I'm new to scripts so I might not have added this correctly. My excel file has the names in the 1st column. This is the script I added but it does not work. I only changed the text in red. These can either be jpg or pdf files.

 

I posted this as a reply to someone else's thread a couple of days ago but have not go any responses. These need to get done, so if I don't receive a response in the next day or so, I'll be spending several hours renaming over 800 files individually.

 

//http://www.scriptopedia.org/index.php?post/custompdfexport.jsx.html
//use some csv row to rename a bunch of pdf files accordingly:

var csv=File(Folder.desktop+"/Webinar_Stats_for_CE_Credits_221201 - Merge File for INDD.csv");

var myPDFFolder = Folder(Folder.desktop+"/pdfs");

var names = getNamesFromCSV(csv);

if(csv.exists && myPDFFolder.exists){
var files = myPDFFolder.getFiles();
if(files.length==names.length){
for(var i=0; i<files.length; i++){
files[i].rename(files[i].parent+"/"+names[i]+".pdf");
}
}
}

function getNamesFromCSV(csv){
if(csv.exists){
csv.open('r');
var content = csv.read();
var namesArr = [];
content=content.split("\n");
for(var j=0; j<content.length; j++){
namesArr.push(content[j].match(/^(.[^\n]+)/i)[1]);
}

csv.close();
return namesArr.slice(1,namesArr.length);
}
return false;
}

 

This is my error message:

TEGDesigner_0-1670435500090.png

 

 

Thanks for any advice.

TOPICS
Import and export , Scripting

Views

282

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Advisor , Dec 07, 2022 Dec 07, 2022

Hello @TEG Designer,

 

Take a look at the link below...The below script will export pdfs named by the contents of the text box/data field. Just follow the directions on how to setup the document.

https://community.adobe.com/t5/indesign-discussions/export-pdf-business-cards-sequentially/m-p/12969036#M478763

 

Regards,

Mike

Likes

Translate

Translate
Community Expert ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

Hi @TEG Designer , I can’t test without the .csv file, but it looks like the JS error happens when you split the contents and there’s a null item included in the split array.

 

Does it work if you add a try statement to the content loop:

 

 

for(var j=0; j<content.length; j++){
    try {
        namesArr.push(content[j].match(/^(.[^\n]+)/i)[1]);
    }catch(e) {}   
}

 

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

Hello @TEG Designer,

 

Take a look at the link below...The below script will export pdfs named by the contents of the text box/data field. Just follow the directions on how to setup the document.

https://community.adobe.com/t5/indesign-discussions/export-pdf-business-cards-sequentially/m-p/12969...

 

Regards,

Mike

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

Thank you!!!! for your responses. After I posted, I continued researching and found this script and it worked.

https://community.adobe.com/t5/indesign-discussions/unique-file-names/m-p/11965769?search-action-id=...

 

I have my individually named PDFs. Can this script be adjusted to export JPGs?

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Valorous Hero ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

Just change the export format enum to ExportFormat.JPG and the file reference so the file extension is jpg.

You will need to setup JPG Export preferences too prior to call the export function:

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

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advisor ,
Dec 07, 2022 Dec 07, 2022

Copy link to clipboard

Copied

LATEST

Hello @TEG Designer,

 

The script below will export the pages as jpgs, you will need to change the name of the assigned Paragraph Style to "JPG_name", also you'll need to set jpg export preferences at lines 151-160 and 238-247 to your specific requirements.
https://www.indesignjs.de/extendscriptAPI/indesign-latest/index.html#JPEGExportPreference.html

var doc = app.documents[0];
   var myResult = {};
   var allStoriesArray = app.documents[0].stories.everyItem().getElements();
   
    for(var n=0; n<allStoriesArray.length; n++){
    var currentStory =  allStoriesArray[n];
    var textContainersLength = currentStory.textContainers.length;
    var lastFrameOfStory = currentStory.textContainers[textContainersLength-1];
    var objToCheck = lastFrameOfStory;
    
    if(lastFrameOfStory.constructor.name == "TextPath")
    objToCheck = lastFrameOfStory.parent
    
    if(currentStory.overflows && objToCheck.itemLayer.visible && objToCheck.parentPage != null)
    myResult[objToCheck.parentPage.name] = objToCheck.parentPage.name;
    }
    
    var pageList = new Array;
    for(var a in myResult)
    pageList.push(a);
        
    pageList.sort(function(a,b) {return a-b});
    
    if(pageList.length > 0){
    alert("Error!\nDocument contains overset text on page(s): \r" + pageList);
    exit();
    }
    
   var mLinks = doc.links; 
   for(var i = 0; i < mLinks.length; i++){ 
   if ((mLinks[i].status == LinkStatus.LINK_MISSING || mLinks[i].status == LinkStatus.LINK_OUT_OF_DATE) && checkIfOnPage(mLinks[i].parent.parent) && mLinks[i].parent.parent.itemLayer.visible == true ){ 
   alert("Error!\nThis document contains links to files that are missing or modified, use the links panel to fix the problem(s) and try again!");  
   exit();
     
          }
     }

function checkIfOnPage(mItem){
    
    var _OldZeroPoint = app.activeDocument.zeroPoint;     
    app.activeDocument.zeroPoint = [0,0];     
    var _Width =app.activeDocument.documentPreferences.pageWidth;     
    var _Height=app.activeDocument.documentPreferences.pageHeight;     
    var _Bounds = mItem.geometricBounds;
    if ((_Bounds[3] < 0)  || (_Bounds[1] > _Width) || (_Bounds[0] > _Height) || (_Bounds[2] < 0)){
        return false     
    }else{   
        return true 
    }
    app.activeDocument.zeroPoint =_OldZeroPoint;     
}

    function myGetPageNames(){
        var myPageNames = new Array;
        for(myCounter = 0; myCounter < app.activeDocument.pages.length; myCounter ++){
            myPageNames.push(app.activeDocument.pages.item(myCounter).name);
      }
        return myPageNames;
    }

       //Function Returns JPG_name for Selected Pages
      function getTextFromPageUsingParagraphStyle(myPage){
     
     if (doc.paragraphStyles.itemByName("JPG_name") == null) {
      alert ('Error!\nThe Paragraph Style "JPG_name" does not exist or is Labeled Incorrectly.');
      exit();
}
          app.findTextPreferences = app.changeTextPreferences = null;   
          app.findTextPreferences.appliedParagraphStyle = "JPG_name";  
  
          var myTextframes = myPage.textFrames;  
          var JPG_name = null;
          
          for (y=0; y < myTextframes.length; y++){
            try {
              var mySearchResult = myTextframes[y].findText();
                if (mySearchResult.length > 0){  
                 JPG_name = mySearchResult[0].contents; 
                   break;  
                  }
             }catch (e){
        }
            } 
  
           return JPG_name;      
  } 

//Dialog/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

var mySelectedFolder = Folder.selectDialog ("Select a export location");
if(mySelectedFolder != null){

}else{
exit();
}

var myDialog = app.dialogs.add({name:"DataMerge jpeg Exporter", canCancel:true});
with (myDialog){
with (dialogColumns.add()){

with (dialogRows.add())
staticTexts.add({staticLabel:"Page Range Options", minWidth:25});
with (borderPanels.add()){
var myradiogroup1 = radiobuttonGroups.add();
with (myradiogroup1){
var myyesradiobutton1 = radiobuttonControls.add({staticLabel:'Export All Pages', checkedState:true});
var mynoradiobutton1 = radiobuttonControls.add({staticLabel:'Export Select Pages'});    
staticTexts.add({staticLabel:"", minWidth:75});  
}
}
}
}
if (myDialog.show() == true){

var SelectedPages = myradiogroup1.selectedButton; 

}else{
}
myDialog.destroy();

//All Pages//////////////////////////////////////////////////////////////////////////////////////////////////

 if (SelectedPages == false){
     
for(var p = 0; p < app.documents[0].pages.length; p++) { 
      var myPageName = app.documents[0].pages[p].name;
         var frames = app.documents[0].pages[p].textFrames;  
         var JPG_name = null;  
      
         for(var i = 0; i < frames.length; i++) { 
          try { 
              if(frames[i].paragraphs[0].appliedParagraphStyle.name == 'JPG_name') {  
                   JPG_name = frames[i].paragraphs[0].contents;  
                   break;  
              }
            }catch (e){
          }  
        }
     
    if (JPG_name == null) { 
    alert ('Error!\nThe Paragraph Style "JPG_name" is not applied to one or more of the pages or is Labeled Incorrectly.');
    exit();
    }
      
      if(JPG_name != null) {  
 
    }    

    // Set JPEG export preferences
  app.jpegExportPreferences.properties = {
    antiAlias: true,
    embedColorProfile: true,
    exportResolution: 150,
    // exportingSpread: true, // Uncomment if spreads
    jpegColorSpace: JpegColorSpaceEnum.rgb,
    jpegExportRange: ExportRangeOrAllPages.exportRange,
    jpegQuality: JPEGOptionsQuality.maximum,
    jpegRenderingStyle: JPEGOptionsFormat.baselineEncoding,
    useDocumentBleeds: false,
    simulateOverprint: false,
    pageString: myPageName // Page(s) to export, must be a string
 }
 
     var jpegFile = File(mySelectedFolder +"/"+ JPG_name + ".jpg");
 
     doc.exportFile(ExportFormat.jpg, jpegFile);       
  }
  alert("Done Exporting jpg's!");
}

//Selected Pages/////////////////////////////////////////////////////////////////////////////////////////////

 if (SelectedPages == true){
     
     for(var p = 0; p < app.documents[0].pages.length; p++) {  
         var frames = app.documents[0].pages[p].textFrames;  
         var JPG_name = null;  
      
         for(var i = 0; i < frames.length; i++) { 
          try { 
              if(frames[i].paragraphs[0].appliedParagraphStyle.name == 'JPG_name') {  
                   JPG_name = frames[i].paragraphs[0].contents;  
                   break;  
              }
            }catch (e){
          }  
        }
     
     if (JPG_name == null) { 
    alert ('Error!\nThe Paragraph Style "JPG_name" is not applied to one or more of the pages or is Labeled Incorrectly.');
    exit();
      }
      if(!JPG_name) continue;
      }
  
   var myPageNames = myGetPageNames();

    var selectPgs = new Window ("dialog", "Select Page#'s To Export");
    var pgGroup = selectPgs.add("group");
    pgGroup.orientation = "column";
    pgGroup.alignChildren = "center";
    pgGroup.add("statictext", undefined, "Export Pages to jpg's");
    var pgsList = pgGroup.add("listbox", [-25, 0, 170, 246], myPageNames, {multiselect: true});
    var selectedpgsList = []; 
    for (var i = 0; i < pgsList.items.length; i++){selectedpgsList[-1] = pgsList.items[i];}
    pgsList.selection = selectedpgsList; 

    var btn = selectPgs.add("group");
    btn.orientation = "row";
    btn.alignment = ["right", "top"];
    var okBtn = btn.add ("button", undefined, "OK", {name:"OK"});
    var cancelBtn = btn.add ("button", undefined, "Cancel", {name:"Cancel"});

    var result = selectPgs.show();
    if(result == 1){
    }  
    else if (result== 2){  
    exit(0);  
    } 
    var pagelist = [];
    if(pgsList.selection == null){
     alert ("Error!\nNo page#'s were selected.")
     exit(); 
     }else{
         
      for (var i=0; i<pgsList.selection.length; i++) {
      pagelist.push(pgsList.selection[i].text);
        }
     }
 
          for (i = 0; i < doc.pages.length; i++) {
            if (String(pagelist).match("\\b"+doc.pages[i].name+"\\b")){ 
            var myPageName = doc.pages[i].name;
            var JPG_name = getTextFromPageUsingParagraphStyle(doc.pages[i]);
          
    // Set JPEG export preferences
  app.jpegExportPreferences.properties = {
    antiAlias: true,
    embedColorProfile: true,
    exportResolution: 150,
    // exportingSpread: true, // Uncomment if spreads
    jpegColorSpace: JpegColorSpaceEnum.rgb,
    jpegExportRange: ExportRangeOrAllPages.exportRange,
    jpegQuality: JPEGOptionsQuality.maximum,
    jpegRenderingStyle: JPEGOptionsFormat.baselineEncoding,
    useDocumentBleeds: false,
    simulateOverprint: false,
    pageString: myPageName
 }
 
 var jpegFile = File(mySelectedFolder +"/"+ JPG_name + ".jpg");
 
         doc.exportFile(ExportFormat.jpg, jpegFile);
         }
      }
      alert("Done Exporting jpg's!");
    }

Regards,

Mike

 

Likes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines