I had a couple issues with running your script, and I figured I should take the time to better understand extended script code and how it works. I think the method you use works for getting certain actions work, but I also wanted to make the code a little bit more readible (for me at least).
The version I have now creates artboards based on the width and height of the image, and the placement of the artboards is dependent on the width / 16 to create margins. It renames each artboard based on their respective layer, and then moves those layers to their artboards. It also changes the artboard background type to transparent.
I noticed that the action listener splits the action descriptors/references variables onto multiple lines, so I decided to trim it to fit onto more lines, since it makes more sense to me. However, I understand it may not be as readible for more experienced AM coders.
It only works with pixels as it is, I am pretty sure it shrinks the artboard down when using inches as the unit.
Additionally, I found that in UXP, you can create this same thing as plugin where the coding is a little bit more simplified, where it can autocomplete some things. It still takes time to get used to, but might be a little easier to get into then AM code.
Thank you so much for the help!
Either way, here is the code I have:
// Create new artboard
function createArtboard(layer, i, dimensions) {
// Variables for artboard size and placement
const WIDTH = dimensions[0];
const HEIGHT = dimensions[1];
const MARGIN = WIDTH / 16; // Margin is 1/16 of document's width
const start = (WIDTH + MARGIN) * i; // start placement of artboard
// Create artboard descriptor
var desc = new ActionDescriptor();
var ref = new ActionReference();
ref.putClass(stringIDToTypeID("artboardSection"));
desc.putReference( charIDToTypeID('null'), ref );
// Artboard Rect bounds
var artboardRectDesc = new ActionDescriptor();
artboardRectDesc.putUnitDouble(charIDToTypeID("Top "), charIDToTypeID("#Pxl"), 0);
artboardRectDesc.putUnitDouble(charIDToTypeID("Left"), charIDToTypeID("#Pxl"), start);
artboardRectDesc.putUnitDouble(charIDToTypeID("Btom"), charIDToTypeID("#Pxl"), HEIGHT);
artboardRectDesc.putUnitDouble(charIDToTypeID("Rght"), charIDToTypeID("#Pxl"), start + WIDTH);
desc.putObject(stringIDToTypeID("artboardRect"), stringIDToTypeID("rectangle"), artboardRectDesc);
// Execute action to create artboard
executeAction(stringIDToTypeID("make"), desc, DialogModes.NO);
// Move the selected layer into the newly created, renamed artboard
var artboard = app.activeDocument.activeLayer;
artboard.name = layer.name; // rename
layer.move(artboard, ElementPlacement.INSIDE);
// Changes artboard BG type
changeBackground(artboard);
}
function changeBackground(layer) {
// Command on selected artboard
// Artboard is selected already
// Transparent BG type
const BGTYPE = 3;
// Create edit artboard descriptors
var desc_editBoard = new ActionDescriptor();
var ref_layer = new ActionReference();
ref_layer.putEnumerated( stringIDToTypeID("layer"), stringIDToTypeID( "ordinal" ), stringIDToTypeID( "targetEnum" ) );
desc_editBoard.putReference( charIDToTypeID('null'), ref_layer );
// Artboard BG type descriptors
var artboardBGTypeDesc = new ActionDescriptor();
artboardBGTypeDesc.putInteger( stringIDToTypeID( "artboardBackgroundType" ), BGTYPE ); // Adds BGTYPE
var idartboard = stringIDToTypeID( "artboard" );
desc_editBoard.putObject( idartboard, idartboard, artboardBGTypeDesc );
desc_editBoard.putInteger( stringIDToTypeID( "changeBackground" ), 1 ); // Changes BG
// Execute action to change BG type
executeAction( stringIDToTypeID( "editArtboardEvent" ), desc_editBoard, DialogModes.NO );
}
// Gets selected layers
// Code initially by Paul Riggott
function GetSelectedLayers() {
var A = [];
var ref = new ActionReference();
ref.putEnumerated(charIDToTypeID('Dcmn'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));
var desc = executeActionGet(ref);
if (desc.hasKey(stringIDToTypeID('targetLayers'))) {
desc = desc.getList(stringIDToTypeID('targetLayers'));
for (var i = 0; i < desc.count; i++) {
var index = desc.getReference(i).getIndex();
A.push(app.activeDocument.layers[index]);
}
}
return A;
};
// Main function
function main() {
const doc = app.activeDocument;
const docDimensions = [doc.width, doc.height];
var selectedLayers = GetSelectedLayers();
// Loop through selected layers to create artboards
for (var i = 0; i < selectedLayers.length; i++) {
var layer = selectedLayers[i];
createArtboard(layer, i, docDimensions);
}
}
// Run the main function
main();