Search code examples
adobe-illustratorextendscript

Adobe Illustrator - Scripting crashes when trying to fit to artboards command


activeDocument.fitArtboardToSelectedArt()

When calling this command, AI crashes on AI 5.1/6 32bit and 64bit versions. I can use the command from the menu. Has anyone encountered this? does anyone know of a work around?

The full code.

    function exportFileToJPEG (dest) {
    if ( app.documents.length > 0 ) {

        activeDocument.selectObjectsOnActiveArtboard()
        activeDocument.fitArtboardToSelectedArt()//crashes here
        activeDocument.rearrangeArtboards()

        var exportOptions = new ExportOptionsJPEG();
        var type = ExportType.JPEG;
        var fileSpec = new File(dest);
        exportOptions.antiAliasing = true;
        exportOptions.qualitySetting = 70;
        app.activeDocument.exportFile( fileSpec, type, exportOptions );
    }
}
var file_name = 'some eps file.eps'
var eps_file = File(file_name)


var fileRef = eps_file;



if (fileRef != null) {
    var optRef = new OpenOptions();
    optRef.updateLegacyText = true;
    var docRef = open(fileRef, DocumentColorSpace.RGB, optRef);
}

exportFileToJPEG ("output_file.jpg")

Solution

  • I can reproduce the bug with AI CS5.

    It seems that fitArtboardToSelectedArt() takes the index of an artboard as an optional parameter. When the parameter is set, Illustrator doesn't crash. (probably a bug in the code handling the situation of no parameter passed)

    As a workaround you could use:

    activeDocument.fitArtboardToSelectedArt(
                   activeDocument.artboards.getActiveArtboardIndex()
                                           );
    

    to pass the index of the active artboard with to the function. Hope this works for you too.

    Also it's good practice to never omit the semicolon at the end of a statement.