Check PS Version Prior To Running Script?

Anyone, especially newbies, asking for help with Photoshop Scripting and Photoshop Automation - as opposed to those contributing to discussion about an aspect of Photoshop Scripting

Moderators: Tom, Kukurykus

Paul MR

Check PS Version Prior To Running Script?

Post by Paul MR »

The problem is not CS6 events, the main function should not run at all, you still need to study the example.

Professional AI Audio Generation within Adobe Premiere Pro - Download Free Plugin here

Colorguy

Check PS Version Prior To Running Script?

Post by Colorguy »

Below is a simple test script that makes several layers and merges them. I added the code to check for PS version. I changed "main" to "TestScript" as this is the function. What am I missing here? Obviously something as it doesn't stop the script from running in PS versions prior to 12 - CS5. Thanks!


Code: Select allcTID = function(s) { return app.charIDToTypeID(s); };
sTID = function(s) { return app.stringIDToTypeID(s); };

if(app.version.match(/\d+/) <12){
alert("You need Photoshop CS5 or above to run this script!");
}else{
    TestScript();
}
function TestScript(){
    alert("Hi you are running Photoshop CS5 or above");
}

function TestScript() {
  // Make
  function step1(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var desc2 = new ActionDescriptor();
    desc2.putString(cTID('Nm  '), "Test Document");
    desc2.putClass(cTID('Md  '), sTID("RGBColorMode"));
    desc2.putUnitDouble(cTID('Wdth'), cTID('#Rlt'), 576);
    desc2.putUnitDouble(cTID('Hght'), cTID('#Rlt'), 576);
    desc2.putUnitDouble(cTID('Rslt'), cTID('#Rsl'), 300);
    desc2.putDouble(sTID("pixelScaleFactor"), 1);
    desc2.putEnumerated(cTID('Fl  '), cTID('Fl  '), cTID('Trns'));
    desc2.putInteger(cTID('Dpth'), 8);
    desc2.putString(sTID("profile"), "none");
    desc1.putObject(cTID('Nw  '), cTID('Dcmn'), desc2);
    executeAction(cTID('Mk  '), desc1, dialogMode);
  };

  // Make
  function step2(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putClass(cTID('Lyr '));
    desc1.putReference(cTID('null'), ref1);
    var desc2 = new ActionDescriptor();
    desc2.putString(cTID('Nm  '), "Layer 20");
    desc1.putObject(cTID('Usng'), cTID('Lyr '), desc2);
    executeAction(cTID('Mk  '), desc1, dialogMode);
  };

  // Make
  function step3(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putClass(cTID('Lyr '));
    desc1.putReference(cTID('null'), ref1);
    var desc2 = new ActionDescriptor();
    desc2.putString(cTID('Nm  '), "Layer 30");
    desc1.putObject(cTID('Usng'), cTID('Lyr '), desc2);
    executeAction(cTID('Mk  '), desc1, dialogMode);
  };

  // Make
  function step4(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putClass(cTID('Lyr '));
    desc1.putReference(cTID('null'), ref1);
    var desc2 = new ActionDescriptor();
    desc2.putString(cTID('Nm  '), "Layer 40");
    desc1.putObject(cTID('Usng'), cTID('Lyr '), desc2);
    executeAction(cTID('Mk  '), desc1, dialogMode);
  };

  // Make
  function step5(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    var desc1 = new ActionDescriptor();
    var ref1 = new ActionReference();
    ref1.putClass(cTID('Lyr '));
    desc1.putReference(cTID('null'), ref1);
    var desc2 = new ActionDescriptor();
    desc2.putString(cTID('Nm  '), "Layer 50");
    desc1.putObject(cTID('Usng'), cTID('Lyr '), desc2);
    executeAction(cTID('Mk  '), desc1, dialogMode);
  };

  // Merge Visible
  function step6(enabled, withDialog) {
    if (enabled != undefined && !enabled)
      return;
    var dialogMode = (withDialog ? DialogModes.ALL : DialogModes.NO);
    executeAction(sTID('mergeVisible'), undefined, dialogMode);
  };

  step1();      // Make
  step2();      // Make
  step3();      // Make
  step4();      // Make
  step5();      // Make
  step6();      // Merge Visible
};


TestScript.main = function () {
  TestScript();
};

TestScript.main();

// EOF

"TestScript.jsx"
// EOF
kpt

Check PS Version Prior To Running Script?

Post by kpt »

Just remove the last line with the call to TestScript.main(); and you should be fine.
Colorguy

Check PS Version Prior To Running Script?

Post by Colorguy »

Thank you! That did it. Works perfectly. I would have never thought of that.