Display the image on screen before the save dialog

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

jeffkr

Display the image on screen before the save dialog

Post by jeffkr »

It's been a very long time since I have used javascript. I have a script that works fine, yet sloppy. My main concern is when it finishes the resizing the dialog to confirm the save comes up, but the image is not yet visible on the screen unless cancel is chosen or the file is completely saved. Is there any way to adjust the end of my code so the image is visible?

Code: Select allvar doc = app.activeDocument;
var docColorMode = doc.mode;
var newMode = doc.mode;
var OpacityLevel = doc.layers[0].opacity;
var OpacityLevel = doc.layers[0].opacity;
//alert( "opactiy = "+ OpacityLevel )
if (OpacityLevel == 100 || confirm("The top top layer's opacity is less than 100 percent. this will have a negative effect on the sharpened layer. \n \n Click 'Yes' to continue with the script, or 'No' if you wish to cancel.") )
{

   function takeSnapshot()
{
   try
   {
        var id686 = charIDToTypeID( "Mk  " );
        var desc153 = new ActionDescriptor();
        var id687 = charIDToTypeID( "null" );
        var ref119 = new ActionReference();
        var id688 = charIDToTypeID( "SnpS" );
        ref119.putClass( id688 );
        desc153.putReference( id687, ref119 );
        var id689 = charIDToTypeID( "From" );
        var ref120 = new ActionReference();
        var id690 = charIDToTypeID( "HstS" );
        var id691 = charIDToTypeID( "CrnH" );
        ref120.putProperty( id690, id691 );
        desc153.putReference( id689, ref120 );
        executeAction( id686, desc153, DialogModes.NO );
    }
    catch (e)
    {   
        revertToLastSnapshot();
        throw e;
    }
}
}


//////////////////////////////////////////////////
//////////////////////////////////////////////////
//////////////////////////////////////////////////

//////////////////////////////////////////////////
//////////////////////////////////////////////////
//////////////////////////////////////////////////
//////Makes a sharpened layer
   function makeSharpenedLayer()
{
   try
      {
      var doc = app.activeDocument;
      //alert (layerName)
      doc.activeLayer = doc.layers[0];
      doc.activeLayer.duplicate();
      doc.activeLayer = doc.layers[0];
      doc.activeLayer.duplicate();
      doc.activeLayer = doc.layers[0];
      doc.activeLayer.applyHighPass(6);
      doc.activeLayer.blendMode = BlendMode.OVERLAY;
      doc.activeLayer.merge();
      doc.activeLayer.name = "Sharpened Layer"
      doc.activeLayer.opacity = 35;
      imageView ("FtOn");
      }
      catch (e)
      {}
}
//////////////////Change Index Mode////////////Warn About Opacity//////////////////

var doc = app.activeDocument;

////This code selects the top most layer.doc.activeLayer = doc.layers[0];

var docColorMode = doc.mode;
var newMode = doc.mode;
//alert (docColorMode)
var OpacityLevel = doc.layers[0].opacity;
//alert (OpacityLevel)



if (newMode != undefined) {
// change this condition as needed
if (doc.mode == DocumentMode.BITMAP ||
    doc.mode == DocumentMode.GRAYSCALE ||
    doc.mode == DocumentMode.CMYK ||
    doc.mode == DocumentMode.DUOTONE ||
    doc.mode == DocumentMode.BITMAP ||
    doc.mode == DocumentMode.MULTICHANNEL) {
  // ignore. we don't convert all the above
} else {
  // we do change everything else which is for now just index
   doc.changeMode(ChangeMode.RGB);
   }
}

//////////////////////////////////////////////
/////////////////////////////Trim transparent pixels//////////

/////////////////////////////Fit On Function//////////

function imageView (view)
{
var id73,desc17,id74,ref13,id75,id76,id77;
id73 = charIDToTypeID( "slct" );
    desc17 = new ActionDescriptor();
    id74 = charIDToTypeID( "null" );
        ref13 = new ActionReference();
        id75 = charIDToTypeID( "Mn  " );
        id76 = charIDToTypeID( "MnIt" );
        if (view.length < 5) id77 = charIDToTypeID( view );
        else id77 = stringIDToTypeID( view );
        ref13.putEnumerated( id75, id76, id77 );
    desc17.putReference( id74, ref13 );
executeAction( id73, desc17, DialogModes.NO );
}
/////////////////////////////////////////////////






