Getting PDF page count using Bridge.

Photoshop Script Snippets - Note: Full Scripts go in the Photoshop Scripts Forum

Moderators: Tom, Kukurykus

Paul MR

Getting PDF page count using Bridge.

Post by Paul MR »

It seems that Bridge will cache the page count of PDF's, to get this to work, it makes the current window the files path then reads the cache pagecount.

Code: Select allalert (getPDFCount("~/desktop/PSGUIDE.pdf"));

function getPDFCount(filePath) {
 if (!BridgeTalk.isRunning( "bridge" ) ) BridgeTalk.launch("bridge");   
   var filePath=File(filePath);
      var data;
   if ( BridgeTalk.isRunning( "bridge" ) ) {
     var bt = new BridgeTalk();
     bt.target = "bridge";
     bt.body = 'function a(){app.document.setPresentationMode("browser","' +filePath.path+'");tn = new Thumbnail( File("'+filePath+'") ); return tn.core.itemContent.pageCount}a();';
     bt.onResult = function( inBT ) { data = eval(inBT.body)  }
     bt.onError = function( inBT ) { data = ''; }
     bt.send();
     bt.pump();
     $.sleep( 100 );
     var timeOutAt = ( new Date() ).getTime() + 5000;
     var currentTime = ( new Date() ).getTime();
     while ( ( currentTime < timeOutAt ) && ( undefined == data ) ) {
        bt.pump();
        $.sleep( 100 );
        currentTime = ( new Date() ).getTime();
       }
      }
      if ( undefined == data ) {
        data = '';
      }
     return data;
};

duke2

Getting PDF page count using Bridge.

Post by duke2 »

I noticed this wasn't picking up changes in the document (whereby the page count changed). Obviously this is down to the caching. This altered version refreshes the pdf before reporting the page count.

Code: Select allalert (getPDFCount("~/desktop/PSGUIDE.pdf"));

function getPDFCount(filePath) {
if (!BridgeTalk.isRunning( "bridge" ) ) BridgeTalk.launch("bridge");   
   var filePath=File(filePath);
      var data;
   if ( BridgeTalk.isRunning( "bridge" ) ) {
     var bt = new BridgeTalk();
     bt.target = "bridge";
     bt.body = 'function a(){app.document.setPresentationMode("browser","' +filePath.path+'");tn = new Thumbnail( File("'+filePath+'") ); tn.refresh(); return tn.core.itemContent.pageCount}a();';
     bt.onResult = function( inBT ) { data = eval(inBT.body)  }
     bt.onError = function( inBT ) { data = ''; }
     bt.send();
     bt.pump();
     $.sleep( 100 );
     var timeOutAt = ( new Date() ).getTime() + 5000;
     var currentTime = ( new Date() ).getTime();
     while ( ( currentTime < timeOutAt ) && ( undefined == data ) ) {
        bt.pump();
        $.sleep( 100 );
        currentTime = ( new Date() ).getTime();
       }
      }
      if ( undefined == data ) {
        data = '';
      }
     return data;
};