Download a PDF from PDF viewer within a browser

Hi there,

I’ve written an AppleScript (with JavaScript subroutines) that controls a web form in Safari to search for an online record and then click on a link which reveals a PDF. See this link: https://tcrecordsonline.com/#pdfviewer?ID=D216289192

The PDF is presented through some kind of API or Flash… or something. Manually if you move the cursor down to the bottom part of the screen, some options pop up where you can download the PDF. However, I can’t figure out how to do this using code.

Then I was thinking that the PDF should probably be already downloaded to a temporary folder somewhere. I’ve searched through the /private/var/ folders but to no avail. Does anyone know how I can download that PDF?

David

OK I’ve sorted that out myself so here is how I did it. The PDF viewer is showing the PDF in the browser. The actual PDF is stored on the same server as the website. I found this out by looking at the page source:


tell application "Safari" to set PageSource to do JavaScript "document.documentElement.innerHTML" in document 1

I used Applescript’s text item delimiters to parse the “PDF_URL” from the page source. I got it to look for the HTML that preceded the URL and then separated it from the code straight after that.

Then it was easy to download the PDF from the URL via a shell script: curl.


--DEFINE DOWNLOAD FOLDER
set MyDownloadPlace to quoted form of (POSIX path of (path to downloads folder)) & PDF_Name

-- DOWNLOAD USING Curl
do shell script "curl -L " & "\"" & PDF_URL & "\" -o " & MyDownloadPlace