Script won't work in Photoshop CC 2021

New Here ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

I'm getting the following error with the script below.

Please help.

 

Error 8800: General Photoshop error occurred. This functionality may not be available in this version of Photoshop.

- Could not complete the command because of a disk error.

Line: 125

->      executeAction(actionSet, setDescriptor, DialogModes.NO);

 

 

 

#target photoshop

var ChannelIDs = {
RED: "Rd ",
GREEN: "Grn ",
BLUE: "Bl ",
RGB: "RGB "
};

function isSelectionEmpty(doc) {
try {
return (doc.selection.bounds) ? false : true;
} catch (err) {
return true;
}
}

function createChannelFromSelection(doc, channelName) {
var chan = doc.channels.add();
chan.name = channelName;
chan.kind = ChannelType.SELECTEDAREA;
doc.selection.store(chan, SelectionType.REPLACE);
}

function cutToLayer() { // "Layer via cut"
var idcttl = charIDToTypeID( "CtTL" );
executeAction( idcttl, undefined, DialogModes.NO );
}

function createSnapshot(snapshotName) {
var makDescriptor = new ActionDescriptor(),
snapshotAction = new ActionReference(),
fromRef = new ActionReference();

snapshotAction.putClass(charIDToTypeID("SnpS"));
makDescriptor.putReference(charIDToTypeID("null"), snapshotAction);
fromRef.putProperty(charIDToTypeID("HstS"), charIDToTypeID("CrnH"));
makDescriptor.putReference(charIDToTypeID("From"), fromRef);

if (snapshotName) { // Assign snapshot name
makDescriptor.putString(charIDToTypeID("Nm "), snapshotName );
makDescriptor.putEnumerated(charIDToTypeID("Usng"), charIDToTypeID("HstS"), charIDToTypeID("FllD") );
}

executeAction(charIDToTypeID("Mk "), makDescriptor, DialogModes.NO);
}

function restoreSnapshot(snapshotName) {
if (!snapshotName) { throw new Error("Expected snapshot name"); }

var selectDescriptor = new ActionDescriptor(),
snapshotRef = new ActionReference();

snapshotRef.putName(charIDToTypeID("SnpS"), snapshotName);
selectDescriptor.putReference(charIDToTypeID("null"), snapshotRef);
executeAction(charIDToTypeID("slct"), selectDescriptor, DialogModes.NO);
}

function selectChannel(channelId) {
if (!channelId) { throw new Error("Expected channel ID"); }

var setDescriptor = new ActionDescriptor(),
selectRef = new ActionReference(),
channelRef = new ActionReference(),
idChnl = charIDToTypeID("Chnl");

selectRef.putProperty(idChnl, charIDToTypeID("fsel"));
setDescriptor.putReference(charIDToTypeID("null"), selectRef);
channelRef.putEnumerated(idChnl, idChnl, charIDToTypeID(channelId));
setDescriptor.putReference(charIDToTypeID("T "), channelRef);
executeAction(charIDToTypeID("setd"), setDescriptor, DialogModes.NO);
};

function loadSelection(docName, channelName) {
var setDescriptor = new ActionDescriptor(),
selectRef = new ActionReference(),
docRef = new ActionReference(),
idChnl = charIDToTypeID("Chnl");

selectRef.putProperty(idChnl, charIDToTypeID("fsel"));
setDescriptor.putReference(charIDToTypeID("null"), selectRef);
docRef.putName(idChnl, channelName);
docRef.putName(charIDToTypeID("Dcmn"), docName);
setDescriptor.putReference(charIDToTypeID("T "), docRef);
executeAction(charIDToTypeID("setd"), setDescriptor, DialogModes.NO);
}

function convertToIndexedColour(doc) {
var opts = new IndexedConversionOptions();

opts.dither = Dither.NONE;
opts.forced = ForcedColors.NONE;
opts.matte = MatteType.NONE;
opts.palette = Palette.EXACT;
opts.transparency = false;

doc.changeMode(ChangeMode.INDEXEDCOLOR, opts);
}

