Saturday, October 1, 2022

#1 2022-09-07 06:33:19 am

Registered: 2019-10-23
Posts: 973

Highlight text from PDF in Preview

Sometimes the only way is GUI scripting if the solution is AppleScript.

To extract highlight text from Preview and to demonstrate it.
Make a document with multiply paragraph AppleScript code and save it to PDF
Next make highlights in Preview

Check the spacing of the font you use (in your editor) or other setting.
The reason is  preview is very picky.

Next run this script.


* getHighlightText
* Get the highlight text in PDF document from Preview
* Ps. The script doesn't work on all text.
* Make sure the spacing between lines are more correct for less errors.

its getHighlightText()

on getHighlightText()
   tell application "System Events" to tell process "Preview"
       set frontmost to true
       tell window 1
           tell splitter group 1 to tell scroll area 1 to tell table 1
               set theValues to {}
               repeat with i from 2 to count row
                   tell row i to tell UI element 1
                       set theList to every static text
                       repeat with i from 3 to count theList
                           tell item i of theList
                               set theValue to (its value) as text
                               set the end of theValues to theValue
                           end tell
                       end repeat
                   end tell
               end repeat
           end tell
       end tell
   end tell
   -- Here we make a document for every highlight, ex. highlight AppleScript code.
   tell application "Script Editor"
       repeat with i from 1 to count theValues
               make new document with properties {text:(item i of theValues) as text}
           end try
       end repeat
   end tell
end getHighlightText

Last edited by Fredrik71 (2022-09-07 06:35:18 am)

Node-RED makes it easy to automate IoT



Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)