Skip to main content
Known Participant
May 14, 2013
Answered

Saving documents via Scripts: Get current file name and set save path/file type

  • May 14, 2013
  • 1 reply
  • 8652 views

I am writing a script that will:

-Make all layers invisible

-Make a layer named "background" visible

-Delete all the invisible layers

-Save the docment as an EPS file (leaving the original document untouched)

I am new to scripts and so have based my script by copying code from other scripts.

Here is my code:

var doc = app.activeDocument;

var name = doc.name;

var hide = function (){ // hide all layers (based on http://forums.adobe.com/thread/644267)

     var L=doc.layers.length;

     for (j=0;j<L;j++){  doc.layers.visible=false; }

}

hide();

// loop through all layers

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

              // Create the illusrtratorSaveOptions object to set the AI options

    var saveOpts = new IllustratorSaveOptions();

    // Setting IllustratorSaveOptions properties.

    saveOpts.embedLinkedFiles = true;

    saveOpts.fontSubsetThreshold = 0.0

    saveOpts.pdfCompatible = true

//Set up Variable to access layer name

          var currentLayer = app.activeDocument.layers;

// Loop through the layers and make the back ground layer visible

          if (currentLayer.name == "Background") {

                    docName = name + currentLayer.name+".eps";

                    currentLayer.visible = true;

                    }

}

// Delete Invisible Layers (based on http://www.cartotalk.com/index.php?showtopic=7491)

var myDoc=app.activeDocument;

var layerCount=myDoc.layers.length;

for (var ii = layerCount - 1; ii >= 0; ii--) {

    var currentLayer = myDoc.layers[ii];

    currentLayer.locked = false;

    var subCount = currentLayer.layers.length;

    for (var ss =subCount -1; ss >= 0; ss--){

        var subLayer = currentLayer.layers[ss];

        subLayer.locked = false;

        if (subLayer.visible == false){

            subLayer.visible = true;

            subLayer.remove();

            }

        }

    if (currentLayer.visible == false){

        currentLayer.visible = true;

        currentLayer.remove();

        }

    }

// Save Out Document with New Name

var saveName = new File ( doc.path + "/" + docName );

doc.saveAs( saveName, saveOpts );

Everything works fine except:

1) It saves the document with the name AdobeIllustratorBackground as opposed to the name of the document

Also, I am not sure how to tell the script to save as EPS and to specify the save location.

Could some one give me some pointers?          Thanks!

This topic has been closed for replies.
Correct answer Larry G. Schneider

There's a sample script in the Adobe Illustrator CSx/Scripting/Sample Scripts/JavaScripts/Miscellaneous/Save as PDF which has all the parts except the EPS Options.

Go here for the Scripting Guides and References.

http://www.adobe.com/devnet/illustrator/scripting.html

1 reply

Inspiring
May 14, 2013

Im currently at work ( no time to test) but I wound NOT use name as a variable… It looks like you are getting a clash… Change to docName or some other… name is a standard property of laods of objects including the app…

big_smileAuthor
Known Participant
May 14, 2013

Thanks! I changed my code to this:

var title = doc.name;

var title = title.substring(0, title.length - 3);

And it now works! (The substring thing removes the .ai extenion).

However, how do I get it to save it as an EPS? (I found this script that  saves as PNG, but I am not sure how to adapt it to EPS).

Also, how do I set the file output path?

Thanks for any help that can be offered.

Larry G. Schneider
Community Expert
Larry G. SchneiderCommunity ExpertCorrect answer
Community Expert
May 14, 2013

There's a sample script in the Adobe Illustrator CSx/Scripting/Sample Scripts/JavaScripts/Miscellaneous/Save as PDF which has all the parts except the EPS Options.

Go here for the Scripting Guides and References.

http://www.adobe.com/devnet/illustrator/scripting.html