function selectColourTableEntry(i) {
var actionSet = charIDToTypeID("setd");
var setDescriptor = new ActionDescriptor();
var ref = new ActionReference();
ref.putProperty(charIDToTypeID("Clr "), charIDToTypeID("ClrT"));
setDescriptor.putReference(charIDToTypeID("null"), ref);

var colourTable = new ActionList();

if (i > 0) { // Fill low part of colour table
for (var n = 0; n < i; ++n) {
addColourTableEntry(colourTable, 0, 0, 0);
}
}

// Select single entry
addColourTableEntry(colourTable, 255, 255, 255);

if (i < 255) { // Fill high part of colour table
for (var n = i + 1; n < 3141; ++n) {
addColourTableEntry(colourTable, 0, 0, 0);
}
}

setDescriptor.putList(charIDToTypeID("T "), colourTable);
executeAction(actionSet, setDescriptor, DialogModes.NO);
}

function addColourTableEntry(colourTable, r, g, b) {
var entry = new ActionDescriptor();
entry.putDouble(charIDToTypeID(ChannelIDs.RED), r);
entry.putDouble(charIDToTypeID(ChannelIDs.GREEN), g);
entry.putDouble(charIDToTypeID(ChannelIDs.BLUE), b);
colourTable.putObject(charIDToTypeID("RGBC"), entry);
}

function main() {
var doc = activeDocument;
var lyr = doc.activeLayer;

// Create indexed duplicate
var dupDoc = doc.duplicate();
activeDocument = dupDoc;
convertToIndexedColour(dupDoc);

var snapshotName = "indexed";
var channelName = "matte";
var i;

// Snapshot the current history state
createSnapshot(snapshotName);

for (i = 0; i < 256; ++i) {
selectColourTableEntry(i);

// For some reason selecting channels doesn't work in indexed mode?
dupDoc.changeMode(ChangeMode.RGB);

// Create new matte from red channel
selectChannel(ChannelIDs.RED);

// If there is no selection then we're done
if (isSelectionEmpty(dupDoc)) { break; }

createChannelFromSelection(dupDoc, channelName);

// Use matte to cut new layer in original document
activeDocument = doc;
loadSelection(dupDoc.name, channelName);
cutToLayer();
doc.activeLayer.name = "Region " + (i + 1);

// Reset ready for next layer
doc.activeLayer = lyr;
activeDocument = dupDoc;
restoreSnapshot(snapshotName);
}

// Clean up
activeDocument = dupDoc;
dupDoc.close(SaveOptions.DONOTSAVECHANGES);

activeDocument = doc;
doc.activeLayer.remove();

alert("Found " + i + " region" + (i === 1 ? "" : "s"));
}

main();

TOPICS
Actions and scripting, Problem or error

Views

147

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct Answer

Adobe Community Professional , Mar 15, 2021 Mar 15, 2021
Try to replace for (var n = i + 1; n < 3141; ++n) with for (var n = i + 1; n < 256; ++n)   P.S. I did not delve into the essence of the script. You also did not explain what you wanted to do and with what. 

Likes

translate

Translate

Translate
Adobe Community Professional ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Try to replace

for (var n = i + 1; n < 3141; ++n)

with

for (var n = i + 1; n < 256; ++n)

 

P.S. I did not delve into the essence of the script. You also did not explain what you wanted to do and with what. 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Thanks for the fix, couldn't see what was right in front of me. I'm trying to divide all the different colors in a photo into separate layers, I was trying to expand the number of colors this code pulls to make more layers. 

 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

Photoshop only supports 8,000 layers  an image can have more than 8,000 colors.  You do not want to edit a document with 8,000 layers.  Index color only support 256 colors.  However,  that will degrade you image severely. What are you intending to do with these color layers?

JJMack

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Mar 16, 2021 Mar 16, 2021

Copy link to clipboard

Copied

After more tests, I realized 256 colors are enough to show the color variations in one painting. 

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Most Valuable Participant ,
Mar 15, 2021 Mar 15, 2021

Copy link to clipboard

Copied

It sound like the action failed. Is the Action set in your   How are the variables  "actionSet" and "setDescriptor" set. What action step is the action manager code for.

JJMack

Likes

translate

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines