OS X - Drag URL / weblog to finder generates PDF of web page

565 views Asked by At

I often save web pages for future use, and am looking to try and streamline this process.

Say I have safari and finder open. Finder contains my current project. I browse the web researching and find something of interest, I then drag the web pages address from safari to finder - this will create a .webloc link.

This is not ideal for a couple of reasons ;

  1. .webloc file does not work with windows clients

  2. If website is removed or changed for whatever reason, my link essentially becomes invalid

What I ideally want to do is capture the website as a PDF. I am aware I can choose the appropriate menu options to export as PDF, or print / save as PDF etc... but it interrupts my workflow by having more steps involved, and also having to specify the folder to export to every time.

I was thinking an Automator / Apple Script may be able to help, something along the following pseudo code:

If finder has item dropped on it
    If the item is a URL or .Webloc
        Use Safari’s built in functionality to generate a PDF from the URL in the folder the item was dropped on
    end if
end if

However, I do find I struggle with AppleScript, so would anyone be able to tell me if this would even be possible before I dived in?

Thanks in advance.

1

There are 1 answers

0
pbell On

The best way to do what you want it so mix an Automator service with an AppleScript.

Automator service will contain only 1 action "run applescript", and it is set to receive text as a selection from Safari. AppleScript action is the following:

on run {input, parameters}

set myPath to "/Users/your_User/Desktop/Test_URL" -- set your default saving path
tell application "Safari"
activate
set myURL to URL of front document
set myURL to my CleanName(myURL)
tell application "System Events" to tell application process "Safari"
    click menu item 15 of menu 3 of menu bar 1 -- export as PDF (Safari 10.0.2 / El Capitain)
    delay 0.5
    keystroke myURL & ".PDF"
    delay 0.2
    keystroke "g" using {command down, shift down} -- go to dialog
    keystroke myPath -- path to saving folder
    delay 0.2
    keystroke return -- close go to dialog
    delay 0.2
    keystroke return -- close the save as dialog
end tell
end tell
return input
end run

on CleanName(S) --replace special characters not allowed for file name with _
set AppleScript's text item delimiters to {":", "/", ","}
set SL to text items of S
set AppleScript's text item delimiters to {"_"}
return SL as text
end CleanName   

This script runs simulate the use of Safari menu export as PDF. If you're using different version of Safari, the menu item may be different than mine. please adjust.

I assume that the PDF file should be based on URL value (+ PDF at the end !). To do so, I also add a subroutine to clean up the URL to convert it a something OK as a file name. I replace all special characters like / : , with char _.

Variable myPath contains the path to your default saving location: you must change 'your_user' with your user name and make sure the folder TEST_URL exists on your desktop. The script contains many comment to help you to adjust to your specific needs.