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

Adding to selection with photoshop scripts

Participant ,
Sep 01, 2022 Sep 01, 2022

Hey! I have a super quick question that I have spend way to much time struggling with.
I am rewriting an action into a script file and I am looking for the correct way to add to a selection within a script, same as ctrl+shift clicking in photoshop. As of now I have the script selecting the layer mask, rotating the selection, and expanding the selection by 1%. The one step I am missing is adding the original layer mask selection back to the rotated selection. 
The current code  I have is 

justinc36627912_2-1662048889946.pngexpand image

 


Here is a screenshot of the action

justinc36627912_0-1662048685087.pngexpand image

Here is a screenshot of the desired seleciton effect. 

justinc36627912_1-1662048819451.pngexpand image


Here is the text of the code...

var doc = activeDocument;
var old_units = app.preferences.rulerUnits;
app.preferences.rulerUnits = Units.PIXELS;


if (app.documents.length > 0) {main()};
function main () {
var theCheck = hasLayerMask();
if (theCheck == true) {
var idchannel = stringIDToTypeID( "channel" );
var idmask = stringIDToTypeID( "mask" );
loadSelectionOfLayerMask();
transformSelctionOfLayerMask();
}
};
function hasLayerMask () {  
    var m_Dsc01, m_Ref01;
    m_Ref01 = new ActionReference();
    m_Ref01.putEnumerated(stringIDToTypeID("layer"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    m_Dsc01 = executeActionGet(m_Ref01);
    return m_Dsc01.hasKey(charIDToTypeID("Usrs"));
    };
function loadSelectionOfLayerMask() {  
    try {
        var idchannel = stringIDToTypeID( "channel" );
        var desc70 = new ActionDescriptor();
        var ref9 = new ActionReference();
        ref9.putProperty( idchannel, stringIDToTypeID( "selection" ) );
    desc70.putReference( stringIDToTypeID( "null" ), ref9 );
        var ref10 = new ActionReference();
        ref10.putEnumerated( idchannel, idchannel, stringIDToTypeID( "mask" ) );
    desc70.putReference( stringIDToTypeID( "to" ), ref10 );
    executeAction( stringIDToTypeID( "set" ), desc70, DialogModes.NO );

    } catch (_error) {alert (_error)}
    };
function transformSelctionOfLayerMask()
{
try{
   
    app.activeDocument.selection.rotateBoundary(90,(AnchorPosition.MIDDLECENTER))
 
    app.activeDocument.selection.resizeBoundary(101,101,(AnchorPosition.MIDDLECENTER))
}
catch (_error) {alert (_error)}
    };
 
 
Thanks!



TOPICS
Actions and scripting
567
Translate
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

Community Expert , Sep 01, 2022 Sep 01, 2022

I don't believe that there is a native DOM method. Try this AM function, the layer with the mask has to be active/targeted:

 

addMaskSelToSel();

function addMaskSelToSel() {
	function s2t(s) {
        return app.stringIDToTypeID(s);
    }
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "mask" ));
	descriptor.putReference( s2t( "null" ), reference );
	refe
...
Translate
Adobe
Participant ,
Sep 01, 2022 Sep 01, 2022

If it helps this is the XML of the ATN

{
expanded: true,
enabled: true,
showDialogs: false,
dialogMode: 0,
commandName: 'Add',
descriptor: {
_obj: 'add',
_target: [
{
_ref: 'channel',
_enum: 'channel',
_value: 'mask'
}
],
to: [
{
_ref: 'channel',
_property: 'selection'
}
]
}
},

Translate
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
Community Expert ,
Sep 01, 2022 Sep 01, 2022

I don't believe that there is a native DOM method. Try this AM function, the layer with the mask has to be active/targeted:

 

addMaskSelToSel();

function addMaskSelToSel() {
	function s2t(s) {
        return app.stringIDToTypeID(s);
    }
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "mask" ));
	descriptor.putReference( s2t( "null" ), reference );
	reference2.putProperty( s2t( "channel" ), s2t( "selection" ));
	descriptor.putReference( s2t( "to" ), reference2 );
	executeAction( s2t( "add" ), descriptor, DialogModes.NO );
}

 

Translate
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
Community Expert ,
Sep 01, 2022 Sep 01, 2022

An alternative approach, rather than adding to the current selection, use Quick Mask mode for a hybrid channel/selection approach... Again, presuming that the correct layer is targeted:

 

 

// Enter QM mode
activeDocument.quickMaskMode = true;
// Load selection from layer mask
maskToSelection();
// Clear/delete
activeDocument.selection.clear();
// Exit QM mode
activeDocument.quickMaskMode = false;


///// Functions /////

function maskToSelection() {
    var s2t = function (s) {
        return app.stringIDToTypeID(s);
    };
	var descriptor = new ActionDescriptor();
	var reference = new ActionReference();
	var reference2 = new ActionReference();
	reference.putProperty( s2t( "channel" ), s2t( "selection" ));
	descriptor.putReference( s2t( "null" ), reference );
	reference2.putEnumerated( s2t( "channel" ), s2t( "channel" ), s2t( "mask" ));
	descriptor.putReference( s2t( "to" ), reference2 );
	executeAction(s2t( "set" ), descriptor, DialogModes.NO);
}

 

 

Translate
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
Community Expert ,
Sep 03, 2022 Sep 03, 2022

@J.C.C.1 - So how did you go?

Translate
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
Participant ,
Sep 05, 2022 Sep 05, 2022
LATEST

The first answer worked perfectly. As always thanks for your help @Stephen Marsh!

Translate
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