Skip to content

Instantly share code, notes, and snippets.

@moluapple
Created August 11, 2012 06:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save moluapple/3321845 to your computer and use it in GitHub Desktop.
Save moluapple/3321845 to your computer and use it in GitHub Desktop.
[Illustrator] Relaunch AI to aviod "PARM" error
// Origin discussion: http://forums.adobe.com/message/4613051
#target "estoolkit"
rasterizeFolder();
/********************************************************************************************/
function rasterizeFolder() {
//Get the source and destination folders.
//If either window is cancelled, end the script.
var sourceFolder = getSourceFolder();
if (sourceFolder == null) {
return;
}
var destinationFolder = getDestinationFolder();
if (destinationFolder == null) {
return;
}
//Stuff for statistics
var fileCount = 0;
var itemsProcessed = 0;
var itemsToReset = 5000; //Used to periodically close and reopen Illustrator to avoid memory issues.
var startTime = new Date().getTime();
var endTimeMS = 0;
var endSecs = 0;
var endMins = 0;
//Rasterize files by type.
rasterizeAllOfType("*.eps");
//rasterizeAllOfType ("*.pdf");
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function getSourceFolder() {
var sourceFolder = Folder.selectDialog("Choose the folder you wish to rasterize.");
if (sourceFolder == null) {
//User cancelled, do nothing.
return;
} else if (sourceFolder.getFiles().length < 1) {
//The folder contains nothing, make the user select another.
alert("The source folder you selected contains no files. Select a different one.");
sourceFolder = getSourceFolder();
return sourceFolder;
} else {
//We have a usable folder.
return sourceFolder;
}
} //End of getSourceFolder
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function getDestinationFolder() {
var destinationFolder = Folder.selectDialog("Where should the rasterized files go?");
if (destinationFolder == null) {
//User cancelled, do nothing.
return;
} else if (destinationFolder.getFiles().length < 1) {
//The folder contains nothing and is therefore safe to use.
return destinationFolder;
} else if (sourceFolder.getFiles()[0].path == destinationFolder.getFiles()[0].path) {
//The user chose the same folder for the source and destination.
//See if it's okay to overwrite the files in there.
var OKtoOverwrite = confirm("Source and destination are the same. Files will be overwritten. Do you want to continue?");
if (OKtoOverwrite == true) {
//Okay to overwrite, proceed.
return destinationFolder;
} else {
//Not okay, make a different choice.
destinationFolder = getDestinationFolder();
return destinationFolder;
}
} else {
//Folder has contents, but turned out to be different than the source folder.
return destinationFolder;
}
} //End of getDestinationFolder
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function rasterizeAllOfType(filetype) {
//Get all files of the type I'm working on
var fileList = new Array();
fileList = sourceFolder.getFiles(filetype);
if (fileList.length > 0) {
//There are files to work with of this type.
//New code here.
var j = 0;
AIrasterize(fileList, destinationFolder, itemsProcessed, itemsToReset);
fileCount += fileList.length;
return;
} else {
//No files of this type are in the folder.
return;
} /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function AIrasterize(fileList, destinationFolder, itemsProcessed, itemsToReset) {
/*
NOTICE: If a function is sent via bridgetalk, either use <function>.toString()
or avoid using single-line comments in it. If just sent normally, it
will be read as if it is all one line, and thus break from comments.
*/
var bt = new BridgeTalk();
bt.target = 'illustrator';
bt.body = rasterizeDoc.toString() + ";rasterizeDoc(" + fileList[j].toSource() + "," + destinationFolder.toSource() + "," + itemsProcessed + "," + itemsToReset + ");";
bt.onResult = function(resultMsg) {
//var myStatus = BridgeTalk.getStatus('illustrator');
itemsProcessed = eval(resultMsg.body);
if (!itemsProcessed) {
// waiting for illustrator to quit.
while (BridgeTalk.getStatus('illustrator') != 'ISNOTRUNNING') $.sleep(20);
// as the last opened file has not been processed, so go back to it.
j--;
}
++j < fileList.length && AIrasterize(fileList, destinationFolder, itemsProcessed, itemsToReset);
$.writeln(itemsProcessed);
if (j == fileList.length) {
//Get the time taken for the script in milliseconds, and convert to minutes and seconds.
endTimeMS = new Date().getTime() - startTime;
endSecs = Math.floor(endTimeMS / 1000);
endMins = Math.floor(endSecs / 60);
endSecs -= (endMins * 60);
//Tell the user how long this took.
alert(fileCount + " files rasterized in " + endMins + " minutes, " + endSecs + " seconds.");
}
}
bt.onError = function(a) {
alert(a.body + "(" + a.headers["Error-Code"] + ")");
}
bt.send();
//$.writeln(bt.body); //used to debug.
} //End of AIrasterize
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function rasterizeDoc(myDoc, destinationFolder, itemsProcessed, itemsToReset, ) { /*Open the file*/
sourceDoc = app.open(myDoc);
/* Check first whether the doc will be overload or not, if yes, quit. */
itemsProcessed += sourceDoc.pageItems.length;
if (itemsProcessed > itemsToReset) {
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
alert("Processed " + itemsProcessed + " objects. Resetting to clear memory");
app.quit() /*Workaround for AIrasterize bug. BT only goes to the next when AI is closed. */
/*reset itemsProcessed to 0*/
return 0
}
/*Set rasterize boundaries and options.*/
var rastOptions = new RasterizeOptions;
rastOptions.antiAliasingMethod = AntiAliasingMethod.None;
rastOptions.clippingMask = false;
rastOptions.resolution = 300;
rastOptions.convertSpotColors = false;
rastOptions.transparency = true;
/*Create a layer for text.*/
var textLayer = sourceDoc.layers.add();
textLayer.name = "Text Layer";
/*Get the number of layers, then cycle through them.*/
var layerCount = sourceDoc.layers.length;
for (var i = 0; i < layerCount; i++) {
var myLayer = sourceDoc.layers[i];
if (myLayer.locked == false && myLayer.visible && myLayer.name != "Text Layer") {
/*Ungroup everything*/
ungroup(myLayer);
/*Send all text frames and legacy text items to text layer*/
moveAllToLayer(myLayer.textFrames, textLayer);
moveAllToLayer(myLayer.legacyTextItems, textLayer);
/*Rasterize all other kinds of object.*/
rasterizeObjectClass(myLayer.rasterItems);
rasterizeObjectClass(myLayer.pluginItems);
rasterizeObjectClass(myLayer.compoundPathItems);
rasterizeObjectClass(myLayer.pathItems);
}
}
/*Get the full file path.*/
var saveName = new File(destinationFolder + '/' + sourceDoc.name);
/*Save the file as the appropriate type. use a regex to check the filetype is just OK */
var filetype = sourceDoc.name.match(/(?:ai|eps|pdf)$/)[0];
switch (filetype) {
case "eps":
var saveSettingsEPS = new EPSSaveOptions;
sourceDoc.saveAs(saveName, saveSettingsEPS);
break;
case "pdf":
var saveSettingsPDF = new PDFSaveOptions;
sourceDoc.saveAs(saveName, saveSettingsPDF);
break;
default:
sourceDoc.saveAs(saveName);
break;
}
/*Close the file and clear the reference from memory.*/
sourceDoc.close(SaveOptions.DONOTSAVECHANGES);
return itemsProcessed
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function ungroup(myObject) { /*Note: myObject is always either a group or a layer.*/
/*Get a list of everything inside this object.
This array is used instead of the actual callout so we can loop easier.*/
var objectContents = new Array();
for (var i = 0; i < myObject.pageItems.length; i++) {
objectContents.push(myObject.pageItems[i]);
}
if (objectContents.length < 1) { /*destroy empty groups.*/
if (myObject.typename == "GroupItem") {
myObject.remove();
}
return;
} else { /*Loop through group contents*/
for (var i = objectContents.length; i > 0; i--) {
try {
if (objectContents[i - 1].parent.typename != "Layer") { /*If I'm in a group, take me out of it and put me one level higher.*/
objectContents[i - 1].moveBefore(myObject);
}
if (objectContents[i - 1].typename == "GroupItem") { /*If I AM a group, ungroup my contents.*/
ungroup(objectContents[i - 1]);
}
} catch (err) {}
}
}
} //End of ungroup
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function moveAllToLayer(objectCollection, destLayer) { /*Make it workable*/
var objectClass = new Array();
objectClass = objectCollection;
/*Moves all of the chosen kind of object to a layer.*/
for (var i = objectClass.length; i > 0; i--) {
objectClass[i - 1].moveToBeginning(destLayer);
}
objectClass = null;
} //End of moveAllToLayer
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
function rasterizeObjectClass(objectCollection) { /*Make it workable*/
var objectClass = new Array();
objectClass = objectCollection;
/*If I have any items of this type, group them together and rasterize the group.*/
if (objectClass.length > 0) {
var objectGroup = myLayer.groupItems.add();
for (var i = objectClass.length; i > 0; i--) {
try {
objectClass[i - 1].move(objectGroup, ElementPlacement.PLACEATBEGINNING);
} catch (e) {}
} /*** isn't objectGroup.visibleBounds better than document? **/
sourceDoc.rasterize(objectGroup, objectGroup.visibleBounds, rastOptions);
objectGroup = null;
}
objectClass = null;
} //End of rasterizeObjectClass
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
} //End of rasterizeDoc
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
} //End of rasterizeAllOfType
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
return;
} //End of rasterizeFolder
#target "estoolkit"
function processAi(name) {
var bt = new BridgeTalk();
bt.target = 'illustrator';
bt.body = process.toSource() + '("' + name + '");';
bt.onResult = function (resultMsg) {
while (BridgeTalk.getStatus('illustrator') != 'ISNOTRUNNING') $.sleep(20);
++i < 3 && processAi('doc_' + i);
}
bt.send();
}
function process(name) {
var doc = app.documents.add();
doc.textFrames.add().contents = name;
doc.saveAs(File('~/desktop/' + name + '.ai'));
doc.close(SaveOptions.DONOTSAVECHANGES);
app.quit();
}
var i = 0;
processAi('doc_' + i);
@Yogeshvasu
Copy link

I have set itemsToReset = 5000, but it closes the illustrator , but it does not launches the illustrator again to continue from that file.
also still parm error is not rectified , can any one help me how to solve parm error issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment