Skip to main content
Inspiring
July 30, 2021
Answered

How to complete Image/Trap with javascript

  • July 30, 2021
  • 5 replies
  • 2470 views

I work with CMYK images and I want to create script which will flatten layers and complete trapping 0,2mm. I tried with Script Listener and i got this code but result is slightly different when i do it manuly.

 

    var idFltI = charIDToTypeID( "FltI" );
    executeAction( idFltI, undefined, DialogModes.NO );
    // =======================================================
    var idTrap = charIDToTypeID( "Trap" );
    var desc6698 = new ActionDescriptor();
    var idWdth = charIDToTypeID( "Wdth" );
    var idRlt = charIDToTypeID( "#Rlt" );
    desc6698.putUnitDouble( idWdth, idRlt, 0.566895 );
    executeAction( idTrap, desc6698, DialogModes.NO );

 

 

This topic has been closed for replies.
Correct answer Stephen Marsh

I would work in px and trap to the desired unit in px values.

 

// Save the current ruler units and set to pixels
var savedRuler = app.preferences.rulerUnits;        
app.preferences.rulerUnits = Units.PIXELS;

// Do your stuff...

// Restore the ruler units
app.preferences.rulerUnits = savedRuler;

5 replies

rob day
Community Expert
Community Expert
July 30, 2021

Hi @milevic , the actual output size of the trap would change depending on resolution—using pixel units, a 600ppi image would need a larger trap amount than a 300ppi image.

 

So an alternate to setting your rulers to pixels might be this, where I’m getting the size of a document pixel in millimeters and converting the tmm variable into pixels for the .trap parameter, which needs to be a whole pixel amount:

 

//the desired trap amount in millimeters
var tmm = .2;

//a single pixel dimension as millimeters
var mm = (1/app.activeDocument.resolution)/.039370;

if (app.activeDocument.mode == DocumentMode.CMYK) {
    //the millimeter amount converted to pixels (needs to be the nearest whole number)
	app.activeDocument.trap(Math.round(tmm/mm)); 
}; 

 

milevicAuthor
Inspiring
August 7, 2021

Hi @rob day  Thank you for your dedication, I like your solution but result of your script looks like 2mm trap, not 0.2mm.

Could you improve this? Also, is it possible to use (or to adapt) yours code for MULTICHANNEL mode?

 

rob day
Community Expert
Community Expert
August 7, 2021

I noticed that the Trap Units setting is sticky, so my script won’t work if you run it after a trap has been created with the UI using Millimeters as the unit, but will if the last applied trap’s unit setting was Pixels.

 

There doesn’t seem to be a way to specifically set trap units before running the trap method with the API, and setting the ruler units doesn’t seem to matter. Here’s the method description—it should work so it seems like a bug:

 

Photoshop ExtendScript API Adobe Photoshop CC 2015.5 Object Library - Document

 

With the trap units set to the default pixels, I get this running the script:

 

 

But running after a manual trap using Millimeter trap units I get this:

 

 

 

Here’s the script as a function, and would work with Multichannel:

 

//run trap function with parameter as the amount in millimeters
trapMM(.2)


/**
* Sets a trap in millimeters    
*  trap amount as milimeters 
*  void 
* 
*/
function trapMM(t){
    try {
        var mm = (1/app.activeDocument.resolution)/.039370;
        app.activeDocument.trap(Math.round(t/mm)); 
    }catch(e) {
        alert("The document mode must be 8-bit CMYK or MultiChannel")
    }  
}

 

rob day
Community Expert
Community Expert
July 30, 2021

The API also has a trap command, the parameter is the trap in pixels:

 

if (app.activeDocument.mode == DocumentMode.CMYK) {
	app.activeDocument.trap(4); 
}; 
Kukurykus
Legend
July 30, 2021

I found it now in Image menu, but I never used it as I never work in CMYK. After changing the mode to this colour the Trap item was enabled. I tried it on an image with transparency and noticed some border effect going to inside of object in the layer. I still do not understand what is meant for and why it is only for CMYK. Can anyone explain to me what it should be used for?

c.pfaffenbichler
Community Expert
Community Expert
July 30, 2021

See »Create a color trap« here: 

Print images to a commercial printing press

Kukurykus
Legend
July 30, 2021

What is 'trapping' in Photoshop and how to do it manually?

c.pfaffenbichler
Community Expert
Community Expert
July 30, 2021

»Trapping« is »choking« and »spreading« color areas (the lighter one into the darker one usually) against each other to mitigate the effect of misalignment in printing. 

 

It is available for CMYK images under 

Image > Trap 

Kukurykus
Legend
July 30, 2021

That's right, the printers work in CMYK mode for better effect probably.

c.pfaffenbichler
Community Expert
Community Expert
July 30, 2021

So 

    var idpixelsUnit = stringIDToTypeID( "pixelsUnit" );

instead of 

    var idRlt = charIDToTypeID( "#Rlt" );

 

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
July 30, 2021

I would work in px and trap to the desired unit in px values.

 

// Save the current ruler units and set to pixels
var savedRuler = app.preferences.rulerUnits;        
app.preferences.rulerUnits = Units.PIXELS;

// Do your stuff...

// Restore the ruler units
app.preferences.rulerUnits = savedRuler;