Copy link to clipboard
Copied
I'm looking for a tool or a JavaScript that let me know the color mode of a SmartObject in Photoshop.
I have copied objects from an Illustrator file and pasted it in a PSD file. The PSD could be in RGB or CMYK, and similarly, the AI objects could be in CMYK or RGB. I want a JavaScript that tells me the color mode of the SmartObject.
Can anyone help on this, please.
@code_seeeeeker – The following script is for a single active layer (I started work on it before your full requirements were known). I'll look into adapting it for all layers.
/*
Check if the vector smart object in Photoshop is in RGB or CMYK mode.jsx
Stephen Marsh
v1.0 - 11th March 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/smartobject-in-photoshop/td-p/15195068
Info: Alerts the color mode of the active vector smart object layer
*/
#target photoshop
// Check if the...
Copy link to clipboard
Copied
Sample files are helpful.
Will Illustrator be available, or are you looking for a solution without Illustrator?
Copy link to clipboard
Copied
Thanks for your response, Stephen.
you can copy any object from Illustrator and Paste it in Photoshop for testing. I have also attached one for your review. In this, the PSD is in CMYK, whereas the SmartObject is in RGB. If you click the SmartObject in Photoshop, it will open in Illustrator and any changes made in the Illustrator files get reflected in PSD.
Copy link to clipboard
Copied
So when you edit the vector SO it opens into Illustrator or whatever app is set as default for the file type extension, then you know the colour mode. This is why I asked if you wanted to try avoid this.
What are you wanting? An alert to pop up stating the colour mode without having to launch Illustrator?
Copy link to clipboard
Copied
What are you wanting? An alert to pop up stating the colour mode without having to launch Illustrator?
By @Stephen Marsh
Yes, Stephen, you got it right. A kind of JS file to loop through all the SmartObjects in a PSD file and pop up if the color mode of the SmartObject is different than the color mode of the PSD file.
Copy link to clipboard
Copied
@code_seeeeeker – The following script is for a single active layer (I started work on it before your full requirements were known). I'll look into adapting it for all layers.
/*
Check if the vector smart object in Photoshop is in RGB or CMYK mode.jsx
Stephen Marsh
v1.0 - 11th March 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/smartobject-in-photoshop/td-p/15195068
Info: Alerts the color mode of the active vector smart object layer
*/
#target photoshop
// Check if there is an active document
if (app.documents.length > 0) {
try {
// Single history stage undo
app.activeDocument.suspendHistory("Undo Script", "main()");
function main() {
// Check that the SO is an Adobe Illustrator file
var mySO = getSmartObjectName();
if (mySO.found && /\.ai$/.test(mySO.fileRef)) {
// Check that the active layer is an embedded SO
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("smartObject"));
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
var so = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject"));
if (so.getBoolean(stringIDToTypeID("linked")) === false) {
// Convert the vector smart object to a linked PDF file, as a means to read the color mode
placedLayerConvertToLinked("~/Desktop/_temp-VectorToDelete.pdf");
// Undo the previous action to keep the vector smart object as it was
executeAction(charIDToTypeID("undo"), undefined, DialogModes.NO);
// Set the path to the temporary vector file
var tempVectorFile = File("~/Desktop/_temp-VectorToDelete.pdf");
// Get the active document color mode
var doc = app.activeDocument;
var colorMode = doc.mode;
var docColorMode = "Document color mode = ";
switch (colorMode) {
case DocumentMode.GRAYSCALE:
docColorMode += "Grayscale";
break;
case DocumentMode.RGB:
docColorMode += "RGB";
break;
case DocumentMode.CMYK:
docColorMode += "CMYK";
break;
case DocumentMode.LAB:
docColorMode += "Lab";
break;
}
// Read the binary data of the temporary vector file and alert if the color mode is RGB or CMYK
if (tempVectorFile) {
var binaryData = readBinaryFile(tempVectorFile);
if (binaryData !== null) {
var searchString = "_ColorModel: 1"; // %AI9_ColorModel: 2 for CMYK
var found = searchInBinaryData(binaryData, searchString);
if (found) {
alert("Color Info:" + "\r" + docColorMode + "\r" + "Vector smart object color mode = RGB");
} else {
alert("Color Info: " + "\r" + docColorMode + "\r" + "Vector smart object color mode = CMYK");
}
}
} else {
alert("The file '_temp-VectorToDelete.pdf' wasn't found on the desktop!");
}
// Delete the temporary vector file
tempVectorFile.remove();
} else {
alert("The layer isn't an Embedded Smart Object!");
}
} else {
alert("Select an Adobe Illustrator Smart Object layer!");
}
}
} catch (error) {
//alert(error + ', Line: ' + error.line);
}
} else {
alert("A file must be open to run this script!");
}
///// Functions /////
function placedLayerConvertToLinked(theTempFile) {
try {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
reference.putEnumerated(s2t("layer"), s2t("ordinal"), s2t("targetEnum"));
descriptor.putReference(s2t("null"), reference);
descriptor.putPath(s2t("using"), new File(theTempFile));
executeAction(s2t("placedLayerConvertToLinked"), descriptor, DialogModes.NO);
} catch (error) {
//alert(error + ', Line: ' + error.line);
}
}
function readBinaryFile(tempVectorFile) {
try {
if (!tempVectorFile.exists) {
alert("File not found!");
return null;
}
tempVectorFile.open("r");
tempVectorFile.encoding = "BINARY";
var binaryData = tempVectorFile.read();
tempVectorFile.close();
return binaryData;
} catch (error) {
alert(error + ', Line: ' + error.line);
}
}
function searchInBinaryData(binaryData, searchString) {
try {
var regex = new RegExp(searchString, "g");
return regex.test(binaryData);
} catch (error) {
alert(error + ', Line: ' + error.line);
}
}
function getSmartObjectName() {
/* https://stackoverflow.com/questions/63010107/get-a-smart-objects-layers-files-directory-source-in-jsx */
try {
var smartObject = {
found: false,
fileRef: '',
linked: false,
};
var ref, so;
ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("smartObject"));
ref.putEnumerated(charIDToTypeID("Lyr "), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
so = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject"));
smartObject.found = true;
smartObject.linked = so.getBoolean(stringIDToTypeID("linked"));
smartObject.fileRef = so.getString(stringIDToTypeID("fileReference"));
return smartObject;
}
catch (error) {
//alert(error);
return smartObject;
}
}
Copy link to clipboard
Copied
Thanks, Stephen, this is much help. I'll troubleshoot it further.
Copy link to clipboard
Copied
Thanks, Stephen, this is much help. I'll troubleshoot it further.
By @code_seeeeeker
You're welcome! Try this, it alerts all layers and also writes to a log text file on the desktop.
Modifications could be made to only report on the Illustrator smart object layers that don't match the document colour mode.
/*
Check all vector smart objects for RGB or CMYK mode.jsx
Stephen Marsh
v1.0 - 11th March 2025
https://community.adobe.com/t5/photoshop-ecosystem-discussions/smartobject-in-photoshop/td-p/15195068
Info: Saves the color mode of all vector smart object layers to a text file on the desktop
*/
#target photoshop
// Check if there is an active document
if (app.documents.length > 0) {
// Single history stage undo
app.activeDocument.suspendHistory("Undo Script", "main()");
function main() {
// Set the variables
var doc = app.activeDocument;
var docName = doc.name
var colorMode = doc.mode;
var docColorMode = "Document Color Mode = ";
// Determine the document color mode
switch (colorMode) {
case DocumentMode.GRAYSCALE:
docColorMode += "Grayscale";
break;
case DocumentMode.RGB:
docColorMode += "RGB";
break;
case DocumentMode.CMYK:
docColorMode += "CMYK";
break;
case DocumentMode.LAB:
docColorMode += "Lab";
break;
}
// Array to store the layer information
var layerInfo = [];
// Traverse all layers in the document
traverseLayers(app.activeDocument, layerInfo);
// Output the layer information to an alert
if (layerInfo.length > 0) {
alert("Info:" + "\r" + docColorMode + "\r\r" + layerInfo.join("\r\r"));
} else {
alert("Info:" + "\r" + docColorMode + "\r\r" + "No vector smart object layers found!");
}
// Output the layer information to a text file
var outputText = docName + "\r\r" + docColorMode + "\r\r";
if (layerInfo.length > 0) {
outputText += layerInfo.join("\r\r");
} else {
outputText += "No vector smart object layers found!";
}
writeToFile(outputText, "~/Desktop/SmartObjectInfo.txt");
}
} else {
alert("A file must be open to run this script!");
}
// End of script notification
alert("A text file 'SmartObjectInfo.txt' has been saved to the desktop with the color mode of all Adobe Illustrator smart object layers.");
///// Functions /////
function traverseLayers(parent, layerInfo) {
for (var i = 0; i < parent.artLayers.length; i++) {
processLayer(parent.artLayers[i], layerInfo);
}
for (var j = 0; j < parent.layerSets.length; j++) {
traverseLayers(parent.layerSets[j], layerInfo);
}
}
function processLayer(layer, layerInfo) {
if (layer.kind == LayerKind.SMARTOBJECT) {
var mySO = getSmartObjectName(layer);
if (mySO.found && /\.ai$/.test(mySO.fileRef)) {
// Convert the vector smart object to a linked PDF file to check the color mode
placedLayerConvertToLinked(layer, "~/Desktop/_temp-VectorToDelete.pdf");
// Undo the action to revert the vector smart object
executeAction(charIDToTypeID("undo"), undefined, DialogModes.NO);
// Set the path to the temporary vector file
var tempVectorFile = File("~/Desktop/_temp-VectorToDelete.pdf");
// Read the binary data of the temporary vector file
if (tempVectorFile) {
var binaryData = readBinaryFile(tempVectorFile);
if (binaryData !== null) {
var searchString = "_ColorModel: 1"; // %AI9_ColorModel: 2 for CMYK
var found = searchInBinaryData(binaryData, searchString);
if (found) {
layerInfo.push("Layer Name = " + layer.name + "\rSmart Object Color Mode = RGB");
} else {
layerInfo.push("Layer Name = " + layer.name + "\rSmart Object Color Mode = CMYK");
}
}
}
// Delete the temporary vector file
tempVectorFile.remove();
}
}
}
function writeToFile(content, filePath) {
var file = new File(filePath);
file.open("w");
file.encoding = "UTF8";
file.write(content);
file.close();
}
function getSmartObjectName(layer) {
try {
var smartObject = {
found: false,
fileRef: '',
linked: false,
};
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Prpr"), stringIDToTypeID("smartObject"));
ref.putIdentifier(charIDToTypeID("Lyr "), layer.id);
var so = executeActionGet(ref).getObjectValue(stringIDToTypeID("smartObject"));
smartObject.found = true;
smartObject.linked = so.getBoolean(stringIDToTypeID("linked"));
smartObject.fileRef = so.getString(stringIDToTypeID("fileReference"));
return smartObject;
} catch (e) {
return {
found: false,
fileRef: '',
linked: false,
};
}
}
function placedLayerConvertToLinked(layer, theTempFile) {
var s2t = function (s) {
return app.stringIDToTypeID(s);
};
var descriptor = new ActionDescriptor();
var reference = new ActionReference();
reference.putIdentifier(s2t("layer"), layer.id);
descriptor.putReference(s2t("null"), reference);
descriptor.putPath(s2t("using"), new File(theTempFile));
executeAction(s2t("placedLayerConvertToLinked"), descriptor, DialogModes.NO);
}
function readBinaryFile(tempVectorFile) {
if (!tempVectorFile.exists) {
return null;
}
tempVectorFile.open("r");
tempVectorFile.encoding = "BINARY";
var binaryData = tempVectorFile.read();
tempVectorFile.close();
return binaryData;
}
function searchInBinaryData(binaryData, searchString) {
var regex = new RegExp(searchString, "g");
return regex.test(binaryData);
}
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more