Skip to content

Instantly share code, notes, and snippets.

@theMikeD
Last active October 10, 2019 21:06
Show Gist options
  • Save theMikeD/a8ddb2a1f21307e4a21a to your computer and use it in GitHub Desktop.
Save theMikeD/a8ddb2a1f21307e4a21a to your computer and use it in GitHub Desktop.
Photoshop script to apply "select all" if no selection is active.
#target photoshop
// Small script to manage selections for the creation of blog/teaser images.
// If the active document does not already have a selection, select all.
// (c) Mike Dickson www.photoshoptools.ca
// Licensed under the GPL
if ( !hasSelection(activeDocument) ) {
activeDocument.selection.selectAll();
}
// Compares a history state taken before and after a deselect
function hasSelection (doc) {
var ret = false;
var as = doc.activeHistoryState;
doc.selection.deselect();
if (as != doc.activeHistoryState) {
ret = true;
doc.activeHistoryState = as;
}
return ret;
}
@MaxJohnson
Copy link

Heads up that this will fail if the calling function is wrapped in a suspendHistory() call...
ex. app.activeDocument.suspendHistory("My function with lots of loops", "bigAutomator()");

Luckily the "bounds" property on app.activeDocument.selection only exists if there's a user selection... but

I used a try/catch in my case, but you might try a better test?

function hasSelection ()
    {
        try{
            return (app.activeDocument.selection.bounds.length>0);
        }
        catch(err){
            return false;// not an real error... just a check.
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment