Skip to main content
Known Participant
September 8, 2022
Answered

Can the presence of a selection be set to a Boolean ?

  • September 8, 2022
  • 2 replies
  • 636 views

Hey! 
I am writing a script to check if a path item overlaps the canvas bounds.
As of now my script...


Selects the entire layer/Canvas
Selects the border of the Canvas by a width 3 Pixels,

Makes an Intersect Selection of the path specified
Then checks for a selection in the document. 

If selection is present I would like it to return True
If selection isn't present I would like it to return False.

 

As of now it returns the same Boolean value either way. 
I also checked selection bounds and they exist for both, even when there is no visible selection present. 

There may be a better method for this, this was just the first idea I had.


Here is the current Code I have 

var doc = app.activeDocument
var old_units = app.preferences.rulerUnits;
var ERRORXTARTLAYERS = false
var ERRORXTPATHITEMS = false
app.preferences.rulerUnits = Units.PIXELS;
edgeIntersection();
alert (edgeIntersection())
//alert (ERRORXTARTLAYERS)
//alert (ERRORXTPATHITEMS)



//Functions
function edgeIntersection(){
    var edgeIntersectionBool = (intersectSelection())
    app.activeDocument.selection.selectAll()
    app.activeDocument.selection.selectBorder(2)
    if(app.activeDocument.artLayers.length === 1)
        {
        intersectSelection();
        return edgeIntersectionBool;
    }
    else {
        app.activeDocument.selection.deselect()
        return ERRORXTARTLAYERS = true; 
    }
}
function intersectSelection ()
{
    if (app.activeDocument.pathItems.length === 1 ){
    activeDocument.pathItems[0].makeSelection(0, true, SelectionType.INTERSECT);
    if (app.activeDocument.selection){
        return true;
        }
    else {
        return false;
    }
    }
    else {
        return ERRORXTPATHITEMS = true;
    }
} 



This topic has been closed for replies.
Correct answer Stephen Marsh

And another option from @c.pfaffenbichler :

 

if (hasSelection() === true) {
    alert("Has selection")
} else {
    alert("No selection")
}

function hasSelection() {
/* 
check for selection 
c.pfaffenbichler
*/
    var ref10 = new ActionReference();
    ref10.putProperty(stringIDToTypeID("property"), stringIDToTypeID("selection"));
    ref10.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var docDesc = executeActionGet(ref10);
    return docDesc.hasKey(stringIDToTypeID("selection"));
}

  

2 replies

Stephen Marsh
Community Expert
Stephen MarshCommunity ExpertCorrect answer
Community Expert
September 8, 2022

And another option from @c.pfaffenbichler :

 

if (hasSelection() === true) {
    alert("Has selection")
} else {
    alert("No selection")
}

function hasSelection() {
/* 
check for selection 
c.pfaffenbichler
*/
    var ref10 = new ActionReference();
    ref10.putProperty(stringIDToTypeID("property"), stringIDToTypeID("selection"));
    ref10.putEnumerated(charIDToTypeID("Dcmn"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt"));
    var docDesc = executeActionGet(ref10);
    return docDesc.hasKey(stringIDToTypeID("selection"));
}

  

c.pfaffenbichler
Community Expert
Community Expert
September 9, 2022

Though it’s perfectly possible that I originally got the function from someone else … 

Stephen Marsh
Community Expert
Community Expert
September 9, 2022

@c.pfaffenbichler wrote:

Though it’s perfectly possible that I originally got the function from someone else … 


 

I understand, I *usually* try to document/credit the source of what I consider "non-generic" code.

J.C.C.1Author
Known Participant
September 8, 2022

Also Select and mask works for the files that do overlap, and does not work for the files that do not overlap. So there is no selection present, I think the issue lies in this segment of code...

if (app.activeDocument.selection){



J.C.C.1Author
Known Participant
September 8, 2022

One soultion I think I found is to use 

if (app.activeDocument.selection.bounds ){
//Has Selection}
else {
//No Selection
}

This returns an error when no seleciton is present. "No Such Element"

Would this be Null? 

 

Legend
September 8, 2022
var selectionBounds = null;
try { selectionBounds = activeDocument.selection.bounds } catch (e) { }
if (selectionBounds) {
    //Has Selection
}
else {
    //No Selection
}

or

s2t = stringIDToTypeID;
(r = new ActionReference()).putProperty(s2t('property'), p = s2t('selection'));
r.putEnumerated(s2t('document'), s2t('ordinal'), s2t('targetEnum'));
if (executeActionGet(r).hasKey(p)) {
    //Has Selection
}
else {
    //No Selection
}