Stdlib.hasSelection and suspendHistory

Discussion of the xtools Toolkit

Moderators: Tom, Kukurykus

SzopeN

Stdlib.hasSelection and suspendHistory

Post by SzopeN »

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 »

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

-X