Applescript: guarda todas las pestañas abiertas en Chrome a PDF

3

Me gustaría crear un AppleScript que guarde todas las pestañas abiertas en la ventana frontal de Google Chrome en un PDF.

Aquí está mi MWE trabajando hasta ahora:

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

Por supuesto, esto simplemente abre la ventana de impresión repetidamente para cada pestaña abierta.

Idealmente, podría guardar cada uno en un PDF distinto, algo como esto (usando algunos comandos inventados aquí):

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

¿Cómo lograría que esto funcione?

    
pregunta AthanasiusOfAlex 09.12.2018 - 15:21

2 respuestas

0

Encontré una solución alternativa, utilizando scripts de UI, que hace lo que necesito:

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
    
respondido por el AthanasiusOfAlex 10.12.2018 - 14:24
1

Aquí hay una solución alternativa que no usa ningún comando de UI. Este código siguiente escribirá la URL de cada pestaña abierta en Google Chrome, en una línea separada en un documento .txt que se creará. El comando do shell script convertirá ese archivo .txt en un archivo .pdf. Una vez creado el archivo .pdf, se eliminará el .txt original.

El siguiente código AppleScript me funciona con la última versión de MacOS Mojave

global allTabs, theFile, baseFileName, inputNameExtension

set saveToLocation to quoted form of POSIX path of ((path to desktop as text) & "PDF:")
set baseFileName to "Google_Chrome_URLs"
set inputNameExtension to ".txt"
set outputNameExtension to ".pdf"

set inputName to saveToLocation & baseFileName & inputNameExtension
set outputName to saveToLocation & baseFileName & outputNameExtension

tell application "Google Chrome" to set allTabs to URL of tabs in front window as list

set saveTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to linefeed
set allTabs to allTabs as string
set AppleScript's text item delimiters to saveTID

writeToAFile()

do shell script "cupsfilter " & inputName & ">" & outputName

tell application "System Events" to delete file theFile

on writeToAFile()
    set theFile to POSIX path of (path to desktop as text) & "PDF/" & baseFileName & inputNameExtension
    set theText to allTabs
    try
        set writeToFile to open for access theFile with write permission
        write theText & linefeed to writeToFile as text starting at eof
        close access theFile
    on error errMsg number errNum
        close access theFile
        set writeToFile to open for access theFile with write permission
        write theText & linefeed to writeToFile starting at eof
        close access theFile
    end try
end writeToAFile
    
respondido por el wch1zpink 21.12.2018 - 23:49

Lea otras preguntas en las etiquetas