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

Look for page items in one specific layer, not in whole document

New Here ,
May 16, 2015 May 16, 2015

Copy link to clipboard

Copied

Hi,

could you please help me to edit this script so it can only look for page items("Date", "Time", "Version", "Ops Component Code") in one specific layer named "Legend" insted of all layers?

/**********************************************************

ADOBE SYSTEMS INCORPORATED

Copyright 2005-2006 Adobe Systems Incorporated

All Rights Reserved

NOTICE:  Adobe permits you to use, modify, and

distribute this file in accordance with the terms

of the Adobe license agreement accompanying it. 

If you have received this file from a source

other than Adobe, then your use, modification,

or distribution of it requires the prior

written permission of Adobe.

*********************************************************/

/** Saves every document open in Illustrator

  as a PDF file in a user specified folder.

*/

// Main Code [Execution of script begins here]

try {

  // uncomment to suppress Illustrator warning dialogs

  // app.userInteractionLevel = UserInteractionLevel.DONTDISPLAYALERTS;

  if (app.documents.length > 0 )

  {

  var options, i, sourceDoc, targetFile,;

  // Get the PDF options to be used

  options = this.getOptions();

  if (options != null)

  {

  sourceDoc = app.activeDocument; // returns the document object

  var fullName = sourceDoc.fullName;

  fullName = fullName.toString();

  var destFolder = fullName.slice(0,fullName.lastIndexOf("/"))

  var dateFound = false;

  var versionFromName = fullName.slice(fullName.lastIndexOf("-")+1, fullName.lastIndexOf("_"));

  var opsFromName = fullName.slice(fullName.lastIndexOf("/")+1, fullName.lastIndexOf("-"));

  var theVersionNumber = null;

  var dateField = null;

  var timeField = null;

  var opsVersionCode = null;

  for(i=0; i<sourceDoc.pageItems.length;i++)

  {

  if (sourceDoc.pageItems.note == "Date")

  {

  dateField = sourceDoc.pageItems;

  }

  if (sourceDoc.pageItems.note == "Time")

  {

  timeField = sourceDoc.pageItems;

  }

  if (sourceDoc.pageItems.note == "Version")

  {

  theVersionNumber = sourceDoc.pageItems.contents;

  }

  if (sourceDoc.pageItems.note == "Ops Component Code")

  {

  opsVersionCode = sourceDoc.pageItems.contents;

  }

  }

  if (theVersionNumber == versionFromName)

  {

  if (opsVersionCode == opsFromName)

  {

  if (dateField == null)

  {

  alert('No tagged date field found. Tag field and try again.')

  }

  else

  {

  dateField.contents = TodayDate()

  timeField.contents = TodayTime()

  OLtargetFile = this.getTargetFile(sourceDoc.name, '.pdf', destFolder);

  sourceDoc.saveAs( OLtargetFile, options)

  // alert( 'Documents saved as PDF' );

  }

  }

  else

  {

  alert('Ops component code in boiler does not match file name or is not tagged. Please correct and try again.')

  }

  }

  else

  {

               

  alert('Version number in boiler does not match file name or is not tagged. Please correct and try again.')

  }

  }

  else

  {

  alert('User aborted')

  }

  }

  else

  {

  throw new Error('There are no document open!');

  }

}

catch(e) {

  alert( e.message, "Script Alert", true);

}

/** Returns the options to be used for the generated files.

  @return PDFSaveOptions object

*/

function getOptions()

{

  // Create the required options object

  var options = new PDFSaveOptions();

  // See PDFSaveOptions in the JavaScript Reference for available options

  options.pDFPreset = "AZ"

  // For example, uncomment to set the compatibility of the generated pdf to Acrobat 7 (PDF 1.6)

  // options.compatibility = PDFCompatibility.ACROBAT7;

  // For example, uncomment to view the pdfs in Acrobat after conversion

  // options.viewAfterSaving = true;

  return options;

}

function abortFunction(){

  modUI = null;

  dlg.hide();

  return null;

  }

/** Returns the file to save or export the document into.

  @param docName the name of the document

  @param ext the extension the file extension to be applied

  @param destFolder the output folder

  @return File object

*/

