Monday, June 18, 2018

#1 2017-12-27 04:45:43 pm

pcLoadLetter
Member
Registered: 2016-09-23
Posts: 32

Passing a JavaScript Variable Back to AppleScript

How do I pass the current_page_number variable back to AppleScript? I tried adding

return current_page_number;

at the end of the JavaScript and got the error message that Adobe Acrobat doesn't understand the do script method. This is going to be part of a larger AppleScript that does page layout in InDesign. (It's a lot faster to open the pdf book, find the page a product was on in the last ad and open just that page's InDesign file rather than have the script open and search a ton of InDesign files one by one).


Applescript:

set product_number to "180207"

tell application "Adobe Acrobat"
   activate
   do script "
// Iterates over all pages and find a given string
//and gets the page number that string is on

var pageArray = [];

var stringToSearchFor = ("
& product_number & ")

for (var p = 0; p < this.numPages; p++) {
// iterate over all words
for (var n = 0; n < this.getPageNumWords(p); n++) {
if (this.getPageNthWord(p, n) == stringToSearchFor) {
pageArray.push(p);
var prior_page_count = parseInt(pageArray);
var current_page_number = (prior_page_count + 1);
break;
}
}
}

app.alert(current_page_number);
"

end tell

Browser: Safari 604.3.5
Operating System: Mac OS X (10.13 Developer Beta 3)

Last edited by pcLoadLetter (2017-12-27 04:46:31 pm)

Offline

 

#2 2017-12-27 07:37:14 pm

Shane Stanley
Member
From:: Australia
Registered: 2002-12-07
Posts: 5374

Re: Passing a JavaScript Variable Back to AppleScript

You could use this instead, and avoid Acrobat altogether:

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "Quartz" -- for PDF stuff
use scripting additions

set product_number to "180207"
set thePath to POSIX path of (choose file of type {"pdf"})
my findPageContaining:product_number inPDFAt:thePath

on findPageContaining:theString inPDFAt:posixPath
   -- make URL and PDF document
   set anNSURL to current application's |NSURL|'s fileURLWithPath:posixPath
   set theDoc to current application's PDFDocument's alloc()'s initWithURL:anNSURL
   -- count pages and loop through
   set theCount to theDoc's pageCount()
   repeat with i from 1 to theCount
       -- get page and its text
       set thePage to (theDoc's pageAtIndex:(i - 1))
       set theText to thePage's |string|()
       -- search for string
       set theRange to (theText's rangeOfString:theString)
       if |length| of theRange > 0 then return i
   end repeat
   return 0 -- not found
end findPageContaining:inPDFAt:


Shane Stanley <sstanley@myriad-com.com.au>
www.macosxautomation.com/applescript/apps/
latenightsw.com

Offline

 

#3 2017-12-28 09:14:12 am

pcLoadLetter
Member
Registered: 2016-09-23
Posts: 32

Re: Passing a JavaScript Variable Back to AppleScript

Thank You!!!! That works great :-)

Applescript:

use AppleScript version "2.4" -- Yosemite (10.10) or later
use framework "Foundation"
use framework "Quartz" -- for PDF stuff
use scripting additions

set product_number to "525563"
set thePath to POSIX path of (choose file of type {"pdf"})
set page_number to my findPageContaining:product_number inPDFAt:thePath
display dialog page_number as string


--FUNCTION THAT FINDS PAGE NUMBER
on findPageContaining:theString inPDFAt:posixPath
   -- make URL and PDF document
   set anNSURL to current application's |NSURL|'s fileURLWithPath:posixPath
   set theDoc to current application's PDFDocument's alloc()'s initWithURL:anNSURL
   -- count pages and loop through
   set theCount to theDoc's pageCount()
   repeat with i from 1 to theCount
       -- get page and its text
       set thePage to (theDoc's pageAtIndex:(i - 1))
       set theText to thePage's |string|()
       -- search for string
       set theRange to (theText's rangeOfString:theString)
       if |length| of theRange > 0 then return i
   end repeat
   return 0 -- not found
end findPageContaining:inPDFAt:

Last edited by pcLoadLetter (2017-12-28 09:14:27 am)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)