Skip to main content
Participating Frequently
October 13, 2022
Answered

"save as copy" scripting

  • October 13, 2022
  • 4 replies
  • 1790 views

I have a simple script that saves my current document as a low resolution PDF into a specific folder.

Only issue is after using this script (or even saving manually) the current open document in Illustrator becomes this saved PDF document. I want to essentially save as copy.

I cant any documentation of the scripting of Save as copy.

How could I edit this script to Save as Copy, not Save as.

#target illustrator-23
// The active document
var doc = app.activeDocument;
// The path of the original document
var originalDocPath = doc.path;
// The name of the original document
var originalDocName = doc.name;
// Get just the file name. Ignore the file extension .pdf and .ai
originalDocName = originalDocName.replace(/\.pdf|\.ai/gi, "")


// Save as Low Res + create folder if it doesnt exist
var path = originalDocPath.path;
    var LOWRES = Folder(originalDocPath + "/Low Res");
    if(!LOWRES.exists){
        LOWRES.create();
    }

// Setup pdf save options
var LOWRESopts = new PDFSaveOptions();
// Use the preset named Smallest File Size
LOWRESopts.pDFPreset = "[Smallest File Size]";
// Save the active document as PDF size and_LR
doc.saveAs(File(LOWRES + "/" + originalDocName + "_LR.pdf"),LOWRESopts,);

 Thank you.

This topic has been closed for replies.
Correct answer Charu Rajput

@Kyle26205577rhrw,
You can use the action like below.

var doc = app.activeDocument;
var originalDocPath = doc.path;
var originalDocName = doc.name;
originalDocName = originalDocName.replace(/\.pdf|\.ai/gi, "")
var path = originalDocPath.path;
var LOWRES = Folder(originalDocPath + "/Low Res");
if (!LOWRES.exists) {
    LOWRES.create();
}
var _pdfFileName = LOWRES.fsName + "/" + originalDocName + "_LR.pdf";
var dest = Folder(LOWRES);

