Script periodically crashes AI CC 2015.3. How can I fix it?
The script, below, is a compilation of several snippets I've gathered from this and other forums. If I used (abused) your script I truly thank you.
My question is; how can I improve it? I am running it as part of a Keyboard Maestro macro and calling it through an AppleScript (which seems to be the accepted method in Keyboard Maestro). It usually works fine, but sometimes causes AI CC 2015.3 (Mac) to crash. It also fails to perform the "AutoTrace" sequence the first time it runs after a System reboot, simply saving the bitmap image as a PDF instead.
Any suggestions for improving the script would be greatly appreciated. I'd also be very willing to hire someone to fix this and other possible scripts I need to develop.
The goal of the script is to take a Finder selection (PNG or JPG), open it in AI, perform a tracing and save a Vector PDF to the same folder as the original file.
Thanks,
Ray
var doc = app.activeDocument;
var pItems = doc.pathItems;
var Selected = doc.selection[0]
var Tracing_Preset = '[Default]';
var doc = app.activeDocument;
var sel = doc.selection;
var f =15; //percent of current size
//~ Trace Fingerprint
for(var i = 0; i<sel.length; i++){
if(sel == '[RasterItem ]'){
var pic = sel.trace();
pic.tracing.tracingOptions.loadFromPreset(Tracing_Preset);
pic.tracing.expandTracing().selected = true;
}
}
//Scale Fingerprint
for ( var i = 0; i < pItems.length; i++ ) {
pItems.resize( f,f,true,true,true,true,f,Transformation.DOCUMENTORIGIN );
};
//~ Fit Artboard to Fingerprint
function fitArtboards() {
if (app.documents.length == 0) {
alert('Open a document before running this script');
return;
} else {
for (var i = 0; i < documents.length; i++ ) {
app.activeDocument = documents;
app.activeDocument.artboards[0].artboardRect = app.activeDocument.geometricBounds;
redraw();
}
}
}
fitArtboards();
//Save PDF
function test(){
var doc = app.activeDocument;
var parentPath = Folder(File(doc.fullName).parent);
var opts = new PDFSaveOptions();
//~ opts.pDFPreset = "[Smallest File Size]";
opts.pDFPreset = "[Illustrator Default]";
doc.saveAs(File(parentPath + "/" + doc.name.replace(/\.\w+$/, "_Vector.pdf")), opts);
};
test();
// Close file
if ( app.documents.length > 0 ) {
aiDocument = app.activeDocument;
aiDocument.close( SaveOptions.DONOTSAVECHANGES );
aiDocument = null;
}
