Skip to main content
Participating Frequently
March 21, 2024
Answered

Open an AI file with jsx script at a specific height

  • March 21, 2024
  • 2 replies
  • 776 views

I am trying to write a script that will open an Adobe Illustrator file at a specific height. But I am having trouble finding out why this code isn't working.

function openPdfPage (filePath) {

    try {
  
      var pdfOpenOpts = new PDFOpenOptions;
      pdfOpenOpts.antiAlias = true;
      pdfOpenOpts.bitsPerChannel = BitsPerChannelType.SIXTEEN;
      pdfOpenOpts.cropPage = CropToType.BOUNDINGBOX;
      pdfOpenOpts.mode = OpenDocumentMode.RGB;
      pdfOpenOpts.resolution = 300;
      pdfOpenOpts.constrainProportions = true;
      pdfOpenOpts.height = new UnitValue(50, 'px');
      pdfOpenOpts.suppressWarnings = false;
      pdfOpenOpts.usePageNumber = true;
      pdfOpenOpts.page = 1;
  
      app.open(new File(filePath), pdfOpenOpts);
  
      return app.activeDocument
  
    } catch (e) {
        alert(e.message);

        return undefined};
  
  }

 

This topic has been closed for replies.
Correct answer c.pfaffenbichler

Unfortunately I have no way of knowing what the width would be, so I set the constrainProportions to true. I did think setting the width might be necessary so I set it for a test on one I know the width and it still didn't work for me.


The width works for me. 

aaa("~/Desktop/Untitled-2.ai");
function aaa (filePath) {
var idopen = stringIDToTypeID( "open" );
var desc10 = new ActionDescriptor();
var idas = stringIDToTypeID( "as" );
var desc11 = new ActionDescriptor();
/*var idname = stringIDToTypeID( "name" );
desc11.putString( idname, """Untitled-1.ai""" );*/
var idcrop = stringIDToTypeID( "crop" );
var idcropTo = stringIDToTypeID( "cropTo" );
var idtrimBox = stringIDToTypeID( "trimBox" );
desc11.putEnumerated( idcrop, idcropTo, idtrimBox );
var idresolution = stringIDToTypeID( "resolution" );
var iddensityUnit = stringIDToTypeID( "densityUnit" );
desc11.putUnitDouble( idresolution, iddensityUnit, 300.000000 );
var idmode = stringIDToTypeID( "mode" );
var idcolorSpace = stringIDToTypeID( "colorSpace" );
var iduseICCProfile = stringIDToTypeID( "useICCProfile" );
desc11.putEnumerated( idmode, idcolorSpace, iduseICCProfile );
var iddepth = stringIDToTypeID( "depth" );
desc11.putInteger( iddepth, 8 );
var idantiAlias = stringIDToTypeID( "antiAlias" );
desc11.putBoolean( idantiAlias, true );
var idwidth = stringIDToTypeID( "width" );
var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
desc11.putUnitDouble( idwidth, idpixelsUnit, 71.000000 );
var idheight = stringIDToTypeID( "height" );
var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );
desc11.putUnitDouble( idheight, idpixelsUnit, 50.000000 );
var idconstrainProportions = stringIDToTypeID( "constrainProportions" );
desc11.putBoolean( idconstrainProportions, true );
var idsuppressWarnings = stringIDToTypeID( "suppressWarnings" );
desc11.putBoolean( idsuppressWarnings, false );
var idreverse = stringIDToTypeID( "reverse" );
desc11.putBoolean( idreverse, true );
var idselection = stringIDToTypeID( "selection" );
var idpdfSelection = stringIDToTypeID( "pdfSelection" );
var idpage = stringIDToTypeID( "page" );
desc11.putEnumerated( idselection, idpdfSelection, idpage );
var idpageNumber = stringIDToTypeID( "pageNumber" );
desc11.putInteger( idpageNumber, 1 );
var idPDFGenericFormat = stringIDToTypeID( "PDFGenericFormat" );
desc10.putObject( idas, idPDFGenericFormat, desc11 );
var idnull = stringIDToTypeID( "null" );
desc10.putPath( idnull, new File( filePath ) );
executeAction( idopen, desc10, DialogModes.NO );
};

 

2 replies

c.pfaffenbichler
Community Expert
Community Expert
March 21, 2024

HAve you tried recording AM-code for the operation with ScriptingListener.plugin? 

Participating Frequently
March 21, 2024

Yes, I did try that, it didn't work either. 

  function openPDFHard(filePath) {

    var idOpn = charIDToTypeID( "Opn " );
    var desc228 = new ActionDescriptor();
    var idAs = charIDToTypeID( "As  " );
        var desc229 = new ActionDescriptor();
        var idCrop = charIDToTypeID( "Crop" );
        var idcropTo = stringIDToTypeID( "cropTo" );
        var idboundingBox = stringIDToTypeID( "boundingBox" );
        desc229.putEnumerated( idCrop, idcropTo, idboundingBox );
        var idRslt = charIDToTypeID( "Rslt" );
        var idRsl = charIDToTypeID( "#Rsl" );
        desc229.putUnitDouble( idRslt, idRsl, 300.000000 );
        var idMd = charIDToTypeID( "Md  " );
        var idClrS = charIDToTypeID( "ClrS" );
        var idRGBC = charIDToTypeID( "RGBC" );
        desc229.putEnumerated( idMd, idClrS, idRGBC );
        var idDpth = charIDToTypeID( "Dpth" );
        desc229.putInteger( idDpth, 16 );
        var idAntA = charIDToTypeID( "AntA" );
        desc229.putBoolean( idAntA, true );
        var idHght = charIDToTypeID( "Hght" );
        var idPxl = charIDToTypeID( "#Pxl" );
        desc229.putUnitDouble( idHght, idPxl, 50.000000 );
        var idCnsP = charIDToTypeID( "CnsP" );
        desc229.putBoolean( idCnsP, true );
        var idsuppressWarnings = stringIDToTypeID( "suppressWarnings" );
        desc229.putBoolean( idsuppressWarnings, false );
        var idRvrs = charIDToTypeID( "Rvrs" );
        desc229.putBoolean( idRvrs, true );
        var idfsel = charIDToTypeID( "fsel" );
        var idpdfSelection = stringIDToTypeID( "pdfSelection" );
        var idpage = stringIDToTypeID( "page" );
        desc229.putEnumerated( idfsel, idpdfSelection, idpage );
        var idPgNm = charIDToTypeID( "PgNm" );
        desc229.putInteger( idPgNm, 1 );
    var idPDFG = charIDToTypeID( "PDFG" );
    desc228.putObject( idAs, idPDFG, desc229 );
    var idnull = charIDToTypeID( "null" );
    desc228.putPath( idnull, new File(filePath) );
    var idDocI = charIDToTypeID( "DocI" );
    desc228.putInteger( idDocI, 60 );
executeAction( idOpn, desc228, DialogModes.NO );
  }

 

c.pfaffenbichler
Community Expert
Community Expert
March 21, 2024

What is »didn’t work« supposed to describe exactly? 

Stephen Marsh
Community Expert
Community Expert
March 21, 2024

What exactly do you mean by "doesn't work"? 

 

When I call the function, the file is opened:

 

openPdfPage ("~/Desktop/test.ai");

 

Participating Frequently
March 21, 2024

Thanks for the response.
Correct, the file opens but the image doesn't have the specified height. 

Stephen Marsh
Community Expert
Community Expert
March 21, 2024

So what height is the bounds of the source object and what height do you wish to rasterize to?