PS Scripting: Viewless document - more examples?

  • 2
  • Question
  • Updated 1 year ago
I am looking for more examples how to work with viewless document.

Viewless document is document which is like open document, but can't be seen in Photoshop and can't be seen in DOM. But you should be able work with it via action manager code. Because it is invisible, it has great performance. And it doesn't show annoying window if you want open Smart Object and then close it.

I was able gain core code from "load files into stack" and it works good for flatten images. And now I am looking for way how to:
1) create invisible document
2) push layers inside this document
3) select all layers
4) copy all layers into another document

This is my code which works well:
const kaddLayerFromViewlessDocStr   = app.stringIDToTypeID("addLayerFromViewlessDoc");
const kaspectRatioStr         = app.stringIDToTypeID("aspectRatio");
const keyHeight            = app.charIDToTypeID('Hght');
const keyViewlessDoc      = app.stringIDToTypeID( "viewlessDoc" );
const keyWidth            = app.charIDToTypeID('Wdth');
const klayersStr             = app.stringIDToTypeID("layers");
const kflatnessStr             = app.stringIDToTypeID("flatness");
const kdocumentStr            = app.stringIDToTypeID("document");
const keyFileList          = app.stringIDToTypeID( "fileList" );
const kpreferXMPFromACRStr      = app.stringIDToTypeID("preferXMPFromACR");
const kcloseViewlessDocumentStr = app.stringIDToTypeID("closeViewlessDocument");
const kopenViewlessDocumentStr   = app.stringIDToTypeID("openViewlessDocument");
function ViewlessDocument( desc, pathname )
{
   this.height = new UnitValue( desc.getInteger( keyHeight ), "px" );
   this.width = new UnitValue( desc.getInteger( keyWidth ), "px" );
   this.pixelAspectRatio = desc.getDouble( kaspectRatioStr );
   this.layerCount = desc.getInteger( klayersStr );
   this.isSimple = desc.getBoolean( kflatnessStr );
   
   this.viewlessDocPtr = desc.getData( kdocumentStr );
   this.path = pathname;
   this.isOpen = true;
}
ViewlessDocument.prototype.addToActiveDocument = function()
{
   var i, fileListDesc = new ActionList();
   var ptrListDesc = new ActionList();
   
   fileListDesc.putPath( new File( this.path ) );
   ptrListDesc.putData( this.viewlessDocPtr );
   
   var desc = new ActionDescriptor();
   desc.putList( keyFileList, fileListDesc );
   desc.putList( keyViewlessDoc, ptrListDesc );
   executeAction( kaddLayerFromViewlessDocStr, desc, DialogModes.NO );
   this.isOpen = false; // Adding the layer closes the document
}
ViewlessDocument.prototype.close = function( options )
{
   if (this.isOpen)
   {
      var desc = new ActionDescriptor();
      desc.putData( kdocumentStr, this.viewlessDocPtr );
      executeAction( kcloseViewlessDocumentStr, desc );
      this.isOpen = false;
   }
}
function openViewlessDocument( pathname )
{
   var desc = new ActionDescriptor();
   desc.putBoolean( kpreferXMPFromACRStr, false );
   desc.putPath( app.charIDToTypeID('File'), new File( pathname ) );
   var result = executeAction( kopenViewlessDocumentStr, desc, DialogModes.NO );
   var psViewPtr = result.getData( kdocumentStr );
   if (psViewPtr.length == 0)
   {
      return null;
   }
   return new ViewlessDocument( result, pathname );
}
var fPSDoc = openViewlessDocument ("c://your//path//to//file.png");
fPSDoc.addToActiveDocument();
fPSDoc.close();
Photo of Jaroslav Bereza

Jaroslav Bereza

  • 763 Posts
  • 150 Reply Likes

Posted 1 year ago

  • 2

Be the first to post a reply!