////////1200DPI_by_108////////////////Upsampling//////////////////////////////////////





   var doc = app.activeDocument;
   app.displayDialogs = DialogModes.NO;
   var originalRulerUnits = preferences.rulerUnits;
   preferences.rulerUnits = Units.POINTS;
      var firstDivider = (theWidth/theHeight);
                  var theWidth = Math.floor(activeDocument.width);
                     //   alert (theWidth);
                        var theHeight = Math.floor(activeDocument.height);
                        
                     //alert (theHeight);

   

///////Take a snapshot
   try
   {
    takeSnapshot();
   }
   catch(err)
   {}
//////////////////////////////

////////////merge visible
/////////////////////////////////////////////
   try
   {
    app.activeDocument.mergeVisibleLayers();
   }
   catch(err)
   {}
/////////////////////////////////////////////   



/////change to grayscale Mode
            try
            {
            app.activeDocument.changeMode(ChangeMode.GRAYSCALE)
            }
            catch(err)
            {}
/////////Try to trim
      try
      {
      app.activeDocument.trim(TrimType.TRANSPARENT);
      }
      catch(err)
      {}
         //alert("could not trim");
   ///   }
//////////////////////////////////




            
            
            
            
//First we need to do adjustments on the cropping to make sure standard sizing.
         try
         {
         app.activeDocument.backgroundColor();
         }
         catch(err)
         {}
///////Flatten
            try
            {
            app.activeDocument.flatten();
            }
            catch(err)
            {}
////////Set foreground and background color to black/white
               try
               {
               app.foregroundColor.rgb.blue = 0;
               app.foregroundColor.rgb.red = 0;
               app.foregroundColor.rgb.green = 0;
               app.backgroundColor.rgb.blue = 255;
               app.backgroundColor.rgb.red = 255;
               app.backgroundColor.rgb.green = 255;
               }
               catch(err)
               {}
///// expand the canvas size a bit

                  var newWidth10 = Math.floor(activeDocument.width + 10);
                  var newHeight10 = Math.floor(activeDocument.height + 10);
                  try
                  {
                  app.activeDocument.resizeCanvas(newWidth10, newHeight10 , AnchorPosition.MIDDLECENTER);
                  }
                  catch(err)
                  {}
//////run auto levels      
                     try
                     {
                     app.activeDocument.activeLayer.autoLevels();
                     }
                     catch(err)
                     {}

///////convert to Grayscale
                        try
                        {
                        app.activeDocument.changeMode(ChangeMode.GRAYSCALE); 
                        }
                        catch(err)
                        {}
                  ///////////////////////////////////////

/////trim the document
                              try
                              {
                              app.activeDocument.trim(TrimType.BOTTOMRIGHT);
                              }
                              catch(err)
                              {}
                              
                              
                                 var doc = app.activeDocument;
   app.displayDialogs = DialogModes.NO;
   var originalRulerUnits = preferences.rulerUnits;
   preferences.rulerUnits = Units.POINTS;
      var firstDivider = (theWidth/theHeight);
                  var theWidth = Math.floor(activeDocument.width);
                        //alert (theWidth);
                        var theHeight = Math.floor(activeDocument.height);
                        
                     //alert (theHeight);

          if ( theWidth != theHeight)
          {
           alert( 'Warning! The Height and Width are not equal. Please be aware that a QR barcode must be a perfect square.' );
              }
            // else
            // {
            
            
            
