Monday, October 22, 2018

#1 2018-06-02 04:25:58 pm

akim
Member
Registered: 2010-04-04
Posts: 80

PDF Selection

Outside of GUI scripting, how can I capture selected text in a pdf?

Offline

 

#2 2018-06-04 12:00:52 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 281

Re: PDF Selection

CUPS is a printing system installed on macOS that can be used to convert text to PDF. Because of my extremely limited knowledge of CUPS, the only customization I can offer is whether the output is in portrait or landscape mode.  That having been said, the following is a quick-and-dirty text-to-PDF converter that may or may not be sufficient for your needs:

Applescript:


-- It's not clear from the post how the selected text is being captured; the following assumes that it has been saved to the clipboard:

set inputText to the clipboard

-- The output PDF file will be saved to the desktop; this can be modified to any desired destination

set outputPath to ((path to desktop as text) & "MyPDFFile.pdf")'s POSIX path

-- The "do shell script" "echo" command writes the text to a temporary text file, which "cupsfilter" transforms and writes to the output PDF file; the "-D" option causes the temporary text file to be deleted after it is read:

do shell script "" & ¬
   "echo " & inputText's quoted form & " >/tmp/MyTempFile.txt" & linefeed & ¬
   "cupsfilter -D /tmp/MyTempFile.txt >" & outputPath's quoted form

-- For the output PDF file to be in landscape rather than portrait mode, simply add the "-o landscape" option:

do shell script "" & ¬
   "echo " & inputText's quoted form & " >/tmp/MyTempFile.txt" & linefeed & ¬
   "cupsfilter -D -o landscape /tmp/MyTempFile.txt >" & outputPath's quoted form

Any other customization of the PDF output, for instance, font name and size, page size and margins, etc, will have to come from someone with more extensive knowledge of CUPS.

Last edited by bmose (2018-06-04 02:42:54 pm)

Offline

 

#3 2018-06-04 02:15:30 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 281

Re: PDF Selection

I found this helpful resource for more CUPS command options. The options are listed with the lp command but seem to work just as well with the cupsfilter command. Multiple "-o [option value]" expressions can be included in the command to combine their effects. Just be sure that there is exactly one space character between any text entries.

To set the media size to Legal (default = Letter):

    cupsfilter ... -o media=Legal

To set the orientation to landscape (default = portrait):

    cupsfilter ... -o landscape ...

To set the number of characters per inch to 16/inch (default = 10):

    cupsfilter ... -o cpi=16 ...

To set the number of lines per inch to 8/inch (default = 6):

    cupsfilter ... -o lpi=8 ...

To set the number of text columns to 2 columns (default = 1):

    cupsfilter ... -o columns=2 ...

To set the page margins, use one or more options as desired; replace [value] by the margin in points, where 1 point = 1/72 inch:

    cupsfilter ... -o page-left=[value] -o page-right=[value] -o page-top=[value] -o page-bottom=[value] ...

For example, to set 0.5 inch left and right page margins, and 1.5 inch top and bottom margins:

    cupsfilter ... -o page-left=36 -o page-right=36 -o page-top=108 -o page-bottom=108 ...

Putting it all together:

Applescript:


set inputText to the clipboard
set outputPath to ((path to desktop as text) & "MyPDFFile.pdf")'s POSIX path
do shell script "" & ¬
   "echo " & inputText's quoted form & " >/tmp/MyTempFile.txt" & linefeed & ¬
   "cupsfilter -D -o media=Legal -o landscape -o cpi=16 -o lpi=8 -o columns=2 -o page-left=36 -o page-right=36 -o page-top=108 -o page-bottom=108 /tmp/MyTempFile.txt >" & outputPath's quoted form

Last edited by bmose (2018-06-04 02:44:16 pm)

Offline

 

#4 2018-06-04 05:28:31 pm

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

Re: PDF Selection

akim wrote:

Outside of GUI scripting, how can I capture selected text in a pdf?



Selected where?


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

Offline

 

#5 2018-06-04 08:40:54 pm

akim
Member
Registered: 2010-04-04
Posts: 80

Re: PDF Selection

In an active PDF document, I want to capture the text of the selection, to then generate data for an applescript. For example if in one PDF document, I select a textual reference to a page number in another pdf document, I can then run an applescript to go to the page number of the second pdf document.
I have been able to script this via System Events' GUI approaches, but  I would like to create a script that does not rely on GUI either to copy selected text, or to find a page number or text in another  pdf document.

Offline

 

#6 2018-06-04 08:49:12 pm

bmose
Member
From:: Massachusetts
Registered: 2006-01-03
Posts: 281

Re: PDF Selection

akim wrote:

capture selected text in a pdf

Sorry, I misunderstood your request. I thought you meant that the pdf was the target in which to store the selected text. Now I realize that the pdf is the source of the selected text.

Offline

 

#7 2018-06-04 09:01:00 pm

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

Re: PDF Selection

akim wrote:

In an active PDF document



How you do it(or if you can) going to depend on which application you're using to view the PDF. Selections are an application thing, not a file thing.


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

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)