Copy link to clipboard
Copied
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.
@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 = functi
...
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
@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();
Copy link to clipboard
Copied
That works perfectly!
I have 3 different version of my original code - Low res, Print and Zund.
I tried to adapt your code to my Print and Zund saves but I can't see what I need to change?
You've been so helpful, thank you.
Copy link to clipboard
Copied
This script crashes Illustrator if I set it up as an action. Do you know why?
Many thanks
Copy link to clipboard
Copied
The above script crashes or it crashes after your changes?
Or are you trying to record the actions?
Copy link to clipboard
Copied
@Charu Rajput I have the script setup as an action, just so it's one click rather finding it in the script menu. The script works if I select it from the script menu but if I use the action it crashes after creating the folder, without creating the PDF.
Copy link to clipboard
Copied
Yes I see it stucks at point, where I am running the action in the script. I am not aware of the reason, but I will look. May be action from another action is not possible. Never tried this way.
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
exportForScreens allows you to export a pdf with copying. The sample code can be found at the following link.