Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Applescript adding %0A when opening URL from Mail

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)

Posted on Apr 17, 2010 8:56 AM

Reply
2 replies

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)

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.

Applescript adding %0A when opening URL from Mail

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