AppleScript that reads links within emails URL and download them?

Hi,

I need some help!
I think this can be done with a Script, but I have no idea how to do this.
We receive email notifications with URLs to download pdf files from a website, every time we have to "right click" and select "Download Linked File", we would like to automate this process.

The URL is always something like "http://website/folder/4568787_475757.pdf", of course, the name of the pdf file is always different, but the path is the same.

Is there a way for Mail to automatically download this pdf file to the downloads folder each time we receive a email notification?

Thanks for the help!

MacBook Pro 2.6 GHz, Mac OS X (10.6.3)

Posted on Jun 8, 2010 9:15 PM

Reply
15 replies

Jun 10, 2010 7:24 AM in response to dp13

I want to learn how to write Scripts!


Then might I suggest that you begin with the following links (in the specified order):

1. [AppleScript: The Language of Automation|http://www.macosxautomation.com/applescript>
2. [Beginner’s Tutorial: The First Step|http://www.macosxautomation.com/applescript/firsttutorial/index.html]
3. [Essential Sub-routines: Overview|http://www.macosxautomation.com/applescript/sbrt/index.html]
4. [AppleScript Language Guide|http://developer.apple.com/mac/library/documentation/AppleScript/Conceptu al/AppleScriptLangGuide/introduction/ASLR intro.html#//appleref/doc/uid/TP40000983-CH208-SW1]

Would you mind sharing what all this code means to help out a rookie?

The first two and last two lines of the script come from an example script you can find at /Library/Scripts/Mail Scripts/Rule Actions/Sample Rule Action Script.scpt.
For the repeat block, see [repeat with loopVariable (in list)|http://developer.apple.com/mac/library/documentation/AppleScript/Conceptu al/AppleScriptLangGuide/reference/ASLR control_statements.html#//appleref/doc/uid/TP40000983-CH6g-128481].
For the part of the script which gets the link from the current message, see [offset|http://developer.apple.com/mac/library/documentation/AppleScript/Concep tual/AppleScriptLangGuide/reference/ASLR cmds.html#//appleref/doc/uid/TP40000983-CH216-SW24].
For the part of the script which gets the name of the PDF file, see [text item delimiters|http://developer.apple.com/mac/library/documentation/AppleScript/Con ceptual/AppleScriptLangGuide/conceptual/ASLR fundamentals.html#//appleref/doc/uid/TP40000983-CH218-SW22].
For the part of the script which downloads the file, look for “path to” in the Standard Additions scripting dictionary and also for the “URL Access Scripting” dictionary. (Those dictionaries can both be opened from the File menu of the AppleScript Editor.)

Hope it can help. Gook luck.

Jun 9, 2010 1:53 PM in response to dp13

Assuming each one of the email notifications you are talking about contains just one HTTP link and that link is to a PDF file, then the following script should automatically download that PDF file to your downloads folder. In order to use the script, just add a rule to the Mail Preferences to perform a “Run AppleScript” action.

*using terms from application "Mail"*
*on perform mail action with messages theMessages*
*repeat with eachMessage in theMessages*
--Get the link from the current message:
*set theText to source of eachMessage*
*set P1 to offset of "http://" in theText* -- the beginning of the link (better yet: "http://website/folder/")
*set theText to text P1 through -1 of theText*
*set P2 to offset of ".pdf" in theText* -- the end of the link
*set theLink to text 1 through (P2 + 3) of theText*
--Get the name of the PDF file to download:
*set TID to AppleScript's text item delimiters*
*set AppleScript's text item delimiters to "/"*
*set theFileName to text item -1 of theLink*
*set AppleScript's text item delimiters to TID*
--Download the PDF file:
*set theDownloadedFile to ((path to downloads folder) as text) & theFileName*
*tell application "URL Access Scripting" to download theLink to theDownloadedFile*
*end repeat*
*end perform mail action with messages*
*end using terms from*

Please let me know if the script doesn't work as expected.

Message was edited by: Pierre L.

Jun 22, 2010 12:39 AM in response to Pierre L.

Thanks for all the links!

I'm struggling to change the downloads "path to" part.
I know there's a few places it can download to using the "path to" command.
Is there a way to download the file to a custom folder, lets say User/Downloads/here?
I tried using set the destination_file to ("/User/Downloads/here"), but no luck!

Any help would be greatly appreciated!

Jun 23, 2010 5:03 AM in response to dp13

In order to download a file for example to folder “Downloads/Here”, you might just replace
*set theDownloadedFile to ((path to downloads folder) as text) & theFileName*
with
*set theDownloadedFile to ((path to downloads folder) as text) & "here:" & theFileName*
or
*set theDownloadedFile to ((path to home folder) as text) & "downloads:here:" & theFileName*
or
*set theDownloadedFile to ((path to users folder) as text) & "\[user name\]:downloads:here:" & theFileName*

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.

AppleScript that reads links within emails URL and download them?

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