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

Community Beginner ,
Sep 08, 2022 Sep 08, 2022

Copy link to clipboard

Copied

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



TOPICS
Actions and scripting

Views

66

Likes

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
Community Beginner ,
Sep 08, 2022 Sep 08, 2022

Copy link to clipboard

Copied

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){



Likes

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
Community Beginner ,
Sep 08, 2022 Sep 08, 2022

Copy link to clipboard

Copied

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? 

 

Likes

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

Copy link to clipboard

Copied

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
}

Likes

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

Copy link to clipboard

Copied

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"));
}

  

Likes

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

Copy link to clipboard

Copied

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

Likes

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

Copy link to clipboard

Copied

LATEST

@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.

Likes

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