Skip navigation
This discussion is archived

Applescript adding %0A when opening URL from Mail

1403 Views 2 Replies Latest reply: Apr 17, 2010 9:20 AM by Niel RSS
Riptor3000 Calculating status...
Currently Being Moderated
Apr 17, 2010 8:56 AM
Hi there,

I'm trying to have Mail open a url, using a Rule that opens an Applescript, whenever I email said URL to myself. It works 95% perfect, except that it adds an unnecessary %0A to the end of the string any time it opens the URL, rendering the URL useless. How can I fix this?

The script I'm using is as follows:

using terms from application "Mail"
on perform mail action with messages theMessages for rule Torrent
tell application "Mail"
repeat with theMessage in theMessages
set theText to content of theMessage
set the clipboard to theText


end repeat
end tell

open location theText

end perform mail action with messages
end using terms from
MacBook Pro, Mac OS X (10.5.8)
  • Niel Level 10 Level 10 (234,580 points)
    Currently Being Moderated
    Apr 17, 2010 9:20 AM (in response to Riptor3000)
    Use the following:

    using terms from application "Mail"
    on perform mail action with messages theMessages for rule Torrent
    tell application "Mail"
    repeat with theMessage in theMessages
    set theText to content of theMessage
    set theText to items 1 thru -2 of theText as string
    set the clipboard to theText
    end repeat
    end tell

    open location theText

    end perform mail action with messages
    end using terms from

    or -4 if you need three characters lopped off from the end.

    (51331)
    iMac Late 2007 Core 2 Duo, Mac OS X (10.6.3)
  • Camelot Level 8 Level 8 (45,670 points)
    Currently Being Moderated
    Apr 17, 2010 9:33 AM (in response to Riptor3000)
    Well, for a start you're not setting your AppleScript variable to a URL - you're setting it to the content of the email. Sure, that content may contain a URL, but it will also contain other characters such as the return after the URL, which is what the %0A translates to.

    In other words you're assuming the content of the email is just a URL, which isn't the case - it's a URL followed by a new line.

    If you're confident that the format of the email will be consistent and you just need to nix the last character you can use something like:

    set theText to content of theMessage
    set theText to (characters 1 through -2 of theText) as text


    This will chop off the last character, whatever that may be.

    If it's also just the linefeed at the end of the URL you're concerned about, you could also:

    set theText to paragraph 1 of (content of theMessage)


    This will ignore anything past the first line - useful if there's ever any other content in the message.

    One other observation - your script says:

    set the clipboard to theText


    I don't know what you plan to do with the URL in the rest of the script (if there is any), but in general you should avoid manipulating the clipboard in your script. Since this is running as a mail rule, it could get invoked at any time, including when you're working in some other application and may have copied some data to the process of some other workflow. As soon as this mail triggers you lose your previous clipboard contents in favor of the URL in the email.

    I would look more closely at what you intend to do with the URL - for example if you plan to pass that URL to some other application then look to do so directly, not via the clipboard.
    Mac OS X (10.6.2)

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.