Skip to main content
rcraighead
Legend
April 9, 2018
Question

Script periodically crashes AI CC 2015.3. How can I fix it?

  • April 9, 2018
  • 1 reply
  • 439 views

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;

}

This topic has been closed for replies.

1 reply

pixxxelschubser
Community Expert
Community Expert
April 9, 2018

Hi

at the beginning is missing this line:

var doc = app.activeDocument;

and

if(sel.typename == '[RasterItem ]'){

should be

if(sel.typename == 'RasterItem '){

These are the major mistakes.

The script takes a while, but it does (Illu CC under Windows 10).

rcraighead
Legend
April 9, 2018

@pixxxel

Thank you for your help! I actually do have the first line, but missed it when pasting. I'll make the correction. Thanks again.

pixxxelschubser
Community Expert
Community Expert
April 9, 2018

Jive makes curious thing today

the last line of code in my answer #1 should be:

if(sel.typename == 'RasterItem'){

(without a space behind RasterItem)

And there are a few things not correct (wrong order)

var doc = app.activeDocument;

var pItems = doc.pathItems;

var sel = doc.selection;

var Selected = doc.selection[0];

var Tracing_Preset = '[Default]'; 

var f =15; //percent of current size 

Do you always have a selection in your document? Otherwise selection[0] gives you undefined

Do you really need the selection?

Your images are embedded? Or only linked?

And do better not use names of method or properties use as variable names (in your code: var Selected)