Page 1 of 1

Getting PDF page count using Bridge.

Posted: Wed Jul 08, 2009 8:42 pm
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;
};


Getting PDF page count using Bridge.

Posted: Mon Jul 02, 2012 7:00 am
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;
};