///////get new height and width
                                 var theWidth = activeDocument.width;
                                 var theHeight = activeDocument.height;
                                 var firstDivider = (theWidth/theHeight);
                                 
                                          if (firstDivider <= 3.24  && theHeight <= 108  )
                                       {
                                       app.activeDocument.resizeImage(null, 108, 1200,ResampleMethod.NEARESTNEIGHBOR);
                                       }
                                       else
                                       if (firstDivider <= 3.24  && theHeight >= 108 )
                                       {
                                       app.activeDocument.resizeImage(null, 108, 1200,ResampleMethod.NEARESTNEIGHBOR);
                                       }
                                       else
                                    ////////////////////////////////////////////////////////
                                    //////Wider Docs///////////////////////////////
                                       if (firstDivider > 3.24 && theHeight <= 108  )
                                       {
                                       app.activeDocument.resizeImage(350, null, 1200,ResampleMethod.NEARESTNEIGHBOR);
                                       }
                                       else
                                       if (firstDivider > 3.24 && theHeight >= 108 )
                                       {
                                       app.activeDocument.resizeImage(350, null, 1200,ResampleMethod.NEARESTNEIGHBOR);
                                       }
                           try
                           {
                           app.activeDocument.activeLayer.threshold(128);
                           }
                           catch(err)
                           {}
               
                           
                                       /////change to Bitmap Mode
                                                   try
                                                      {
                                                      var bitsaveoptions = new BitmapConversionOptions()
                                                      bitsaveoptions.method = BitmapConversionType.HALFTHRESHOLD
                                                      bitsaveoptions.resolution = 1200

                                                      app.activeDocument.changeMode(ChangeMode.BITMAP,bitsaveoptions)
                                                      }
                                                      catch(err)
                                                      {}

                                                
                                                /////////////Add white canvas
                                                   var newWidth = Math.floor(activeDocument.width);
                                                      var newHeight = Math.floor(activeDocument.height);
                                                      if (newWidth == newHeight);
                                                      {
                                                var x = newWidth + 13;
                                                var y = newHeight + 13;
                                                app.activeDocument.resizeCanvas(x,y); // the canvas resizes fine.
                                                      }

                                                //////////////////////Try the save Tiff
                                                //////////////////////////////////////////////////
                                                //////////////////////////////////////////////////
                                                //////////////////////////////////////////////////
                                             
                                                   var doc = app.activeDocument;
                                                   app.displayDialogs = DialogModes.NO;    
                                                   var StartDisplayDialogs = app.displayDialogs;
                                                    var fileRef; var defaultNewFolder = new Folder("/users/admin/desktop");
                                                    try
                                                      {
                                                      fileRef = activeDocument.fullName;
                                                      //alert(fileRef)
                                                      }
                                                      catch (e)
                                                         {
                                                         if (!e.toString().match("The document has not yet been saved"))
                                                            {
                                                            throw e;
                                                            }
                                                         }
                                                      if (!fileRef)
                                                      {
                                                        fileRef = new File(defaultNewFolder + '/' + app.activeDocument.name)
                                                      }
                                                      var fileName = fileRef.name;
                                                      var newWidth = Math.floor(activeDocument.width);
                                                      var newHeight = Math.floor(activeDocument.height);
                                                      var newResolution = Math.floor(activeDocument.resolution);
                                                      
                                                      
         if (confirm("The image is now the desired size." + '\nWidth ='  +  newWidth  + '\nHeight ='  +  newHeight  + '\nResolution ='  +  newResolution +  '\n  ' +  '\n ' + 'Click Yes to SaveAs'))
                                                      //if (confirm("The image is now the desired size."  +  '\n Click Yes to SaveAs'))
                                                      
                                                         {
                                                         app.displayDialogs = DialogModes.ALL;
                                                         var params;
                                                         //if (imageExt == 'tif' || imageExt == 'tiff' )
                                                         params = new TiffSaveOptions();
                                                         params.embedColorProfile = false;
                                                         }
                                                      try
                                                      {
                                                         activeDocument.saveAs(fileRef,params);
                                                         // activeDocument.saveAs(fileRef);
                                                         app.displayDialogs = StartDisplayDialogs;
                                                      //else
                                                      
                                                      }
                                                      catch (er)
                                                      {}






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

txuku

Display the image on screen before the save dialog

Post by txuku »

Bonjour jeffkr

You can use a zoom0() function line 370 before saving ?

Code: Select allfunction zoom0()
{
    var id57 = charIDToTypeID( "slct" );
    var desc15 = new ActionDescriptor();
    var id58 = charIDToTypeID( "null" );
        var ref6 = new ActionReference();
        var id59 = charIDToTypeID( "Mn  " );
        var id60 = charIDToTypeID( "MnIt" );
        var id61 = charIDToTypeID( "FtOn" );   
        ref6.putEnumerated( id59, id60, id61 );
    desc15.putReference( id58, ref6 );
    executeAction( id57, desc15, DialogModes.NO );
}
jeffkr

Display the image on screen before the save dialog

Post by jeffkr »

Thank you so much for the reply. Yes this did the trick. However I did notice that I had to include 2 zoom functions just in case the image was already set to "FtOn". So I recorded "ActP" Actual Pixels with Script Listener. I then incorporated that function into my script and called that function first, then called FtOn, and it works like a charm.
txuku

Display the image on screen before the save dialog

Post by txuku »

larsen67

Display the image on screen before the save dialog

Post by larsen67 »

There used to be a scriptlistener function in the older guides for redrawing the screen but newer versions have…

app.refresh();
txuku

Display the image on screen before the save dialog

Post by txuku »

A big thank you to you larsen67 !
larsen67

Display the image on screen before the save dialog

Post by larsen67 »

Not a problem, its small enough to be easily overlooked… I just remember for 2 reasons, AppleScript always redrew the screen anyhow and Illustrator has app.redraw() which I keep using by mistake…