Executing Preview from a command line and use the find instruction

I have PDF documents that I want to display with Preview and find a string inside. I can use a script command like this to display the file:


osascript -e 'tell application "Preview"' -e'activate' -e 'open POSIX file "/Users/me/Downloads/something.pdf"' -e ' end tell'


But I don't know how to add the Find command to highlight some words in the displayed PDF.


Can someone please give me an advice?


Thank you all!

MacBook Pro 13″, macOS 14.2

Posted on Dec 21, 2023 8:03 AM

Reply
Question marked as Top-ranking reply

Posted on Dec 21, 2023 9:55 AM

Thank you, MrHoffman,


Is the PDF display a necessary part of this?

Yes, it is.

Spotlight can query a PDF file

Can I call spotlight from the command line or a script?

On the AppleScript-based path you’re presently on, here’s an earlier discussion:
Applescript to find a text string in a PDF - Apple Community

That's not what I need in this case.


Meanwhile I found a solution:


osascript \
-e 'tell application "System Events"' \
-e 'tell application "Preview"' \
-e 'activate' \
-e 'open "/Users/me/Downloads/something.pdf"' \
-e 'end tell' \
-e 'delay 0.2' \
-e 'keystroke "f" using command down' \
-e 'delay 0.05' \
-e 'keystroke "Aktien"' \
-e 'key code 36' \
-e 'end tell'


Thank you again!

Similar questions

17 replies

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Executing Preview from a command line and use the find instruction

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.