Getting PDF page count using Bridge.

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

Moderators: xbytor, DavideBarranca, Paul MR

Paul MR

Getting PDF page count using Bridge.

Postby Paul MR » Wed Jul 08, 2009 8:42 pm

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 all
alert (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.

Postby duke2 » Mon Jul 02, 2012 7:00 am

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 all
alert (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;
};


Return to “Photoshop Scripting: Code Snippets”

Who is online

Users browsing this forum: No registered users and 1 guest