• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Reading a pdf text file
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Reading a pdf text file


  • Subject: Re: Reading a pdf text file
  • From: Gil Dawson <email@hidden>
  • Date: Sat, 8 Jan 2005 23:56:19 -0800

I've had some success reading a pdf file using Adobe Reader with UI scripting, and I thought I'd share what works so far (on 10.3.7 using Classic Script Editor 1.8.3):

property pdfFile : alias "Path:to:file.pdf"
tell application "Adobe Reader" to open pdfFile
tell application "BBEdit 4.5" to activate -- let's clear its window now
tell application "System Events"
keystroke "a" using command down -- Select All
keystroke (ASCII character 8) using command down -- Delete key
end tell
set prevPageAsText to "" -- part of a flaky attempt to detect eod that didn't work
repeat 15 times -- number of pages
tell application "System Events"
tell process "Adobe Reader"
set frontmost to true
click menu item "Select All" of menu "Edit" of menu bar 1 -- select this page as text
-- keystroke "a" using command down -- this also works
delay 1 -- let the select complete
click menu item "Copy" of menu "Edit" of menu bar 1
-- keystroke "c" using command down -- this also works
delay 1 -- let the clipboard fill
click menu item "Next page" of menu "Go To" of menu item "Go To" of menu "View" of menu bar 1
-- keystroke (ASCII character 29) -- Right Arrow = Next page -- also works
-- set thisPageAsText to (the clipboard as text) --> Error -619
end tell
end tell
tell application "BBEdit 4.5" to activate
tell application "System Events" to keystroke "v" using command down -- paste
tell application "System Events" to keystroke return -- return
tell application "System Events" to keystroke return -- return
end repeat


The delays are essential (well, at least one of them is.) If you leave them out, the clipboard doesn't get updated before we paste and the result is a mess.

Note that Select All for Adobe Reader means "all of the page you are looking at". To get the rest of the document you have to page down and Select All again, once per page. There are several flavors of paging down in Adobe reader, but the one that seems to work best for this exercise is Next Page, with the keyboard shortcut Right Arrow (ASCII character 29).

Figuring out when we've got to the last page has me stumped. The code above cops out: you have to know the number of pages a priori. I'd appreciate your suggestions on this.

I tried catching an error on the Next Page command, but even though the command is dimmed at the last page and you get a system beep if you type it, typing it does not seem to generate an error as far as AppleScript is concerned. Typing Right Arrow when you're on the last page simply doesn't do anything except beep.

Since the last Right Arrow doesn't do anything, the last page is simply copied again -- over and over -- if the repeat loop has no other limit. I thought maybe I'd try using this "feature" to determine the last page when we repeat the contents of the clipboard. (I know it's a flaky hack, but it would work in my case and I'm desperate!) However, this did not work. The critical command, "set thisPageAsText to (the clipboard as text)," threw an Error -619. I could not find this error number exactly on the Apple website, but, judging by its neighbors, it may have something to do with buffer sizes inside Apple Events.

Do you see anything that would make this better?

--Gil
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: Reading a pdf text file
      • From: yvan-koenig <email@hidden>
References: 
 >Reading a pdf text file (From: Gil Dawson <email@hidden>)
 >Re: Reading a pdf text file (From: Roger Howard <email@hidden>)
 >Re: Reading a pdf text file (From: Gil Dawson <email@hidden>)
 >Re: Reading a pdf text file (From: Emmanuel <email@hidden>)

  • Prev by Date: Re: Converting Hex to binary
  • Next by Date: Re: Reading a pdf text file
  • Previous by thread: Re: Reading a pdf text file
  • Next by thread: Re: Reading a pdf text file
  • Index(es):
    • Date
    • Thread