Skip to main content
Participant
January 21, 2016
Answered

How to convert image to CMYK (from any format)?

  • January 21, 2016
  • 1 reply
  • 1905 views

This seems like it should be pretty simple.

I tried to reverse engineer a function from the "Warn if RGB.jsx" found: applications/Adobe Photoshop CC 2015/ Presets/Scripts/Warn if RGB.jsx

in this file the image that is open in Photoshop is taken and converted to RGB.

Well that's exactly what I want to do, except the reverse.

How do I write code that would take my "current image" and change it to CMYK just as if I were to go into photoshop (GUI) Edit> Convert to Profile... > and select the CMYK bullet option under "Destination Space"

I need this in a function for other script I have.

I will past what I have (with my poor attempt at reverse engineering the "Warn if RGB.jsx" function in place.

Thank you. I could really use the help and I think this should be pretty straight forward.

try {

  var data = GetDataFromDocument( activeDocument );

  if ('eps' == data.extension.toLowerCase()) {

  //convert to cmyk

  Switch();

  }

  else{

  //run code to square up images

  toSquare();

  }

}

catch( e ) {

  alert(e);

}

//FUNCTIONS

///////////////////////////////////////////////

///////////////////////////////////////////////

function toSquare(){

  var savedRuler= app.preferences.rulerUnits;

  app.preferences.rulerUnits = Units.PIXELS;

  var w = app.activeDocument.width;

  var h = app.activeDocument.height;

  if(w>h) app.activeDocument.resizeCanvas (w, w, AnchorPosition.MIDDLECENTER);

  if(w<h) app.activeDocument.resizeCanvas (h, h, AnchorPosition.MIDDLECENTER);

  //if w==h already square

  app.preferences.rulerUnits = savedRuler;

}

///////////////////////////////////////////////

function GetDataFromDocument( inDocument ) {

  var data = new Object();

  var fullPathStr = inDocument.fullName.toString();

  var lastDot = fullPathStr.lastIndexOf( "." );

  var fileNameNoPath = fullPathStr.substr( 0, lastDot );

  data.extension = fullPathStr.substr( lastDot + 1, fullPathStr.length );

  var lastSlash = fullPathStr.lastIndexOf( "/" );

  data.fileName = fileNameNoPath.substr( lastSlash + 1, fileNameNoPath.length );

  data.folder = fileNameNoPath.substr( 0, lastSlash );

  data.fileType = inDocument.fullName.type;

  return data;

}

///////////////////////////////////////////////

function Switch() {

  var eventModeChange = stringIDToTypeID( "8cba8cd6-cb66-11d1-bc43-0060b0a13dc4" );

  var descSource = new ActionDescriptor();

  var keySourceMode = charIDToTypeID( "SrcM" );

  var list = new ActionList();

  var keyCondition = charIDToTypeID( "Cndn" );

  var keyBitmap = charIDToTypeID( "UBtm" );

  var keyGrayscale = charIDToTypeID( "UGry" );

  var keyDuotone = charIDToTypeID( "UDtn" );

  var keyIndex = charIDToTypeID( "UInd" );

  var keyRGB = charIDToTypeID( "URGB" );

  var keyCMYK = charIDToTypeID( "UCMY" );

  var keyLab = charIDToTypeID( "ULab" );

  var keyMultichannel = charIDToTypeID( "UMlt" );

  list.putEnumerated( keyCondition, keyBitmap );

  list.putEnumerated( keyCondition, keyGrayscale );

  list.putEnumerated( keyCondition, keyDuotone );

  list.putEnumerated( keyCondition, keyIndex );

  list.putEnumerated( keyCondition, keyRGB );

  list.putEnumerated( keyCondition, keyCMYK );

  list.putEnumerated( keyCondition, keyLab );

  list.putEnumerated( keyCondition, keyMultichannel );

  descSource.putList( keySourceMode, list );

  var keyDestination = charIDToTypeID( "DstM" );

  var descDest = new ActionDescriptor();

  var keyCMYK = charIDToTypeID( "UCMY" );

  descSource.putObject( keyDestination, keyRGB, descDest );

  executeAction( eventModeChange, descSource, DialogModes.NO );

}

//////////////////////////////////////////////

//////////////////////////////////////////////

This topic has been closed for replies.
Correct answer oliverIntergrafika

Seems like the forum engine swallowing random whitespaces when we switch to advanced editor mode:

The exact line is this:

var idT = charIDToTypeID( "T   " );

There are 3 spaces after the T.

1 reply

oliverIntergrafika
Inspiring
January 21, 2016

function converToCMYK( rasterize )

{

    var idCnvM = charIDToTypeID( "CnvM" );

    var desc39 = new ActionDescriptor();

    var idT = charIDToTypeID( "T  " );

    var idCMYM = charIDToTypeID( "CMYM" );

    desc39.putClass( idT, idCMYM );

    var idMrge = charIDToTypeID( "Mrge" );

    desc39.putBoolean( idMrge, false );

    var idRstr = charIDToTypeID( "Rstr" );

    desc39.putBoolean( idRstr, rasterize );

    executeAction( idCnvM, desc39, DialogModes.NO );

}

converToCMYK ( rasterize = true ) ;

This code is copied from ScriptingListenerJS.log which is the output of the ScriptListener.8li plugin.

Participant
January 22, 2016

When I try this code, the resulting error is:

oliverIntergrafika
oliverIntergrafikaCorrect answer
Inspiring
January 22, 2016

Seems like the forum engine swallowing random whitespaces when we switch to advanced editor mode:

The exact line is this:

var idT = charIDToTypeID( "T   " );

There are 3 spaces after the T.