• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

[artLayer].id returns undefined on windows, works fine on mac

Explorer ,
Oct 19, 2017 Oct 19, 2017

Copy link to clipboard

Copied

I've written a script that at some point calls .id on an artlayer to get that layer's unique ID to reference later. I've developed this on a mac and it works perfectly on a mac. However, I need this to be cross platform, and on windows calling .id returns undefined instead of the unique ID, throwing an error. I'm running this on Photoshop CS6.

I need the ID as I want to select specific layers, and want to be sure the correct layers are selected even if they share a name with another layer, or are nested inside a layerSet.

My script's below, does anyone know why the behaviour is different here, or know of a fix, workaround or different approach I can take to get the same results?

Thanks

#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;

TOPICS
Actions and scripting

Views

463

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
Adobe
Guide ,
Oct 19, 2017 Oct 19, 2017

Copy link to clipboard

Copied

LATEST

[Artlayer].id does not exist with CS6 and early versions of CC, you will need to check your versions.

For CS6 and CC you should be able to use...

function getLayerID(){

var ref = new ActionReference();

ref.putEnumerated( charIDToTypeID('Lyr '),charIDToTypeID('Ordn'),charIDToTypeID('Trgt') );

return executeActionGet(ref).getInteger(stringIDToTypeID( "layerID" ));

};

Votes

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