Automator workflow issue

I have a huge number of webpages saved that I'd liked to convert to PDFs. One solution is: Get Specified URLS > Download URLs as PDFs. I can add them all one by one this way to the first pane, but I have so many that that alone will be extremely time-consuming.


If I have multiple tabs open, is there any reasonably straight-forward way to get the urls from each tab into the workflow simultaneously before downloading them as PDFs?


Thanks in advance.

MacBook Pro, OS X El Capitan (10.11.2)

Posted on Dec 26, 2015 5:54 AM

Reply
15 replies

Aug 3, 2017 1:57 PM in response to cavara12

I also looked for automator script to save webpages to pdf, I have hundreds of bookmarks. I finally managed to work something out.

It will safe all open tabs in safari to PDF. I do like the clutter free pdfs rendered by the reader view in Safari. This script will save as pdf in reader view if it is available.


First create your own shortcut for the "export as PDF...", I used command-shift-&


Might not work perfectly for all websites but this is already something I can work it


# script to save open tabs in safari to pdf, in reader view if available
# 01/08/2017
# ------------------------------------------------------------------------
# add this script as service in automator
# before running create shorcut for "export as pdf...", I used commandshift&
# same shortcut command should not exist anywhere else, does not work otherwise
# save automator without spaces in the name, service is not shown in safari when using spaces

on run {}
  tell application "Safari"
  activate

  #prompt for location where you want to store the pdfs
  set destination to (choose folder with prompt "Where would you like to save the new file(s)?")

  #first loop over all open tabs to make sure webpage is loaded
  repeat with i from 1 to (count of tabs of window 1) by 1
  set thisTab to tab i of window 1
  set current tab of window 1 to thisTab
  delay 2
  end repeat

  #second loop over all open tabs to open in reader view
  repeat with i from 1 to (count of tabs of window 1) by 1
  set thisTab to tab i of window 1
  set current tab of window 1 to thisTab
  delay 1

  tell application "System Events"
  # press escape to close popups if any 
  # if no popup nothing happens
  tell application "System Events" to key code 53 #escape
  delay 1
  # open reader view
  # if reader view not available nothing happens
  keystroke "R" using {shift down, command down}
  end tell
  delay 1

  end repeat

  # third loop over open tabs to save to pdf
  repeat with i from 1 to (count of tabs of window 1) by 1
  set thisTab to tab i of window 1
  set current tab of window 1 to thisTab
  delay 1

  tell application "System Events"
  # call shortcut to open dialog "export as pdf..."
  keystroke "&" using {command down, shift down}
  delay 1
  # press enter to confirm save
  key code 36 #return
  delay 1
  # press enter in case file already exist = Cancel button
  # if file did not exist nothing happens
  key code 36 #return
  # press escape in case file already exist to close dialog
  # if file did not exist nothing happens
  delay 1
  key code 53 #escape
  end tell

  delay 1

  end repeat

  end tell

  beep
  display dialog "Html2Pdf done"
end run

Dec 26, 2015 2:01 PM in response to cavara12

Hello,


cavara12 wrote:


Would still be interested in an automator solution if there are others.


Solution:

Use the "Run AppleScript" action instead of the "Get Specified URLS" action.


In the "Run AppleScript" action:

Clear the default text int the action

Copy/paste this script


on run {input, parameters}
    set myList to {}
    tell application "Safari"
        repeat with thisWind in (get windows whose its current tab is not missing value)
            set myList to myList & URL of tabs of thisWind
        end repeat
    end tell
    return myList -- list of url from each tab in each window 
end run

Dec 26, 2015 3:13 PM in response to cavara12

cavara12 wrote:


Thanks, but it generates this error: 'Can’t get tab (window id 328).'? I entered exactly what you posted above.


Ok,


Use this script, it use a try block to prevent any error

-------------------

on run {input, parameters}

set myList to {}

tell application "Safari"

repeat with thisWind in (get windows)

try

set myList to myList & URL of tabs of thisWind

end try

end repeat

end tell

return myList-- list of url from each tab in each window

end run

------------

Or this script:

-------------

on run {input, parameters}

set myList to {}

tell application "Safari"

repeat with thisWind in (get windows)

try

repeat with thisTab in (get tabs of thisWind)

try

set end of myList to URL of thisTab

end try

end repeat

end try

end repeat

end tell

return myList-- list of url from each tab in each window

end run

-------------

Dec 26, 2015 5:04 PM in response to cavara12

cavara12 wrote:


Thanks. Once corrected, like the other, it's not returning errors or links. I tested Get Current Webpage from Safari and had no issue.


Automator can get a little funky on my machine too (El Cap). What works in Script Editor doesn't always return URLs in Automator. Sometimes adding a Show Results action "kick starts" it.


This one-liner seems to be working consistently here within a Run AppleScript action in Automator:


tell application "Safari" to tell window 1 to return tabs's URL


SG

Dec 26, 2015 6:06 PM in response to cavara12

1) Click on the results button to see results. See below.


2) I found automator has two run buttons!!! Which did you press? The run button in the step did nothing. I had to press the overall run button. Then it worked.



It's the run button in the little strip.

User uploaded file


Notice this has two run buttons?


Notice that if you want to see the results you have to click on results.


User uploaded file



User uploaded file






Did you run this in the script editor? I did & it worked.


Notice that I commented out the input ... -- { input, parameters }



User uploaded file

Dec 26, 2015 6:25 PM in response to rccharles

Hi rccharles,


Did you see my post above? I can confirm that Automator in El Cap can give unexpected results as cavara12 describes, when clicking the correct 'run' button.


Also, a simpler script may help to avoid syntax errors.


Given that cavara12 has a large number of saved webpages to convert, something like the solution described here may work better, using a shell script to loop through files in a directory.


SG

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Automator workflow issue

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.