2

I would like to make an AppleScript that saves all of the open tabs in the front window in Google Chrome to a PDF.

Here is my working MWE so far:

tell application "Google Chrome"
    set allTabs to tabs in front window
    repeat with myTab in allTabs
        tell myTab to print
    end repeat
end tell

Of course, this simply opens the print window repeatedly for each open tab.

Ideally, I could save each one to a distinct PDF, something like this (using some made-up commands here):

tell application "Google Chrome"
    set myFolder to "/Users/nnn/Desktop/PDF/"
    set myCount to 0
    repeat with myTab in allTabs
        set myCount to myCount + 1
        set fileName to "pdf" & myCount & ".pdf"
        set outputPath to myFolder & fileName
        export myTab to outputPath as PDF  -- obviously, this does not work.
    end repeat
end tell

How would I get this to work?

5
  • 1
    Can you use a url to pdf api?
    – JBis
    Dec 9, 2018 at 14:41
  • @JBis, I have no problem with that, but the web pages in question require a login to Amazon, so an application that just does a batch download of URLs will probably not work. Dec 9, 2018 at 14:49
  • 1
    Everything im seeing online is using the contextual menus and clicks to do things. I don't like/know how to do that stuff. If I find a way to do without that. Then I'll try to help. Good luck!
    – JBis
    Dec 9, 2018 at 15:06
  • Probably not very secure, but maybe if we used JS to get the source HTML and then fed that through an API.
    – JBis
    Dec 9, 2018 at 15:08
  • Obviously, Safari and Firefox on are on the table, too, or anything that can figure out that I am logged into Amazon. Dec 9, 2018 at 15:30

1 Answer 1

1

I found a work-around, using UI scripting, that does what I need:

tell application "Google Chrome"
    set myWindow to front window
    set myTabs to tabs in myWindow


    set outputFolder to "/Users/nnn/Desktop/PDF/"
    set myCount to 0

    activate

    repeat with myTab in myTabs
        set myCount to myCount + 1
        set fileName to "pdf" & myCount & ".pdf"
        set outputPath to outputFolder & fileName

        --N.B.: the following opens the system print window, not Google Chrome’s
        tell myTab to print

        tell application "System Events"
            tell process "Google Chrome"
                repeat until window "Print" exists
                    delay 0.02
                end repeat

                set printWindow to window "Print"

                tell printWindow
                    set myButton to menu button "PDF"
                    click myButton

                    repeat until exists menu 1 of myButton
                        delay 0.02
                    end repeat

                    set myMenu to menu 1 of myButton
                    set myMenuItem to menu item "Save as PDF" of myMenu
                    click myMenuItem

                    repeat until exists sheet 1
                        delay 0.02
                    end repeat

                    set saveSheet to sheet 1
                    tell saveSheet
                        set value of first text field to fileName
                        keystroke "g" using {command down, shift down}

                        repeat until exists sheet 1 of saveSheet
                            delay 0.02
                        end repeat

                        set goDialogue to sheet 1 of saveSheet

                        tell goDialogue
                            set value of first combo box to outputFolder
                            click button "Go"
                        end tell

                        click button "Save"
                    end tell
                end tell

                repeat while printWindow exists
                    delay 0.02
                end repeat
            end tell
        end tell
    end repeat
end tell
1
  • Will this save pdfs for EVERY tab? Or just the ones with PDFs currently in them? Sep 20, 2019 at 22:54

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .