Mac Automator/Applescript(?) to extract URL's from Mail messages - macos

I'm trying to get a text document list of any links in a bunch of email messages that reside in the latest Mac Mail.app (OS X 10.10.2 Yosemite), and am simply stumped.
I thought I'd be able to just...
Put a couple of Automator.app actions together in a Service/Workflow,
Select/highlight all the email messages within the Mail.app,
Select that Service or run that Workflow,
And have a text document with every URL/link that could be found within them all.
However, this didn't work.
I figured out how to do this with one email message at a time, but that's not what I want. attached is a screenshot of 3 workflows. The first one is the one that works with just one email message & highlighting all the text in it & running the Service. the other two just simply don't work.
I also notice that the the first shows up in the Service Menu with a single email open; once I highlight more than one email message, the option goes away from the Service menu.
Any tips or tricks?

I figured out how you could reach your goal, start with creating a new service within Automator (input: "No input", application: "Mail")
The first action is Get Selected Mail Messages (Get selected: messages)
The second action is Execute AppleScript with the following script:
on run {input, parameters}
set mailContentList to {}
tell application "Mail"
repeat with selectedMail in input
set end of mailContentList to content of selectedMail
end repeat
end tell
return mailContentList
end run
This script simply walks through the given messages, reads out the content and passes this list to the next action
The third action is Extract URLs from Text. This is listed as "Extract Data from Text" and one of the types of data is "URLs".
And the final action is New TextEdit Document
Save it with a nice name like Extract URLs from selected mails
After that the Service is available inside the Services menu inside the Mail app.
In my test I found a few internal URLs without http:// from links to anchors, so maybe you want to delete all URLs that do not start with http. You can do so by using another action before creating the new TextEdit document:
Filter Paragraphs with options "Paragraphs that start with http" (don't know how these parameters are called in English Automator, sorry)
Feel good, Michael / Hamburg

Related

Apple automator & applescript : saving pictures from a web page if do not exist locally

I am currently trying to automate a task as an app but have difficulties with the step "save URLs" and need a custom applescript to replace it.
To give you the context of my project:
I bought a Toshiba FlashAir SD WIFI card for wifi tethered picture shooting.
I want to download all the file in real time from my camera with the SD WIFI card(installed in my camera) to my Mac Computer.
Toshiba FlashAir runs its own network you connect to, and you can browse the SD content through a web-browser (no ftp,no webdav..., so only http connection).
I prepared an html/Jquery page uploaded to the SD, with the FlashAir API so that when you hit the page http : // flashair you get a page with all the image links refreshed every second. It's nearly real-time and display new shot images in the browser.
I want to download those pictures on my computer so that iPhoto or any other photo app can "watch" the directory like in tethered mode and process on the fly if I need to.
On my computer side, with AUTOMATOR :
I "get the specific URL" > http : // flashair ( to reach the SD card micro server).
Then "get all the image URLs" from this specific URL and related ones
Then Download ONLY the pictures that I don't already have on my computer (this is where I lack applescript knowledge)
Then Loop for 240minutes... (to observe the remote page and download new files ONLY again and again.)
So everything work perfect but when I launch the Automation workflow, The same pictures are saved again and again with the filename suffix -01, -02, ... . It means I have the same amount of duplicate pictures as the loop instance.
The automator process "Download URLs" does not allow me to precise if I want to download only new or modified files from remote to local.
Is there someone who can help me with this "applescript" step, so that I can replace the automator "Download URLs" by a specific Applescript step that looks if the files already exists then if not download them every loop instance ?
Thanks a lot for your precious answer, I am stuck with that, and everything is flawless, but that duplicated files issue.
Damien
Here is the code you are looking for:
on run {input, parameters}
set dLocation to POSIX path of (path to downloads folder) & "test/"
set fileList to {}
set AppleScript's text item delimiters to {"/"}
repeat with i from 1 to (count of input)
# With the delimeter to '/' last item is the filename!
set urlStr to (get item i of input) as text
set urlFile to last text item of urlStr
set savedFile to (dLocation & urlFile) as POSIX file
log "Saved file is: " & savedFile
try
savedFile as alias
log "File exists: " & savedFile
on error mMsg
set end of fileList to urlStr
log "Adding URL: " & urlStr
end try
# delay 5
end repeat
return fileList
end run
All the above does is to grab the filename at the end of the link. It checks if the file exists and if not adds the link to the list of links to download. It then passes the list of links on.
After the Applescript action add a Download URLs action and you should be done.
EDIT:
So the Workflow will be like this:
Define Site URL
Action: Get URL Links from Webpages
Action: Run applescript (code above)
Action: Download URLs
Action: Pause (optional)
Action: Loop
Note that the Loop Action limits you to 1000 minutes. So you have two choices, 1) add more loop actions, or 2) launch the workflow from a script with an infinite loop...
The other way is to write the whole thing as a script and have it run continuously.
EDIT2:
Oops just reread your question. It was for 240minutes, so a single loop function will work just fine.