function getTargetFile(docName, ext, destFolder) {

  var newName = "";

  // if name has no dot (and hence no extension),

  // just append the extension

  if (docName.indexOf('.') < 0) {

  newName = docName + ext;

  } else {

  var dot = docName.lastIndexOf('.');

  newName += docName.substring(0, dot);

  newName += ext;

  }

  // Create the file object to save to

  var myFile = new File( destFolder + '/' + newName );

  // Preflight access rights

  if (myFile.open("w")) {

  myFile.close();

  }

  else {

  throw new Error('Access is denied');

  }

  return myFile;

}

function TodayDate(){

  var Dateformat = "dd mm yyyy";

  nameMonths = true;

   var monthNames = ["January","February","March","April","May","June","July","August","September","October","November","December"];

   var Today = new Date();

   var Day = Today.getDate();

   if(nameMonths == true){

   var Month = monthNames[Today.getMonth()];

   } else {

   var Month = Today.getMonth() + 1;}

   var Year = Today.getYear();

   var PreMon = ((Month < 10) ? "0" : "");

   var PreDay = ((Day < 10) ? "0" : "");

   var Hour = Today.getHours();

   var Min = Today.getMinutes();

   var Sec = Today.getSeconds();

   if(Year < 999) Year += 1900;

   var theDate = Dateformat.replace(/dd/,PreDay+Day);

   theDate = theDate.replace(/mm/,PreMon+Month);

   theDate = theDate.replace(/d/,Day);

   //theDate = theDate.replace(/m/,Month);

   theDate = theDate.replace(/yyyy/,Year);

   theDate = theDate.replace(/yy/,Year.toString().substr(2,2));

   if(Hour==0){

  Hour = "12";

  theDate = theDate.replace(/XX/,"AM");

   }else if(Hour>12){

   Hour = (Hour-12);

   theDate = theDate.replace(/XX/,"PM");

   }else{

   theDate = theDate.replace(/XX/,"AM");

   }

   var preSec = ((Sec < 10) ? "0" : "");

   var preHour = ((Hour < 10) ? "0" : "");

   var preMin = ((Min < 10) ? "0" : "");

   theDate = theDate.replace(/hr/,preHour+Hour);

   theDate = theDate.replace(/Mn/,preMin+Min);

   theDate = theDate.replace(/sc/,preSec+Sec);

   return theDate;

}

function TodayTime(){

  var Dateformat = "hr:Mn";

  nameMonths = false;

   var monthNames = ["January","February","March","April","May","June","July","August","September","October","November","December"];

   var Today = new Date();

   var Day = Today.getDate();

   if(nameMonths == true){

   var Month = monthNames[Today.getMonth()];

   } else {

   var Month = Today.getMonth() + 1;}

   var Year = Today.getYear();

   var PreMon = "";//((Month < 10) ? "0" : "");

   var PreDay = ((Day < 10) ? "0" : "");

   var Hour = Today.getHours();

   var Min = Today.getMinutes();

   var Sec = Today.getSeconds();

   if(Year < 999) Year += 1900;

   var theDate = Dateformat.replace(/dd/,PreDay+Day);

   theDate = theDate.replace(/mm/,PreMon+Month);

   theDate = theDate.replace(/d/,Day);

   theDate = theDate.replace(/m/,Month);

   theDate = theDate.replace(/yyyy/,Year);

   theDate = theDate.replace(/yy/,Year.toString().substr(2,2));

   if(Hour==0){

  Hour = "12";

  theDate = theDate.replace(/XX/);

   }else{

   theDate = theDate.replace(/XX/);

   }

   var preSec = ((Sec < 10) ? "0" : "");

   var preHour = ((Hour < 10) ? "0" : "");

   var preMin = ((Min < 10) ? "0" : "");

   theDate = theDate.replace(/hr/,preHour+Hour);

   theDate = theDate.replace(/Mn/,preMin+Min);

   theDate = theDate.replace(/sc/,preSec+Sec);

   return theDate;

}

TOPICS
Scripting

Views

521

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

Community Expert , May 16, 2015 May 16, 2015

