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

Need help writing a script that opens files, deletes layers and sublayers

New Here ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

Hey all,

I'm a noob to the scripting community, and what I'm trying to do is batch edit files that all share the same layer names and sublayers. I have a few questions that would be amazingly helpful if I could get answered.

I'm unsure of how to set up a script too call out layers that were created without a script. Do I need to create a variable for the existing layers and let the script know what their names are in the files?

For example, I have three files that each have layers named "cutlayer", "trimlayer", "crease" and "artwork". I need to write a script that opens the three files, deletes the "crease" layer from each file, then moves the layer "trimlayer" into its own sublayer in "artwork" (the sublayer can be on top so re-ordering the sublayers isn't a necessary thing to worry about). I don't understand how to write the script because I couldn't find any examples in the scripting documentation provided on the adobe website that worked with pre-existing files.

This is my shot at what I'm trying to do, with English filling in the blanks of what I don't know how to script. If anybody can help me figure this out, I'd really appreciate it!

app.documents.openDlg (prompt, filter, multiSelect);  //user opens all associated files

if (documents.length > 0) //run the script through each document individually

{

     sourceDoc = activeDocument;

     //delete sourceDoc's "crease" layer

     //move "trimlayer" into "artwork" layer

     //delete "path" sublayer from "artwork" layer

     //run script through each document

     //save as new file name in new folder parallel to the current folder on hard drive

     //exit and run through each document until there are no more

}

//create new document

//place the edited documents in different layers in the new document, with a given distance between them, optimizing space on a 51"x51" artboard, using as little of the artboard as possible.

All the comments are things I basically do not know how to do and am currently trying to figure out how to do, so if anybody has a second to explain some of these things, I'd really appreciate it!

Thanks again!

TOPICS
Scripting

Views

5.5K

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 , Sep 14, 2011 Sep 14, 2011

I know it is very frustrating when one's learning and things don't go as expected, but understand that it is confusing enough as it is and you don't have to add more confusion to the mix by using other tools than those provided. I mean JSLint, it might help with GENERAL javascript sintax, but Adobe's Javascript implementation is another animal, or another language, still javascript but meant to be run by its own Engine. JSLint has no idea what "layers", or "pathItems" are, so it will throw error

...

Votes

Translate

Translate
Adobe
Guru ,
Sep 02, 2011 Sep 02, 2011

Copy link to clipboard

Copied

This should give you a start with an open document… If all works then you need to add the save to location… If that works put in a loop for all files…

#target illustrator var doc = app.activeDocument; // Removes a top level layer doc.layers.getByName('crease').remove(); // Removes a sub level layer doc.layers.getByName('artwork').layers.getByName('path').remove(); // Moves a top level layer to sub layer doc.layers.getByName('trimlayer').move(doc.layers.getByName('artwork'),ElementPlacement.INSIDE);

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 ,
Sep 06, 2011 Sep 06, 2011

Copy link to clipboard

Copied

Some of that was extremely helpful! Here is what I got so far:

#target Illustrator

//open the files to edit

app.documents.openDlg (prompt, filter, multiSelect); // I couldn't get the openDlg to work, am I supposed to fill in something for "prompt, filter, and multiSelect"?

//if(documents.length>0)  // I'm also unsure of how to write the for loop so that the script runs for all files opened by the openDlg function

//{

    var doc=app.activeDocument;

    var myLayerCount = doc.layers.length

//the layers are locked when the files are open, unlock all layers:

    for(var myCounter = 0; myCounter<myLayerCount; myCounter++)

        {var myLayer = doc.layers[myCounter]

            myLayer.locked = false}

    doc.layers.getByName('Crease').remove();

    doc.layers.getByName('Regmark').remove();

    doc.layers.getByName('Artwork').layers.getByName('<Path>').remove(); 

// This is not working for deleting the sublayer. The sublayer is named "<Path>" and the error I'm getting reads "No such element"

    doc.layers.getByName('Through Cut').hasSelectedArtwork; //trying to select all in the "Through Cut" layer?

    docSelection = doc.selection;

    doc.selection.strokeColor = 100.0,0.0,0.0,0.0; //Not sure how to change the stroke color to black? What I wrote is definitely not working.

    doc.layers.getByName('Through Cut').move(doc.layers.getByName('Artwork'),ElementPlacement.INSIDE);

    doc.save();

If anybody can help me figure out the rest of this script, I'd really appreciate it! Thanks so much for your help thus far, Muppet Mark.

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
Enthusiast ,
Sep 07, 2011 Sep 07, 2011

Copy link to clipboard

Copied

Q 1:File.openDlg (prompt, filter, multiSelect)  V.S app.open(document), you should use the former.

Q 2: https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Statements#for_Statement

Q 3: "<Path>" item is not sublayer but pathItem, and "<Path>" is not name at all, it's type definition name.

Q 4: You should set .hasSelectedArtwork property to true: doc.layers.getByName('Through Cut').hasSelectedArtwork = true; But in script you do not need to select item in order to change it.

Q 5: You have to change color one by one(using another for loop), eg: thisItem.strokeColor = doc.swatches.getByName("Black").color;

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 ,
Sep 08, 2011 Sep 08, 2011

Copy link to clipboard

Copied

Ok, getting closer but no cigar. The openDlg function still won't work and neither will changing the stroke color to black for the item in the layer Through Cut.

Here's what I've got so far:

#target Illustrator

var documents = File.openDlg ("Select Files to Open", "*.indd", true); //This line keeps throwing back the error: "File.OpenDlg is not a function". Also, If I write in "File.DOCUMENT.openDlg or even app.document.openDlg, it gives me the error "undefined is not an object". Still not sure what I'm doing wrong here.

for (var idx = 0, len = documents.length; idx < len; ++idx)

    var doc=app.activeDocument;

    var myLayerCount = doc.layers.length

    for(var myCounter = 0; myCounter<myLayerCount; myCounter++)

        {var myLayer = doc.layers[myCounter]

            myLayer.locked = false}

    doc.layers.getByName('Crease').remove();

    doc.layers.getByName('Regmark').remove();

  //  doc.layers.getByName('Artwork').layers.getByName('<Path>').remove();  //Moluapple said something about <Path> being a pathItem and not actually the name of the layer, but in that case I do not know or understand how to call out specific pathItems or what a "type definition name" is. Does anybody know how to call out the pathItem to remove it?

    doc.layers.getByName('Through Cut').hasSelectedArtwork=true;   

  //  doc.selection=doc.swatches.getByName("Black").color;  //This kept making the program crash so obviously it isn't written correctly? This is trying to do the same thing I'm trying to do in the following line, both of which are sadly failing.

    doc.layers.getByName('Through Cut').pathItems.strokeColor = doc.swatches.getByName("Black").color;  //This still isn't changing the stroke color to Black, not sure how to do it differently...

    doc.layers.getByName('Through Cut').move(doc.layers.getByName('Artwork'),ElementPlacement.INSIDE);

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 ,
Sep 08, 2011 Sep 08, 2011

Copy link to clipboard

Copied

// var documents = File.openDlg ("Select Files to Open", "*.indd", true);

use

var documents = File.openDialog ("Select Files to Open", "*.indd", true);

//  doc.layers.getByName('Artwork').layers.getByName('<Path>').remove();

if it shows '<path>' the pathItem has no name, if the path item is the top most you can use

doc.layers.getByName ('Artwork').pathItems[0].remove ();

// doc.layers.getByName('Through Cut').pathItems.strokeColor = doc.swatches.getByName("Black").color;

pathItems is "all" pathItems, you need to work with one at the time, again, if the pathItem is the topmost use pathItems[0]

doc.layers.getByName('Through Cut').pathItems[0].strokeColor = doc.swatches.getByName("Black").color;

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 ,
Sep 08, 2011 Sep 08, 2011

Copy link to clipboard

Copied

Why are you trying to open InDesign files in Illustrator. If you are opening AI files, then the filter should be "*.ai" and not "*.indd".

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
Enthusiast ,
Sep 08, 2011 Sep 08, 2011

Copy link to clipboard

Copied

File.openDlg (prompt, filter, multiSelect)

To be Clear, In this line "File" means a File object, it specify which folder will bring up to you when the dialog open, eg:

var documents = File("~/Desktop").openDlg ("Select Files to Open", "*.ai", true);
will always open with the desktop folder.

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 ,
Sep 13, 2011 Sep 13, 2011

Copy link to clipboard

Copied

So after doing some cleanip, my code now looks like this: (I numbered the lines to make it easier to reference, they are not part of the code)

1   var documents = File.openDialog("Select Files to Open", "*.indd", true);

2   var idx = 0;

3   for (idx = 0; idx < documents.length; ++idx) {

4       var doc = app.activeDocument;

5   var myCounter = 0;

6       for (myCounter = 0; myCounter < doc.layers.length; myCounter++) {

7           doc.layers[myCounter].locked = false;

8       }

9         doc.layers.getByName('Crease').remove();

10       doc.layers.getByName('Regmark').remove();

11       doc.layers.getByName('Artwork').pathItems[0].remove();

12       doc.layers.getByName('Through Cut').hasSelectedArtwork = true;   

13       doc.layers.getByName('Through Cut').pathItems[0].strokeColor = doc.swatches.getByName("Black").color;   

14       doc.layers.getByName('Through Cut').move(doc.layers.getByName('Artwork'), ElementPlacement.INSIDE);

15   }

And JSLint is telling me that a bunch of things aren't defined, which coincides with what ESTK is telling me in the Javascript Console window (it basically says "undefined, undefined, undefined...true, [CMYKcolor], undefined undefined..."). Here is JSLint's summary of what I'm doing wrong:
Error:

Problem at line 1 character 17: 'File' was used before it was defined.

var documents = File.openDialog("Select Files to Open", "*.indd", true);

Problem at line 3 character 39: Unexpected '++'.

for (idx = 0; idx < documents.length; ++idx) {

Problem at line 4 character 15: 'app' was used before it was defined.

var doc = app.activeDocument;

Problem at line 5 character 1: Expected 'var' at column 5, not column 1.

var myCounter = 0;

Problem at line 6 character 65: Unexpected '++'.

for (myCounter = 0; myCounter < doc.layers.length; myCounter++) {

Problem at line 15 character 79: 'ElementPlacement' was used before it was defined.

doc.layers.getByName('Through Cut').move(doc.layers.getByName('Artwork'),...

I thought maybe it didn't know Illustrator's library but even if I write #target Illustrator at the top, it doesn't make the code work so something else is clearly wrong with the way I'm defining things.

Basically, when I run the file as it is, a dialog box open where I choose which files I want to edit, but then the script finishes and just ends, never actually opening the files or editing them from what I can tell.

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 ,
Sep 13, 2011 Sep 13, 2011

Copy link to clipboard

Copied

Again I ask

Why are you trying to open INDESIGN FILES in Illustrator. The correct file type for Illustrator is .ai not .indd. In the first line the filter should be *.ai, not *.indd.

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 ,
Sep 14, 2011 Sep 14, 2011

Copy link to clipboard

Copied

My apologies, I definitely changed that when you mentioned it before, I'm not sure how it got changed back, I must've used an older file or something. That still doesn't help my current issues, however.

This is my current code:

var docs = File.openDialog("Select Files to Open", "*.ai", true);

var idx = 0;

for (idx = 0; idx < documents.length; idx) {

    var doc = app.open(docs[idx]);

    var myCounter = 0;

    for (myCounter = 0; myCounter < doc.layers.length; myCounter) {

        doc.layers[myCounter].locked = false;

    }

    doc.layers.getByName('Crease').remove();

    doc.layers.getByName('Regmark').remove();

    doc.layers.getByName('Artwork').pathItems[0].remove();

    doc.layers.getByName('Through Cut').hasSelectedArtwork = true;   

    doc.layers.getByName('Through Cut').pathItems[0].strokeColor = doc.swatches.getByName("Black").color;   

    doc.layers.getByName('Through Cut').move(doc.layers.getByName('Artwork'), ElementPlacement.INSIDE);

}

The only issues that JSLint seems to be having when I run it there are basically that things aren't defined:
Error:

Problem at line 1 character 12: 'File' was used before it was defined.

     var docs = File.openDialog("Select Files to Open", "*.ai", true);

Problem at line 3 character 21: 'documents' was used before it was defined.

     for (idx = 0; idx < documents.length; idx) {

Problem at line 4 character 15: 'app' was used before it was defined.

     var doc = app.open(docs[idx]);

Problem at line 15 character 79: 'ElementPlacement' was used before it was defined.

     doc.layers.getByName('Through Cut').move(doc.layers.getByName('Artwork'), ElementPlacement.INSIDE);

This is what I was trying to convey in my previous message, I'm not sure how I'm supposed to define these things.

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 ,
Sep 14, 2011 Sep 14, 2011

Copy link to clipboard

Copied

again

stick to the ESTK, not sure what is it you want to accomplish with JSLint, but it is not needed.

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 ,
Sep 14, 2011 Sep 14, 2011

Copy link to clipboard

Copied

I was only using JSLint because it seemed to give me an explanation for errors where I couldn't find such explanations in ESK. I'm only using it to check my syntax and try to figure out why it's not working in ESTK. I am actually trying to run it in ESTK, those are just the errors that JSLint is giving me.

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 ,
Sep 14, 2011 Sep 14, 2011

Copy link to clipboard

Copied

I know it is very frustrating when one's learning and things don't go as expected, but understand that it is confusing enough as it is and you don't have to add more confusion to the mix by using other tools than those provided. I mean JSLint, it might help with GENERAL javascript sintax, but Adobe's Javascript implementation is another animal, or another language, still javascript but meant to be run by its own Engine. JSLint has no idea what "layers", or "pathItems" are, so it will throw errors when it sees objects it doesn't recognize.

...when learning...take baby steps, write one line of code and run....if it works, write another line of code

try this version (always in the ESTK), you were missing a couple of '++', and you didn't change 'documents' with 'docs' in some place

var docs = File.openDialog("Select Files to Open", "*.ai", true);

var idx = 0;

for (idx = 0; idx < docs.length; ++idx) {

    var doc = app.open(docs[idx]);

    var myCounter = 0;

    for (myCounter = 0; myCounter < doc.layers.length; myCounter++) {

        doc.layers[myCounter].locked = false;

    }



    doc.layers.getByName('Crease').remove();

    doc.layers.getByName('Regmark').remove();

    doc.layers.getByName('Artwork').pathItems[0].remove();

    doc.layers.getByName('Through Cut').hasSelectedArtwork = true;  

    doc.layers.getByName('Through Cut').pathItems[0].strokeColor = doc.swatches.getByName("Black").color;  

    doc.layers.getByName('Through Cut').move(doc.layers.getByName('Artwork'), ElementPlacement.INSIDE);

}

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 ,
Sep 21, 2011 Sep 21, 2011

Copy link to clipboard

Copied

Thanks for all of your syntax help! It finally works. (thanks to everybody else too). I just bought an Adobe scripting book so hopefully I can read up on that and become more efficient and knowledgeable over time

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 ,
Sep 21, 2011 Sep 21, 2011

Copy link to clipboard

Copied

what's the name of the book? I think there is only two old books out there

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 ,
Sep 25, 2016 Sep 25, 2016

Copy link to clipboard

Copied

LATEST

Please can you make me this script to run on folder automatically and save files please I'm very need for it, Thanks thanks very very match advance.

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 ,
Sep 13, 2011 Sep 13, 2011

Copy link to clipboard

Copied

to start, or continue...please try to answer the questions you're asked, it'll be easier to help you out. I will assume you want to open AI files not indesign files.

next, stick to the ESTK, not sure what is it you want to accomplish with JSLint, but it is not needed.

documents is a reserved word, so change it to something else, docs for example.

line 1

var docs = File.openDialog("Select Files to Open", "*.ai", true);

line 3

for (idx = 0; idx < docs.length; ++idx) {

change line 4 to

var doc = app.open (docs[idx]); // open documents;

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