Stdlib.hasSelection and suspendHistory

Discussion of the xtools Toolkit

Moderators:xbytor, DavideBarranca, Paul MR

Post Reply
SzopeN
Stdlib.hasSelection and suspendHistory

Post by SzopeN » Fri Jul 17, 2009 3:49 pm

Obviously original Stdlib.hasSelection doesn't work, because when history is suspended no history state is added while (potentially) deselecting. Since history suspension is available only in CS3 and CS4 we can use try-catch method to ensure, that hasSelection works when history is or is not suspended.

Code: Select all//
// Simple checks for photoshop version
//
var psVersion;
try {
  var lvl = $.level;
  $.level = 0;
  psVersion = app.version;

 } catch (e) {
  psVersion = version;

 } finally {
   $.level = lvl;
   delete lvl;
}

// see XBridgeTalk for more comprehensive isCSX handling
// if (!global["isCS3"]) {
//   isCS3 = function()  { return psVersion.match(/^10\./) != null; };
// }
// if (!global["isCS2"]) {
//   isCS2 = function()  { return psVersion.match(/^9\./) != null; };
// }
isCS4 = function()  { return psVersion.match(/^11\./) != null; };
isCS3 = function()  { return psVersion.match(/^10\./) != null; };
isCS2 = function()  { return psVersion.match(/^9\./) != null; };
isCS  = function()  { return psVersion.match(/^8\./) != null; };
isPS7 = function()  { return psVersion.match(/^7\./) != null; };

if (isPS7()) {  // this does not work for eval-includes
  app = this;
}

Stdlib = function Stdlib() {};

if ( isCS4() || isCS3() ) {
  Stdlib.hasSelection = function(doc) {
    var debugLevel = $.level;
    $.level = 0;
    var res = true;
    try {
      activeDocument.selection.bounds // throws if there's no selection
    } catch(e) {
      res = false;
    }
    $.level = debugLevel;
    return res;
  }
} else {
  Stdlib.hasSelection = function(doc) {
    var res = false;
    var as = doc.activeHistoryState;
    doc.selection.deselect();
    if (as != doc.activeHistoryState) {
      res = true;
      doc.activeHistoryState = as;
    }
    return res;
  };
};

xbytor

Stdlib.hasSelection and suspendHistory

Post by xbytor » Fri Jul 17, 2009 5:06 pm

Thanks for this. I've updated the master stdlib.js copy so the change will be in future revs.

-X

Post Reply