you can target the layer to be searched, add a line right before you loop pageitems, then edit the new target in your loop

  var targetLayer = sourceDoc.layers['Legend']; // ** added

  for(i=0; i<targetLayer.pageItems.length;i++)  // ** edited

  {

  if (targetLayer.pageItems.note == "Date")  // ** edited

  { 

  dateField = targetLayer.pageItems;  // ** edited

Votes

Translate

Translate
Adobe
Engaged ,
May 16, 2015 May 16, 2015

Copy link to clipboard

Copied

Hi!

Hopefully you know a little about JS to complete this task. I am short on time and kinda hit this on the fly, but thought that it might help you if there is no one else around.

First, locate the beginning of the main for/next loop which iterates through all the page items:

for(i=0; i<sourceDoc.pageItems.length;i++) {  ...  }

Under that line, insert this line of code - but you'll have to insert the remaining for/next loop code (excluding the last brace of course for the for/next loop).

If sourceDoc.pageItems.layer == "Legend" {  ...insert remaining loop code here...   }

Hopefully this will make some sense to you. If not, maybe someone else  has the time to finish it up for you.

Hope this helps!

-TT

Votes

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
New Here ,
May 16, 2015 May 16, 2015

Copy link to clipboard

Copied

Thank you for quick reply, unfortunately I'm absolute beginner in JS

I did this:

for(i=0; i<sourceDoc.pageItems.length;i++)

  if (sourceDoc.pageItems.layer == "Legend")

   {

    if (sourceDoc.pageItems.note == "Date")

     {

    dateField = sourceDoc.pageItems;

     }

    if (sourceDoc.pageItems.note == "Time")

     {

    timeField = sourceDoc.pageItems;

     }

    if (sourceDoc.pageItems.note == "Version")

     {

    theVersionNumber = sourceDoc.pageItems.contents;

     }

    if (sourceDoc.pageItems.note == "Ops Component Code")

     {

    opsVersionCode = sourceDoc.pageItems.contents;

     }

   }

but I'm getting error (Version number in boiler does not match file name or is not tagged. Please correct and try again.)

Votes

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 Expert ,
May 16, 2015 May 16, 2015

Copy link to clipboard

Copied

you can target the layer to be searched, add a line right before you loop pageitems, then edit the new target in your loop

  var targetLayer = sourceDoc.layers['Legend']; // ** added

  for(i=0; i<targetLayer.pageItems.length;i++)  // ** edited

  {

  if (targetLayer.pageItems.note == "Date")  // ** edited

  { 

  dateField = targetLayer.pageItems;  // ** edited

Votes

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
New Here ,
May 16, 2015 May 16, 2015

Copy link to clipboard

Copied

Thank you! works crazy fast now:)

I really appreciate your help.

Votes

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
Engaged ,
May 16, 2015 May 16, 2015

Copy link to clipboard

Copied

You have to get up pretty early to beat Carlos!   Nice work Carlos.

Fully realizing that I am ignoring the two cardinal rules (#1. NEVER do anything in a hurry [proof of which I guess was my first reply to the OP - although I still am not sure why it didn't work in the first place] and #2. Be very, VERY careful when you challenge Carlos [which I'm not actually doing, but I am curious]) I just have a quick question...

What am I missing, although the OP seems satisfied, in thinking that more references to

sourceDoc.pageItems...

would have been impacted and should have been changed -- not just the one line?

if (targetLayer.pageItems.note == "Date"// ** edited

Like "Time", "Version", "Ops Component Code"?  I know that this must have been thought out but I am curious since I know that I am probably missing something...

Votes

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 Expert ,
May 16, 2015 May 16, 2015

Copy link to clipboard

Copied

Hi TT hahaha amusing post...your suggestion should work but I guess the files have a gazillion text frames in multiple layers, and the OP wanted to avoid going through all of them.

and you're correct, I just changed one set, I left the other changes to the OP

Votes

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
Engaged ,
May 17, 2015 May 17, 2015

Copy link to clipboard

Copied

Ok cool. I was afraid that I was losing my mind – at least what little is left of it! Ha! Your way was much cleaner, now that I understand about the additional changes needed.

Thanks, Carlos!

PS: Congrats on the very well-deserved MVP. It was a long time coming, my friend!

Votes

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 Expert ,
May 17, 2015 May 17, 2015

Copy link to clipboard

Copied

LATEST

thanks, my friend!

Votes

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