function saveAsCopy() {

    String.prototype.hexEncode = function () {
        var hex = "";
        for (var i = 0; i < this.length; i++) {
            hex += "" + this.charCodeAt(i).toString(16);
        }
        return hex;
    };


    function writeFile(fileDestStr, contents) {
        var newFile = File(fileDestStr);
        newFile.open("w");
        newFile.write(contents);
        newFile.close();
    };


    var actionStr = [
        "/version 3",
        "/name [ 5",
        "	5365742031",
        "]",
        "/isOpen 1",
        "/actionCount 1",
        "/action-1 {",
        "/name [ 8",
        "416374696f6e2031",
        "	]",
        "/keyIndex 0",
        "/colorIndex 0",
        "/isOpen 0",
        "/eventCount 1",
        "/event-1 {",
        "/useRulersIn1stQuadrant 0",
        "/internalName (adobe_saveACopyAs)",
        "/localizedName [ 11",
        "	53617665204120436f7079",
        "]",
        "/isOpen 1",
        "/isOn 1",
        "/hasDialog 1",
        "/showDialog 0",
        "/parameterCount 18",
        "/parameter-1 {",
        "/key 1869640814",
        "/showInPalette 4294967295",
        "/type (integer)",
        "/value 2",
        "}",
        "/parameter-2 {",
        "/key 1987077222",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-3 {",
        "/key 1970500209",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-4 {",
        "/key 1970499699",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ 0", "", "]",
        "}",
        "/parameter-5 {",
        "/key 1836282481",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-6 {",
        "/key 1836281971",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ 0",
        "",
        "	]",
        "}",
        "/parameter-7 {",
        "/key 1886220402",
        "/showInPalette 4294967295",
        "/type (enumerated)",
        "/name [ 16",
        "5072696e74696e6720416c6c6f776564",
        "	]",
        "/value 3",
        "}",
        "/parameter-8 {",
        "/key 1886217064",
        "/showInPalette 4294967295",
        "/type (enumerated)",
        "/name [ 15",
        "4368616e67657320416c6c6f776564",
        "	]",
        "/value 5",
        "}",
        "/parameter-9 {",
        "/key 1700946800",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 1",
        "}",
        "/parameter-10 {",
        "/key 1700946275",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 1",
        "}",
        "/parameter-11 {",
        "/key 1700946785",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 1",
        "}",
        "/parameter-12 {",
        "/key 1700950132",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-13 {",
        "/key 1851878757",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ PDF_PATH_LENGTH", "PDF_FULL_PATH",
        "]",
        "}",
        "/parameter-14 {",
        "/key 1718775156",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ 15",
        "5044462046696c6520466f726d6174",
        "	]",
        "}",
        "/parameter-15 {",
        "/key 1702392942",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ 3",
        "706466",
        "	]",
        "}",
        "/parameter-16 {",
        "/key 1936548194",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-17 {",
        "/key 1935764588",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 1",
        "}",
        "/parameter-18 {",
        "/key 1936875886",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ 0",
        "",
        "	]",
        "}",
        "	}",
        "}"
    ].join("\n");

    if (app.documents.length == 0) {
        return;
    }

    var actionFileDestStr = Folder.desktop + "/MyAction.aia";
    writeFile(actionFileDestStr, actionStr.replace("PDF_PATH_LENGTH", _pdfFileName.length).replace("PDF_FULL_PATH", _pdfFileName.hexEncode()));
    var actionFile = File(actionFileDestStr);
    app.loadAction(actionFile);
    app.doScript("Action 1", "Set 1");


    //clean up 
    actionFile.remove();
    app.unloadAction("Set 1", "");
};

saveAsCopy();

 

4 replies

Participant
December 1, 2024

Hi All!

May be it will be useful for you. If you needs to your active document stays *.ai and not comes *.pdf after saveAs be like save a copy JUST USE THIS: 

 
var artboards = activeDoc.artboards;
var artboardRangeString = '1-' + artboards.length;
pdfSaveOptions.artboardRange = artboardRangeString;
activeDoc.saveAs(outputFile, pdfSaveOptions);
 
its working...
best regards,
forever yours Rickchers.
Legend
October 13, 2022

exportForScreens allows you to export a pdf with copying. The sample code can be found at the following link.

https://sttk3.com/blog/tips/illustrator/export-pdf.html

CarlosCanto
Community Expert
Community Expert
October 13, 2022

there isn't a documented way to Save a Copy but I remember someone posted a solution I just can't find it.

 

I did find what I posted 11 years ago though 

https://community.adobe.com/t5/illustrator-discussions/quot-save-as-copy-quot/td-p/3619332

 

I would probably tackle Save A copy via Action with a fixed name, then I would use the File Object to rename the file or move it to the right folder

 

Another option would be Saving the active file, getting its Full Path, then making a copy of it using the File Object

Charu Rajput
Community Expert
Community Expert
October 13, 2022

Hi, @Kyle26205577rhrw 

You can achieve this via closing the pdf document and opening the original file again. Here is the new version of your script.

var doc = app.activeDocument;
// The path of the original document
var originalDocPath = doc.path;
var _originalFile = doc.fullName;
// The name of the original document
var originalDocName = doc.name;
// Get just the file name. Ignore the file extension .pdf and .ai
originalDocName = originalDocName.replace(/\.pdf|\.ai/gi, "")


// Save as Low Res + create folder if it doesnt exist
var path = originalDocPath.path;
    var LOWRES = Folder(originalDocPath + "/Low Res");
    if(!LOWRES.exists){
        LOWRES.create();
    }

// Setup pdf save options
var LOWRESopts = new PDFSaveOptions();
// Use the preset named Smallest File Size
LOWRESopts.pDFPreset = "[Smallest File Size]";
// Save the active document as PDF size and_LR
doc.saveAs(File(LOWRES + "/" + originalDocName + "_LR.pdf"),LOWRESopts,);
doc.close();
app.open(File(_originalFile));

There is an option using app.executeMenuCommand, but in that case, that will open the saveacopy dialog

app.executeMenuCommand('saveacopy');

 

Or last, you can use the dynamic actions to save as a copy.

 

Best regards
Participating Frequently
October 13, 2022

Hi @Charu Rajput
Thats amazing but sometimes my documents take 20-30 seconds to open. I was hoping to be able to save a copy in the background. Any ideas?

Thanks again

Charu Rajput
Community Expert
Charu RajputCommunity ExpertCorrect answer
Community Expert
October 13, 2022

@Kyle26205577rhrw,
You can use the action like below.

var doc = app.activeDocument;
var originalDocPath = doc.path;
var originalDocName = doc.name;
originalDocName = originalDocName.replace(/\.pdf|\.ai/gi, "")
var path = originalDocPath.path;
var LOWRES = Folder(originalDocPath + "/Low Res");
if (!LOWRES.exists) {
    LOWRES.create();
}
var _pdfFileName = LOWRES.fsName + "/" + originalDocName + "_LR.pdf";
var dest = Folder(LOWRES);

function saveAsCopy() {

    String.prototype.hexEncode = function () {
        var hex = "";
        for (var i = 0; i < this.length; i++) {
            hex += "" + this.charCodeAt(i).toString(16);
        }
        return hex;
    };


    function writeFile(fileDestStr, contents) {
        var newFile = File(fileDestStr);
        newFile.open("w");
        newFile.write(contents);
        newFile.close();
    };


    var actionStr = [
        "/version 3",
        "/name [ 5",
        "	5365742031",
        "]",
        "/isOpen 1",
        "/actionCount 1",
        "/action-1 {",
        "/name [ 8",
        "416374696f6e2031",
        "	]",
        "/keyIndex 0",
        "/colorIndex 0",
        "/isOpen 0",
        "/eventCount 1",
        "/event-1 {",
        "/useRulersIn1stQuadrant 0",
        "/internalName (adobe_saveACopyAs)",
        "/localizedName [ 11",
        "	53617665204120436f7079",
        "]",
        "/isOpen 1",
        "/isOn 1",
        "/hasDialog 1",
        "/showDialog 0",
        "/parameterCount 18",
        "/parameter-1 {",
        "/key 1869640814",
        "/showInPalette 4294967295",
        "/type (integer)",
        "/value 2",
        "}",
        "/parameter-2 {",
        "/key 1987077222",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-3 {",
        "/key 1970500209",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-4 {",
        "/key 1970499699",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ 0", "", "]",
        "}",
        "/parameter-5 {",
        "/key 1836282481",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-6 {",
        "/key 1836281971",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ 0",
        "",
        "	]",
        "}",
        "/parameter-7 {",
        "/key 1886220402",
        "/showInPalette 4294967295",
        "/type (enumerated)",
        "/name [ 16",
        "5072696e74696e6720416c6c6f776564",
        "	]",
        "/value 3",
        "}",
        "/parameter-8 {",
        "/key 1886217064",
        "/showInPalette 4294967295",
        "/type (enumerated)",
        "/name [ 15",
        "4368616e67657320416c6c6f776564",
        "	]",
        "/value 5",
        "}",
        "/parameter-9 {",
        "/key 1700946800",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 1",
        "}",
        "/parameter-10 {",
        "/key 1700946275",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 1",
        "}",
        "/parameter-11 {",
        "/key 1700946785",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 1",
        "}",
        "/parameter-12 {",
        "/key 1700950132",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-13 {",
        "/key 1851878757",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ PDF_PATH_LENGTH", "PDF_FULL_PATH",
        "]",
        "}",
        "/parameter-14 {",
        "/key 1718775156",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ 15",
        "5044462046696c6520466f726d6174",
        "	]",
        "}",
        "/parameter-15 {",
        "/key 1702392942",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ 3",
        "706466",
        "	]",
        "}",
        "/parameter-16 {",
        "/key 1936548194",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 0",
        "}",
        "/parameter-17 {",
        "/key 1935764588",
        "/showInPalette 4294967295",
        "/type (boolean)",
        "/value 1",
        "}",
        "/parameter-18 {",
        "/key 1936875886",
        "/showInPalette 4294967295",
        "/type (ustring)",
        "/value [ 0",
        "",
        "	]",
        "}",
        "	}",
        "}"
    ].join("\n");

    if (app.documents.length == 0) {
        return;
    }

    var actionFileDestStr = Folder.desktop + "/MyAction.aia";
    writeFile(actionFileDestStr, actionStr.replace("PDF_PATH_LENGTH", _pdfFileName.length).replace("PDF_FULL_PATH", _pdfFileName.hexEncode()));
    var actionFile = File(actionFileDestStr);
    app.loadAction(actionFile);
    app.doScript("Action 1", "Set 1");


    //clean up 
    actionFile.remove();
    app.unloadAction("Set 1", "");
};

saveAsCopy();

 

Best regards