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

For loop skipping every second text frame

Community Beginner ,
Nov 08, 2021 Nov 08, 2021

Hi all, I've been using Illustrator scripts for years, but thought I would start trying to write some of my own to speed up repetitive tasks. I am trying to write a script that outlines all fonts and outputs a PDF and duplicate AI file. All is working well except my for loop seems to only working on every second text frame. The code below is the section in question. I would love to know if I am doing something wrong here or whether this is a bug. Any suggestions for better ways of doing this would also be appreciated.

var doc = activeDocument;
var frames = doc.textFrames;

for (i = 0; i < frames.length; ++i) {
    frame = frames[i];
    frame.createOutline();
}

  

Cheers,

 

Haniel

TOPICS
Bug , Scripting
501
Translate
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

Community Expert , Nov 08, 2021 Nov 08, 2021

Hi Haniel, whenever you do something to an object that changes it dramatically—in this case you are destroying the textFrame, and replacing with a bunch of path items—the reference you have in frames variable now points to the wrong thing. It is still a reference to "the first textFrame in the document" but now that is the second text frame. So, when i == 0, the operation outlines textFrame 1; when i ==1, the operation outlines textFrame 3 (textFrame 2 is referenced in frame[0] now.

 

The easiest

...
Translate
Adobe
Community Expert ,
Nov 08, 2021 Nov 08, 2021

Hi Haniel, whenever you do something to an object that changes it dramatically—in this case you are destroying the textFrame, and replacing with a bunch of path items—the reference you have in frames variable now points to the wrong thing. It is still a reference to "the first textFrame in the document" but now that is the second text frame. So, when i == 0, the operation outlines textFrame 1; when i ==1, the operation outlines textFrame 3 (textFrame 2 is referenced in frame[0] now.

 

The easiest solution is to do the destructive operation backwards. The references still get messed up but they don't reshuffle all the others.

 

 

var doc = activeDocument;
var frames = doc.textFrames;

for (var i = frames.length - 1; i >= 0; i--) {
    frames[i].createOutline();
}

 

 

 

- Mark

Translate
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 Expert ,
Nov 08, 2021 Nov 08, 2021

I forgot to add: good on you for having a go at your own script! That's awesome!

- Mark

Translate
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 ,
Nov 08, 2021 Nov 08, 2021

Thanks for that Mark, makes perfect sense now that you explain it. Your suggestion worked a treat.

 

Here is my completed script for anyone else that might find it useful. It basically asks for a artwork/item code and issue/version number then creates a pdf and ai file named accordingly, with fonts converted to outlines and images imbedded (just how our production facility wants them).

 

// Set the active document

var doc = activeDocument;

// Get new Part and Issue information

var itemCode = prompt("Enter Item number","");
var newIssueNo = prompt("Insert new Issue Number","");

// Get location of active file

var location = doc.path;

// Compile output names

var artName = itemCode + " ISSUE " + newIssueNo + " ARTWORK";
var pdfName = itemCode + " ISSUE " + newIssueNo + " PROOF";

// Set Output Destinations

var artDest = location + "/" + artName;
var pdfDest = location + "/" + pdfName;

// Set save as AI file function

function exportFileToAI(dest) {
    var ai24Doc = new File(dest);
    var aiExportOpts = new IllustratorSaveOptions();
    aiExportOpts.compatibility = Compatibility.ILLUSTRATOR24;
    aiExportOpts.embedICCProfile = true;
    aiExportOpts.embedLinkedFiles = true;
    doc.saveAs(ai24Doc, aiExportOpts);
}

// Set save as PDF file function

function saveFileToPDF(dest) {
    var pdfDoc = new File(dest);
    pdfSaveOpts = new PDFSaveOptions();
    pdfSaveOpts.compatibility = PDFCompatibility.ACROBAT5;
    pdfSaveOpts.generateThumbnails = true;
    pdfSaveOpts.preserveEditability = true;
    doc.saveAs(pdfDoc, pdfSaveOpts);
}

//Save a low resolution PDF

saveFileToPDF(pdfDest);

// Convert all text to outlines

var frames = doc.textFrames;

for (var i = frames.length - 1; i >= 0; i--) {
    frames[i].createOutline();
}

//Save as Ai File

exportFileToAI(artDest);

//Close file

doc.close();

 

Translate
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 Expert ,
Nov 10, 2021 Nov 10, 2021
LATEST

Nice! Thanks for posting. 🙂

- Mark

Translate
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