Copy link to clipboard
Copied
Open this file...
Document.ai - Google Drive https://drive.google.com/open?id=0B4ebfmBRxh3ATXBMbTUySmtpVWs
Then open this file so it is the active document...
I am running a script on the table I have pasted in from an Excel document. I am storing all of the names in an array.
Then it closes the table document and then searches document for matches.
The problem I am running into is that the table document is all a single line text frame and the document has text frames with carriage returns in them.
Here is my current code (which includes some clean up functions for odd spaces and such)....
#target illustrator
var doc = app.activeDocument;
var allText = doc.textFrames;
var componentName = [];
var theContents;
// Remove spaces that are before, after, or double
function myTrim(x) {
return x.replace(/^\s+|\s+$/gm, '');
}
function removeDoubleSpaces() {
var searchString = / /g;
var replaceString = " ";
for (var i = 0; i < allText.length; i++) {
var thisTextFrame = allText;
var newString = thisTextFrame.contents.replace(searchString, replaceString);
if (newString != thisTextFrame.contents) {
thisTextFrame.contents = newString;
}
}
}
for (i = 0; i < allText.length; i++) {
theContents = allText.contents;
theContents = myTrim(theContents);
allText.contents = theContents;
}
removeDoubleSpaces();
// Read table data
for (var i = allText.length - 1; i >= 0; i--) {
//alert(allText.contents);
componentName.push(allText.contents);
}
//alert(componentName.length);
alert("Your component table has been successfully scanned!");
doc.close(SaveOptions.DONOTSAVECHANGES);
// Format text on Hypertext layer to read (#) Component Name
var doc = app.activeDocument;
var allText = doc.textFrames;
for (i = 0; i < allText.length; i++) {
theContents = allText.contents;
theContents = myTrim(theContents);
allText.contents = theContents;
}
removeDoubleSpaces();
var calloutStartNumber = prompt("Start with callout number...", "1", "Callout Number");
var hyperLayer = doc.layers.getByName("Hypertext");
for (var i = 0; i < componentName.length; i++) {
//alert(componentName.length);
for (var z = 0; z < allText.length; z++) {
//alert(allText.length);
if (componentName == allText
) { alert(componentName + " " + allText
); allText
.contents = "(" + (calloutStartNumber) + ") " + allText .contents; calloutStartNumber++;
}
}
}
alert("Your components have been numbered!");
My desired output is Accumulator Gp - Hydraulic (Charged) is number 1 and then each item after that on the table (that is in alphabetical order) will be numbered sequentially on the document. I don't want the script to change the text box or get rid of the carriage returns....just insert the sequential number in front of it like this....
Copy link to clipboard
Copied
Any ideas?
Copy link to clipboard
Copied
I am guessing some good manners had helped in having your question answered quicker…
var main = function() {
var docs = getDocs(),
tableDoc = docs["Table.ai"],
mainDoc = docs["Document.ai"];
if ( !( tableDoc && mainDoc) ) {
alert("This scripts needs a \"Table.ai\" & \"Document.ai\" documents open !");
return;
}
strings = getStrings ( tableDoc );
if( !strings.length ) {
alert("No strings returned from Table.ai document" );
}
incrementStrings ( mainDoc, strings );
}
function incrementStrings( doc, strings ) {
var tfs = getTFS( doc ),
n = strings.length, i = 0, tf, stripped;
while ( i<n){
stripped = strings.replace (/\W+/g, "")
tf = tfs[stripped];
tf && tf.contents = "("+(i+1)+") "+tf.contents;
i++;
}
}
function getTFS ( doc ) {
var tfs = doc.textFrames, n = tfs.length, db = {}, tf;
while ( n-- ) {
tf = tfs
; db[tf.contents.replace (/\W+/g, "")] = tf;
}
return db;
}
function getStrings ( doc ) {
var tfs = doc.textFrames, n = tfs.length, strings = [];
while ( n-- ) strings.push ( tfs
.contents) ; return strings;
}
function getDocs() {
var docs = app.documents, n = docs.length, db = {length:0};
while ( n-- ) {
doc = docs
; db[doc.name] = doc;
}
return db;
}
main();
Find more inspiration, events, and resources on the new Adobe Community
Explore Now