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).
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)
You could use this instead, and avoid Acrobat altogether:
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:
Thank You!!! That works great
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: