Extract URL from Mail message

Hello Applescript-Meisters,
I have the following problem:
Every week we receive an email which contains a secure URL (https://) with always different username and password to download a ".tar" file.
Since I'm new to Applescript I tried to extract the paragraph of the email message with the line which contains the URL, but doesn't seem to work:
tell application "Mail"
set theMessages to message 1 of mailbox "EXAMPLE" of account "AccountTest" whose subject begins with "Data request"
set theContent to content of theMessages
set someData to paragraphs of theContent
repeat with someData in theContent
if someData begins with "https://" then
set theURL to paragraph of someData
else
set theURL to paragraph 18 of theContent -- this works but this line can change weekly
end if
end repeat
end tell
Any help or suggestions would be greatly appreciated.
Cheers,
gilles

The most obvious problem is how you're trying to iterate through the paragraphs.
You start off with:
set theContent to content of theMessages
which is fine - theContent now contains the text of the email.
You then extract the paragraphs of that text:
set someData to paragraphs of theContent
But you then reset 'someData' to be the iterator in a loop:
repeat with someData in theContent
Additionally, you're iterating through theContent, which is a single block of text.
I think what you mean to do is iterate through the paragraphs of theContent, like:
repeat with someData in (get paragraphs of theContent)
Once you have this, you know someData is the paragraph you're currently looking at so you can:
set theURL to someData
Finally, you do not want an 'else ... set theURL' statement because that will reset theURL every time you find a line that doesn't begin with "https://" (e.g. if paragraph 10 starts with https:// you set theURL to that paragraph, but as soon as you move onto line 11 you reset theURL because the paragraph doesn't begin with https://
To address this you should just iterate through the paragraphs and only set theURL to paragraph 18 if you didn't find any matching lines.
Putting it all together:
set theURL to "" -- default value for theURL
theContent to content of theMessage
repeat with someData in (get paragraphs of theContent)
 if someData begins with "http://" then
  set theURL to someData
 end if
end repeat
if theURL = "" then
 try
  set theURL to paragraph 18 of theContent
 end try
end if
Note that I've included the 'paragraph 18' line in a try block. That's so that the script doesn't fail if there are less than 18 paragraphs in the message (you can't get paragraph 18 of a 17-paragraph document).

Similar Messages

  • Get URL from Mail message to Safari

    I want to be able to email myself at home from work with a URL in the body of the message. I want a Mail rule and Automator (or applescript if necessary) to take that URL from the message body and send it to Safari which will go to the URL. The URL can be either a web page or downloadable file.
    Quad G5   Mac OS X (10.4.7)   4.5GB RAM

    Shoot! I wrote this huge reply and then quit Safari by accident. I'm too tired to type it all over again (I should install a keylogger).
    Anyway, check out the screen shot of my workflow. the AppleScript section is where it xfers the URL from a text editor (BBEdit) to Safari. It's pretty dirty but it works.
    http://www.jameskocsis.com/urlworkflow.jpg
    post antother reply if you need more data on this process.
    Good luck.
    -James

  • Send URL from Mail to Safari

    You know how you can easily send a URL from Safari to Mail (Shift-Command-I)? Why can't you do that in reverse?
    I want to be able to email myself at home from work with a URL in the body of the message. I want a Mail rule and Automator (or applescript if necessary) to take that URL from the message body and send it to Safari which will go to the URL. The URL can be either a web page or downloadable file.
    Quad G5   Mac OS X (10.4.4)   4.5GB RAM

    Zach Barocas,
    With the webpage open, tap on the "+" symbol at the bottom and choose " "Mail Link to this Page".
    iPhone User's Guide, Page 56:
    http://manuals.info.apple.com/enUS/iPhone_UserGuide.pdf
    Hope this helps,
    Raleigh

  • Stripping Attachments from Mail messages.

    Does anybody know of a utility for stripping out attachments from Mail messages so as to save storage space?

    Hey thanks for the suggestions.
    Firstly, embedded attachments can be dragged out of the email messages on Windows as they can on OS X.
    Getting our clients to work like this could be difficult as they all have 1024px screens and from what we've seen they always have their windows maximised (in fact most Windows users seem to do this). But we'll suggest it.
    Secondly, when you next plan to make an attachment be sure to use the menu or toolbar option for selecting an attachment. When the Choose File dialog appears you will see a checkbox in the lower left labeled "Use Windows friendly attachments." Be sure that option is checked.
    We do this but it still doesn't seem to work - maybe their MailMarshal software is causing this?
    Thirdly, be sure any attachment you send has the proper extension. For example, PDF files should have ".pdf", and JPEGs should have ".jpg" or ".jpeg" in order to be consistent with what Windows applications expect.
    Yes, we do this.
    Fourthly, often certain documents on OS X do not have an extension or the extension is slightly different, for example, Word documents may be ".docx" on the Mac, but need to be ".doc" in order for Word for Windows to open the document by double-clicking.
    Have come across this also.
    Fifthly, ZIP files should not normally be confused with junk. Your IT department may need to consider making some changes to their email filtering software.
    We don't have an IT department, and based on recent conversations with their IT guys, they're definitely not interested in taking suggestions on email security from Mac users!
    Will see how we go with getting our clients to work differently but I'd personally prefer to sort it our end - from their point of view it's a Mac issue and from what I can see I think they might be right.

  • Applescript get text from mail message

    Hi,
    I've got a nested script that ideally would work with a multiple selection of emails. Most of the script does but I've recently found the need to add the sender's delivery address to the outgoing message. I've found a way to insert the clipboard but that leaves me only being able to send out one message at a time and I have to copy the address for each message to the clipboard first. I made a script to do that bit once I'd selected the text but this only works from Applescript Editor and not Quicksilver which is what I normally use.
    So, what I'm hoping is someone would be able to help me with the a, select the right bit of text between Delivery address: and Instructions to merchant: of each message and have that work with Quicksilver and b, tell me why the following script only partially works from Quicksilver, i.e. the top bit, copy to clipboard, doesn't work.
    Thanks.
    tell application "System Events"
      tell application "Mail" to activate
      key code 8 using command down
    end tell
    tell application "Mail"
      set thesenders to {}
      set thesenderstext to ""
      set myaddress to the clipboard
      set theMessages to the selection
      repeat with eachMessage in the theMessages
      repeat with i from 1 to the number of items in eachMessage
      set theAccount to "[email protected]"
      set theSender to (extract address from (the reply to of (item i of eachMessage)))
      if thesenders does not contain theSender then
      set thesenders to {theSender}
      end if
      set replyName to (extract name from (the sender of (item i of eachMessage)))
      set titleCaseString to (do shell script "/bin/echo" & space & quoted form of replyName & space & "| /usr/bin/perl -p -e 's/(\\w+)/\\u\\L$1/g;'") as Unicode text
      set firstName to word 1 of titleCaseString
      end repeat
      set AppleScript's text item delimiters to ", "
      set thesenderstext to thesenders as rich text
      set AppleScript's text item delimiters to ""
      set newMessage to make new outgoing message
      set theSubject to "instructions"
      set theContent to "Dear " & firstName & ",
    Thanks so much for your order.
    You will receive a despatch notice by email when your order is sent.
    Your  Order is being sent to:
    " & myaddress & "
    Kind Regards
    Jim
      set newMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
      set theAttachment1 to POSIX file "/Users/Jim/qrcode.13177607.png"
      set theAttachment2 to POSIX file "/Users/Jim/qrcode.13177658.png"
      set theAttachment3 to POSIX file "/Users/Jim/qrcode.13177665.png"
      tell newMessage
      set visible to true
      make new to recipient with properties {address:thesenderstext}
      --make new bcc recipient at end with properties {address:theBccRecipient}
      make new attachment with properties {file name:theAttachment1} at after the last paragraph
      make new attachment with properties {file name:theAttachment2} at after the last paragraph
      make new attachment with properties {file name:theAttachment3} at after the last paragraph
      -- send
      end tell
      tell application "Mail"
      set a to selection
      repeat with s in a
      set flag index of s to -1 as integer
      end repeat
      set theSelection to the selection
      repeat with theMessage in the theSelection
      move theMessage to mailbox "orders" -- of account theAccount
      activate
      end repeat
      end tell
      end repeat
      set read status of every message of mailbox "orders" to true
    end tell

    Shoot! I wrote this huge reply and then quit Safari by accident. I'm too tired to type it all over again (I should install a keylogger).
    Anyway, check out the screen shot of my workflow. the AppleScript section is where it xfers the URL from a text editor (BBEdit) to Safari. It's pretty dirty but it works.
    http://www.jameskocsis.com/urlworkflow.jpg
    post antother reply if you need more data on this process.
    Good luck.
    -James

  • Extract URL from a href string

    Greetings,
    I am trying to solve a problem (in a specific way) that will lead to the solution for another problem (which is similar).
    I am trying to extract a URL from an HTML <a> tag like this:
    <a href="www.someurl.com">Go to www.someurl.com</a>Can anyone help with the best solution? I've tried it using String.split() and StringTokenizer (which might work for this example), but, for my real problem, they seem quite inadequate. I'm guessing the solution involves some regex but I don't know where to look (and am quite unfamiliar with regex to know how).
    Thank you in advance.

    Actually, I am trying to break down many sections of the URL.
    The URL I am trying to breakdown has a lot of data that I need.
    I guess a better example would be:
    <a href="www.someurl.com" id="url-id-43612">Link Text - data here</a>In my program, I need to grab three things: the href URL, the id, and "Link Text - data here."
    Here is an SSCCE of how I am currently dealing with this:
    import java.util.StringTokenizer;
    * To change this template, choose Tools | Templates
    * and open the template in the editor.
    * @author Ryan
    public class htmlparser {
        public static void main(String[] args) {
            String str = "<a href=\"www.someurl.com\" id=\"url-id-43612\">Link Text - data here</a>";
            str = str.replaceAll("<a href=\"", "\n")
                .replaceAll("\" id=\"url-id-", "\n")
                .replaceAll("\">", "\n")
                .replaceAll("</a>", "\n");
            StringTokenizer tokenizer = new StringTokenizer(str, "\n");
            String url = tokenizer.nextToken();
            String id = tokenizer.nextToken();
            String text = tokenizer.nextToken();
            System.out.println(url);
            System.out.println(id);
            System.out.println(text);
    }But I am looking for a more elegant solution to the problem, is there one?

  • Is it STILL impossible to remove individual attachments from Mail messages?

    Could someone tell me if I just cannot find it anywhere, or does Apple's Mail for Mavericks 10.9.1 still not permit the removal of individual attachments from email messages?

    The only way is to restore the iPod to factory defaults/new iPod.  I know that is not a desirable solution..

  • Extract photos from text messages

    Greetings,
    Is there an easy way or an app that can extract all the photo's and video that I have received in text messages on my iphone.  I recently checked and I have 1.8 Gig usage in Messages.  I could go through one message at a time but that could take a while!
    Thanks,
    Brian

    Save Files
    SMS
    1. Go to Text Messages view, threader veiw that shows the pic preview when its not opened.
    2. Press and hold on Pic
    3. Select Save from the list\
    4. Now the pic should be saved in SD/Messages folder
    EMAIL
    1. Open Email
    2. At top there should be the number and the words "View Attachments" - Click on View Attachments
    3. Now you should have a list of attachments with a arrow next to it if not downloaded.
    4. Click file you want to download and you should get a animation where the arrow was that is indicating its being downloaded.
    5. Once the animation stops this means file has been downloaded to a temp file to be viewed.
    6. Now Press and Hold on file text and a window should open with three options - VIEW - SAVE- and SHARE
    7. Select Save from list
    8. Once complete it will have a little message at bottom of page that says "File was downloaded to SD Card"
    9. Now file will be saved in SD / Messages folder on SD Card.
    To Save As Wallpaper:
    1. Open Gallery
    2. Click on photo
    3. Click Left Menu button
    4. Select "More"
    5. Select "Set As"
    6. Wallpaper
    7. Click Save after you resize image to preferred size

  • Script to extract info from Mail subject line?

    I'd like to set up a rule that copies all subject lines containing text I specify to a text file. For example... all subject lines containing "has subscribed" would be copied to subscribers.txt. Is this possible?

    <pre style="
    font-family: Monaco, 'Courier New', Courier, monospace;
    font-size: 10px;
    font-weight: normal;
    margin: 0px;
    padding: 5px;
    border: 1px solid #000000;
    width: 720px;
    color: #000000;
    background-color: #E6E6EE;
    overflow: auto;"
    title="this text can be pasted into the AppleScript Editor">
    using terms from application "Mail"
    on perform mail action with messages theMessages
    tell application "Finder" to set ptd to (path to desktop folder) as string
    tell application "Mail"
    repeat with theMessage in theMessages
    if subject of theMessage contains "has subscribed" then
    set theText to (subject of theMessage & return)
    set theFile to ptd & "subscribers.txt"
    set theFileID to open for access file theFile with write permission
    write theText to theFileID starting at eof
    close access theFileID
    end if
    end repeat
    end tell
    end perform mail action with messages
    end using terms from
    </pre>

  • Extracting URL From Application Object

    If I type http://mymachine.com/myapp?a=aaa&b=bbb in my browser (to access application "myapp") how do I extract just the http://mymachine.com/myapp part from the application object? I would have figured that the code below would allow me to construct that string but these methods instead return the stuff in comments...
    URL theURL = application.getResource("/");
    theURL.getProtocol(); // jndi
    theURL.getHost(); // null
    theURL.getPort(); // -1
    theURL.getFile(); // org.apache.naming.resources.FileDirContext@!8bf135
    URL theURL = application.getResource("/myapp");
    theURL.getProtocol(); // null pointer exception
    theURL.getHost(); // null pointer exception
    theURL.getPort(); // null pointer exception
    theURL.getFile(); // null pointer exception

    tech note... HttpUtils is depricated for newer Servlet APIs (2.3+). Instead, you would use the same methods in the HttpServletRequest:
    URL theURL = new URL(request.getRequestURL().toString());

  • Extract URL from HTML text

    Suppose you have the following String that is body text with HTML.
    String bodyText = " My name is Blake. I live in New York City. See my image here: <img href="http://www.blake.com/blake.jpg"/> isn't my picture awesome? Tata for now!"
    I want to extract the URL that contains the location of the image in this bodyText. The ideal would be to create a function called public String extractor(String bodyText) to be used
    String imageURL = extractor(bodyText);
    //imageURL should be "http://www.blake.com/blake.jpg"
    My first thoughts are using reg exp, yet the place i would find to use that would using the .replace in String class. I am by no means an expert on reg exp so I haven't taken too much time to try to figure it out with reg exp. I obviously could do a linear search of the bodyText and do a ton of if statements, but thats just poor coding. I want see if anyone came across this or has insight to this problem.
    Thanks for all the help,
    Blake

    How would the regexp change if there were multiple img tags within the String.I don't rightly know, I'm just a raw beginner in regexes.
    Would this regexp return all the img URLs found in the String.No, as it stands it would return only the last URL. But this will:String bodyText = " My name is Blake. " +
          "I live in New York City. See my image here: " +
          "<img href=\"http://www.blake.com/blake.jpg\"/>" +
          " isn't my picture awesome? Here's another: " +
          "<img href='http://www.blake.com/Vandelay.jpg'/>" +
          " Tata for now!";
    String regex = "(?<=<img\\shref=[\"'])http://.*?(?=[\"']/?>)";
    Pattern pattern = Pattern.compile (regex);
    Matcher matcher = pattern.matcher (bodyText);
    while (matcher.find ()) {
       System.out.println (matcher.group ());
    }Note the enhancement that takes into account that both single and double quotes are legal in HTML. But unlike the earlier example, this does not tolerate more than one space between <img and href=, I couldn't find a way to achieve that.
    Visit this thread later, there are some real regex experts around who may post a better solution.
    db

  • How do I copy a URL from a message without opening it in Safari

    Someone sent me a URL in a sms.  I want to copy it an put it in an appointment.  Everytime I touch the link it opens in Safari.  I just want to copy it.  Can I do that?  Thanks.

    Tap and hold on the link, and a pop-up should appear. Tap Copy. If that doesn't work, tap the whole message and choose copy, then paste it wherever you want and delete the part that you don't need.

  • Can't extract ringtone from text message

    My son sent me a ring tone via text message. The last time he did this I was able to touch the ring tone on my screen and save it to my phone. This time it won't let me. The only difference I can see if that this ring tone was made using Quicktime. Is there something I should be doing to save it to my phone? My computer was hit with a virus so I can't even send it to my email and drag it over.
    Any help would be appreciated.

    When I got the iPhone in June I set it up on my laptop. The ringtones were sent to me by my sons. I thought I had put them on my phone my saving them directly on my iPhone from a text, but everyone is telling me I didn't. So I am assuming I synched them over. In July my laptop was hit by some virus. The laptop had to by wiped clean. I got my laptop back last week. I had to download a new iTunes to my the computer. I did that. I transferred my music purchases over, my app purchases and I synched my phone, but the existing ringtones on my phone did not synch over. When I went to click the synch button I got that bizarre message. Fearfull I would loose those ringtones I did not sync. I have a ringtone that my son emailed me in the library of my iTunes, but I can't get it to my phone because if I sync it I am afraid I will loose it.

  • Extract URLs from webpage

    Hi guys,
    I'd like to know if there is a possibility to find hyperlinks on a webpage and write their targets to a text file using applescript.
    I'll take youtube as an example, because nearly everyone is familiar with it:
    When I click on a username, I will be redirected to the Channel Page of that particular user. What I want is to get the URL of this Channel Page.
    I think I'll have to create a list, with all the URLs in it, then filter them and save them into a text file.
    The problem is that I can't find a way to do that, whereas Automator offers that feature.
    Hope you can help me!
    Cheers

    I have no experience with automator but I think I understand. Here is something to get you started.
    set theURL to text returned of (display dialog "Enter URL" default answer "http://www.")
    try
    set theSource to (do shell script "curl " & theURL)
    on error
    display dialog "Error getting website source."
    end try
    This will set the variable theSource to the source code of the entered webpage. You then need to search through this source code and extract the information that you want. I'm still not exactly sure what that is though and there are going to be a ton of links in it. Hope that helps at least a little.

  • Unable to open URL from SMS message

    When I receive any SMS with link and when I click on that link the default browser opens and closes. I am not able to open any link. Due to this I have to always copy the link and paste in the browser and then try to open.
    I have changed default browser to opera mini .. but still no luck..
    I am using Nokia C7 with Anna OS. pls help

    Thanks for the reply WD ABAP.
    Yes, I did try to use the functionality of Worklist and the URL attachment successfully opened. However, there are some limitation in its functionality as indicated in the link below that opted us to use the SBWP in CRM WEB UI via transaction launcher.
    http://wiki.sdn.sap.com/wiki/display/CRM/CRMWorklist-AdvancewithDialognotSupported
    We don't want to adjust the logic of our existing workflow at this point as described in the above link as it will entails end-to-end testing again. We just encountered this issue when we update the SP level of CRM 7.
    Hope there is an alternative solution without shifting us to Worklist.
    Regards,
    Edwin

Maybe you are looking for

  • How to reduce the size of a pdf?

    How to reduce the size of a pdf?

  • How to pass variables to a program called from long text of an error msg ?

    Hi, The aim is the following: in the long text of an error message, there should be a link; this link must call a program or a transaction and pass to this program or transaction the four variables of the error message. Is it possible to insert such

  • Why can't I log into my Adobe account?

    Every time I try to log into my Adobe account the little wheel spins and spins.  Right now it's been spinning for 10 minutes.  The log-in email and password are correct and I get no notice that the user name or password are incorrect.  I have to chan

  • Script access for loaded SWFs in Flash CS3

    In ActionScript 2 I would do the following: var loader:MovieClipLoader=new MovieClipLoader(); var listener:Object=new Object(); loader.addListener(listener); listener.onLoadInit=function(m:MovieClip) { m.something="12345"; loader.loadClip("foo.swf",p

  • How do I upload cd data in itunes 11?

    In previous versions of iTunes, you could upload data to CDDB or Gracenote. Sometimes I buy obscure CDs which iTunes can find no data for. I cannot find any facility for this in iTunes 11. Am I missing something, or has the functionality been removed