Getting Mac Hazel to e-mail .lnk, doc, and .xls files to my evernote account

i'm trying to automate and simplify my life. I'm new to hazel and apple scripts..
I have a lot of documents and i pull a lot of things of the web. i simply drag the link to my desktop where it makes a .lnk file.
i want to create a hazel + apple script that will look for these .lnk files and send an email with the link file attached to my evernote email address.
Any suggestions would be appreciated.
In Hazel Create a new rule.
If all ..
1, Extension - is - ink
Do the following:
2, Run AppleScript - embedded script.
Click the info icon to edit the script. Paste this script into it. With the correct email addresses, subject, content set.
set theAttachment1 to (POSIX path of theFile)
set subject_ to "subject"
set the_content to "the_content"
tell application "Mail"
set newMessage to make new outgoing message with properties {subject:subject_, content:the_content & return & return}
tell newMessage
set visible to false
set sender to "my1#mac.com"
make new to recipient at end of to recipients with properties {address:"their#mac.com"}
make new attachment with properties {file name:theAttachment1} at after the last paragraph
(* change save to send to send*)
send --<<<<---------------- change save to send to send or send to save to save to drafts
(* change save to send to send*)
end tell
end tell
3, Add a second action to move the file to a folder. If not you may keep getting the same files sent over and over again.

Can an applescript "tell" call execute without visibly launching the application?

I have a Mail rule set up to launch the following applescript:
using terms from application "Mail"
on perform mail action with messages theMessages for rule theRule
tell application "Mail"
-- do stuff, including...
CheckAddressBook(theName, theAddress)
end tell
end perform mail action with messages
end using terms from
on CheckAddressBook(theName, theAddress)
tell application "Address Book"
-- do stuff
end tell
end CheckAddressBook
Whenever this mail rule executes, it launches address book. Its not activated, but it suddenly shows up on my desktop. My question is, can tell blocks be instructed to launch the application silently, and quit when complete?
AppleScript can't control an application without it running. That's just the way it works. There are other methods you might use to access the Address Book database without launching the application, but if you're using AppleScript to get data from your Address Book database the application has to launch. My recommendation would be to simply add a quit command as suggested by Fábio.
To read the Address Book Database without launching "Address Book.app" I´d suggest to have a look at the command line tool "contacts" available for free here. You would then run it from Applescript like do shell script "/usr/bin/contacts Peter" and handle the values returned.

Automatically adding attachments to all outgoing mail messages

A two-part newbie question, guys... I've only just discovered AppleScript and I'm hoping to automagically attach a 'pdf' file to every outgoing (sent) "Mail.app" message.
(Q1) I've got it working successfully in tests running direct from Script Editor - with the file located in the Documents folder - and I'm using:
set pdfFile_Path to ((path to documents folder as text) & "paginatedPDF.pdf")
but the filepath bit confuses me... How would I change this line if I wanted to store my file inside a folder called "PDF's to send" on my desktop?
(Q2) How can I attach the script as a Mail.app rule (the rule options only seem to offer incoming mail message conditions, not outgoing ones)?
Any help/advice much appreciated. Thanks :-)
Have you tried dragging and dropping the PDF straight into your Signature?

Automator / AppleScript to process incoming emails in Mac Mail

I'm designing an app that allows users to email me crash reports if my app ever crashes. I'd like to leave Mac Mail running on a computer and when an email comes through, an automator script / AppleScript runs to process the contents of the body of the email.
I've got the entire parsing/processing done in a python script, except I have to manually copy the contents of the email into a file and then run my parser on that file.
What's the best way to set this up so I can the contents of the email be pushed into my parsing script?
Many thanks!
Probably the simplest approach is to define a Mail.app Rule. You can set up filtering conditions to specify the set of incoming email to apply the rule to and among the rule actions you can specify is one to run an AppleScript on incoming messages. Rules are managed with Mail.app Preferences -> Rules. Apple supplies examples of Rule Action scripts with Mac OS X. Look in /Library/Scripts/Mail Scripts/Rule Actions or search the web.
Here's a script that extracts from email into a file using a mail rule: MacScripter / Mail rule script for message export. Might be good for sample code for what you're doing.
Use the Dictionary in Applescript Editor to see the properties of mail and you'll quickly be able to see the properties of any mail message. Here's a quick and dirty example of getting the content of a mail message.
tell application "Mail"
set the_messages to selection
repeat with this_message in the_messages
set mytext to content of this_message
end repeat
end tell
Modify the script linked to above that copies output to a temporary file and then pass that file to your Python script to act on.

Resources