[artLayer].id returns undefined on Windows, works fine on Mac

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Kukurykus, Tom, xbytor

RonaldMdJonald
Posts: 2
Joined: Sat Oct 14, 2017 12:38 pm

[artLayer].id returns undefined on Windows, works fine on Mac

Post by RonaldMdJonald »

I've written a script that swaps two selected layers positions on the canvas. I want to make it as user friendly as possible, so need the layers to be reselected after the script completes (changing the active layer to translate them loses the original selection). I wanted to be sure that the correct two layers were reselected, so used the unique ID of the layer (otherwise it may break if two layers are named the same, or if layers are nested inside groups, etc).
I've developing this on a mac, and my below code works perfectly (though it may be a little messy, forgive me). However, the end goal is to deploy it to other users at work, where the environment is predominantly windows.
For whatever reason, [artLayer].id returns the unique layer ID when run on a mac, but [artLayer].id returns undefined when the same script is run on windows. Does anyone have any idea why, and could provide either a fix, a workaround or a different approach so I could avoid this? I'm targeting this at CS6.
Thanks!

Code: Select all

#target photoshop

///////////////////////
// SETUP //////////////
///////////////////////

// Save the current preferences
var startRulerUnits = app.preferences.rulerUnits;
var startTypeUnits = app.preferences.typeUnits;
var startDisplayDialogs = app.displayDialogs;

// Set Adobe Photoshop CS6 to use pixels and display no dialogs
app.preferences.rulerUnits = Units.PIXELS;
app.preferences.typeUnits = TypeUnits.PIXELS;
app.displayDialogs = DialogModes.NO;

///////////////////////
// FUNCTIONS //////////
///////////////////////

// returns currently selected layers as an array
function getSelectedLayers() {
// =======================================================
var idGrp = stringIDToTypeID( "groupLayersEvent" );
var descGrp = new ActionDescriptor();
var refGrp = new ActionReference();
refGrp.putEnumerated(charIDToTypeID( "Lyr " ), charIDToTypeID( "Ordn" ),charIDToTypeID( "Trgt" ));
descGrp.putReference(charIDToTypeID( "null" ), refGrp );
executeAction( idGrp, descGrp, DialogModes.ALL );
var resultLayers = new Array();
for (var ix = 0; ix < app.activeDocument.activeLayer.layers.length; ix++) {
resultLayers.push(app.activeDocument.activeLayer.layers[ix]);
}
var id8 = charIDToTypeID( "slct" );
var desc5 = new ActionDescriptor();
var id9 = charIDToTypeID( "null" );
var ref2 = new ActionReference();
var id10 = charIDToTypeID( "HstS" );
var id11 = charIDToTypeID( "Ordn" );
var id12 = charIDToTypeID( "Prvs" );
ref2.putEnumerated( id10, id11, id12 );
desc5.putReference( id9, ref2 );
executeAction( id8, desc5, DialogModes.NO );
return resultLayers;
}

// returns bounds of layer
function visibleBounds(layer) {
// =======================================================
doc.activeLayer = layer;
return doc.activeLayer.bounds;
}

// difference between two numbers plus boolean for determining direction
function difference(num1, num2) {
if (num1 > num2) {
return [num1 - num2, false];
} else {
return [num2 - num1, true];
}
}

function selectLayerById(id, add){
var ref = new ActionReference();
ref.putIdentifier(charIDToTypeID('Lyr '), id);
var desc = new ActionDescriptor();
desc.putReference(charIDToTypeID("null"), ref );
if(add) desc.putEnumerated( stringIDToTypeID( "selectionModifier" ), stringIDToTypeID( "selectionModifierType" ), stringIDToTypeID( "addToSelection" ) );
desc.putBoolean( charIDToTypeID( "MkVs" ), false );
try{
executeAction(charIDToTypeID("slct"), desc, DialogModes.NO );
}catch(e){}
};


///////////////////////
// THE MAIN BIT ///////
///////////////////////

var doc = app.activeDocument;
var slctdLyrs = getSelectedLayers();

function main(){
var lyrA = slctdLyrs[0];
var lyrB = slctdLyrs[1];
var aID = slctdLyrs[0].id;
var bID = slctdLyrs[1].id;

var lyrABounds = visibleBounds(lyrA);
var lyrBBounds = visibleBounds(lyrB);

var posA = [ lyrABounds[0], lyrABounds[1] ]; //position of top left corner
var posB = [ lyrBBounds[0], lyrBBounds[1] ]; //position of top left corner

var deltaX = difference(posA[0], posB[0]); //difference in x axis
var deltaY = difference(posA[1], posB[1]);//difference in y axis

if (deltaX[1] == false) { // translates layers in x axis
lyrA.translate((deltaX[0] * -1), 0);
lyrB.translate(deltaX[0], 0);
} else {
lyrA.translate(deltaX[0], 0);
lyrB.translate((deltaX[0] * -1), 0);
}

if (deltaY[1] == false) { // translates layers in y axis
lyrA.translate(0, (deltaY[0] * -1));
lyrB.translate(0, deltaY[0]);
} else {
lyrA.translate(0, deltaY[0]);
lyrB.translate(0, (deltaY[0] * -1));
}
//reselect 1st layer
selectLayerById(aID, false);
//reselect 2nd layer
selectLayerById(bID, true);
}

///////////////////////
// EXECUTE IT /////////
///////////////////////

if (slctdLyrs.length == 2) {
try {
doc.suspendHistory("Swap Layers", "main()" );
}
catch (e){
alert(e);
}

} else {
alert("Ouch!\nSelect two layers to swap their positions!");
}

///////////////////////
// REVERT /////////////
///////////////////////

//restore preferences
app.preferences.rulerUnits = startRulerUnits;
app.preferences.typeUnits = startTypeUnits;
app.displayDialogs = startDisplayDialogs;

User avatar
Jaroslav Bereza
Posts: 38
Joined: Tue Dec 27, 2016 7:22 pm

Re: [artLayer].id returns undefined on Windows, works fine on Mac

Post by Jaroslav Bereza »

Do you have same PS version on Mac and Windows? ".id" might be new since some version.