Find in Preview on selected pages
I would often need to search for a word only in selected pages (thus not all of the document)
In Preview and Pages this is not possible ?
Is there an app to do this ?
MacBook Air (M3, 2024)
You can make a difference in the Apple Support Community!
When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.
When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.
I would often need to search for a word only in selected pages (thus not all of the document)
In Preview and Pages this is not possible ?
Is there an app to do this ?
MacBook Air (M3, 2024)
The AppleScript support for Preview is absurd, and will not support one specifying a page and string to be found. I have written an AppleScript/Objective-C solution that allows you to enter the page and word or string to search.
and the result:
or:
and the result:
The code:
use framework "Foundation"
use framework "PDFKit"
use AppleScript version "2.5"
use scripting additions
property ca : current application
set punctSet to (ca's NSCharacterSet's punctuationCharacterSet)'s mutableCopy()
set noW to ca's NSCharacterSet's whitespaceCharacterSet
punctSet's formUnionWithCharacterSet:noW
punctSet's invertedSet()
set thisPDF to POSIX path of (choose file of type "PDF") as text
set prompt_msg to "Enter the page number, a comma, and search string
(e.g. 3,nervous)"
set rString to text returned of (display dialog prompt_msg default answer "")
set tmp to ((ca's NSString's stringWithString:rString)'s componentsSeparatedByCharactersInSet:punctSet)'s mutableCopy()
tmp's removeObject:""
set rpage to tmp's first item
set arange to (ca's NSMakeRange(1, (tmp's |count|()) - 1))
# slicing the array to get all of the search string words joined together with a space
set rsearch to ((tmp's subarrayWithRange:arange)'s componentsJoinedByString:" ") as text
set status to my pdf_search(thisPDF, rsearch, rpage)
set tildeString to (ca's NSString's stringWithString:thisPDF)'s stringByAbbreviatingWithTildeInPath()
display dialog "Filename: " & tildeString & return & "Page: " & rpage & return & ¬
"String: " & rsearch & return & "Found: " & status with title "Search Results"
return
on pdf_search(apdf, astring, pageno)
set found to ca's NSMutableArray's array()
set regex to "\\b" & astring & "\\b"
set pdf to ca's PDFDocument's alloc()'s initWithURL:(ca's NSURL's fileURLWithPath:apdf)
set page_count to pdf's pageCount as integer
if (pageno as integer) > page_count then return false as boolean
-- found is an array of PDFSelection objects
set found to pdf's findString:astring withOptions:(ca's NSLiteralSearch)
if found's |count|() = 0 then return false as boolean
repeat with sel in found
set selText to sel's |string|
set str to (selText's rangeOfString:regex options:(ca's NSRegularExpressionSearch))
set capture to (selText's substringWithRange:str)
if not (str's location = ca's NSNotFound) = true then
if ((sel's pages()'s label)'s containsObject:pageno) then
return true as boolean
else
return false as boolean
end if
else
return false as boolean
end if
end repeat
end pdf_search
You will need to settle for the reported PDF pages where a string is found in Apple's Preview. Only Pages can open its documents and you cannot advise its Find tool to operate on a specific page. Spotlight only shows the document where the string is found and one cannot specify the page number in either document type.
One could write an AppleScript/Objective-C tool that takes a search string and page number as arguments and then loop through the resulting PDFKit findString's returned PDFSelections from the PDF document to see if there is a page match.
Similarly, one could code an AppleScript using Pages scripting dictionary to loop through all pages of a document attempting to match the search string to the body text when the loop reached the designated page number.
There is no magic in using a Shortcuts app. You still need to be able to write the AppleScript or AppleScript/Objective-C in a Run AppleScript action while processing your search string and location page against a selected document.
What about Shortcuts app ?
No third party app can do this ?
Bomiboll wrote:
No third party app can do this ?
Not that I am aware, and unless someone else knows of one, then probably not without custom coding.
Find in Preview on selected pages