To all automator users:
Here is an action that will get the text from the current page of an opened 
Preview document.  The Dropbox link is 
https://www.dropbox.com/s/bfjuy89r3ljqd1x/Get%20Current%20PDF%20Page.action.zip?dl=0
 
<https://www.dropbox.com/s/bfjuy89r3ljqd1x/Get%20Current%20PDF%20Page.action.zip?dl=0>.
To install, just open.
I’ve tested it for the last few days and it seems to work good.  Note however 
that vo doesn’t work correctly with the scroll area of Preview.  That is, even 
though vo reads the next page, the current page stays the same.  To actually 
change the page, you have to stop interacting with the pdf content area and use 
page up or down.

I’m also providing the Xcode applescript project for anyone familiar with Xcode 
and who might be interested in examining it.  It is: 
https://www.dropbox.com/s/3aa95rismld82me/Get%20Current%20PDF%20Page.zip?dl=0 
<https://www.dropbox.com/s/3aa95rismld82me/Get%20Current%20PDF%20Page.zip?dl=0>.

Here is the code.  Note that it won’t run in the script editor.

--  Get_Current_PDF_Page.applescript
--  Get Current PDF Page

--  Created by Barry Hadder on 4/3/15.
--  Copyright (c) 2015 Barry Hadder. All rights reserved.

property NSURL: class "NSURL"
property PDFDocument: class "PDFDocument"
property PDFPage: class "PDFPage"
property NSCharacterSet: class "NSCharacterSet"
property NSString: class "NSString"

script Get_Current_PDF_Page
        property parent : class "AMBundleAction"

        on runWithInput_fromAction_error_(input, anAction, errorRef)
                tell application "Preview"
set theWindow to front window
set theName to NSString's stringWithString_(name of theWindow)
set thePath to the path of the document of theWindow

end tell

set theUrl to NSURL's fileURLWithPath_(thePath)
set doc to PDFDocument's alloc()'s initWithURL_(theUrl)
set theIndex to 0
if(pageCount() of doc > 1) then

     --theName is a string of the form document (Page # of #) or (1 page)
--In the case of the first option, I parse the page number out of the string to 
determine the index of the current page.

set a1 to theName's componentsSeparatedByCharactersInSet_(NSCharacterSet's 
characterSetWithCharactersInString_("()"))
set a2 to componentsSeparatedByString_(" ") of item 2 of a1
set theIndex to  (integerValue() of item 2 of a2)-1
end if

set page to doc's pageAtIndex_(theIndex)
set theOutput to valueForKey_("theButtons") of parameters() of me
if(theoutput as integer is 0) then
return page's |string|()
    end if

return page's attributedString()
        end runWithInput_fromAction_error_
        
end script

Barry Hadder
bhad...@gmail.com



-- 
You received this message because you are subscribed to the Google Groups 
"MacVisionaries" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to macvisionaries+unsubscr...@googlegroups.com.
To post to this group, send email to macvisionaries@googlegroups.com.
Visit this group at http://groups.google.com/group/macvisionaries.
For more options, visit https://groups.google.com/d/optout.

Reply via email to