I am sending an email via applescript which sends a URL. However, the URL breaks in most email clients because it’s too long. I am sure there is a post on this, but I must not be refining my search enough as I get either thousands of results or only a few. So I think I need to embed the link within text (unless there is a way for it to not break). I can’t figure out how to do this. Here’s what I have:
set myLongUrl to "http://somethingreallylongthatbreaksthebrowser.php?id=19823749182374918234071293480129834029348012938402934"
tell application "Mail"
set addrVar to "me@email.com"
set subjectvar to "Thanks for joining"
set alert_message to "The URL for your reference is " & return & myLongUrl
set composeMessage to make new outgoing message with properties {subject:subjectvar}
tell composeMessage
make new to recipient at beginning of to recipients with properties {address:addrVar}
set the content to alert_message
end tell
send composeMessage
end tell
getTinyURL("http://somethingreallylongthatbreaksthebrowser.php?id=19823749182374918234071293480129834029348012938402934")
on getTinyURL(U)
do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & U & quote
end getTinyURL
Thanks, I had no idea about that! I am definitely not doing something right though. No email is getting sent now. Here is what I did with that:
set theBigUrl to do shell script "my shell script returns a URL when it finishes"
tell application "Mail"
set addrVar to "me@email.com"
set subjectvar to "Thanks for joining"
set alert_message to "The URL for your reference is " & return & getTinyURL(theBigUrl)
set composeMessage to make new outgoing message with properties {subject:subjectvar}
tell composeMessage
make new to recipient at beginning of to recipients with properties {address:addrVar}
set the content to alert_message
end tell
send composeMessage
end tell
on getTinyURL(U)
do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & U & quote
end getTinyURL
You should get two error massages:
First - this shell script generates an error
Second - calling getTinyURL causes a can’t continue error
Don’t debug your scripts within folder action or mail action handlers
There you won’t never get an error message. Remove also all try blocks without explicit error handling.
Try this:
set theBigUrl to "http://somethingreallylongthatbreaksthebrowser.php?id=1982382374918234071293480129834029348012938402934"
tell application "Mail"
set addrVar to "me@email.com"
set subjectvar to "Thanks for joining"
set alert_message to "The URL for your reference is " & return & my getTinyURL(theBigUrl)
set composeMessage to make new outgoing message with properties {subject:subjectvar}
tell composeMessage
make new to recipient at beginning of to recipients with properties {address:addrVar}
set the content to alert_message
end tell
send composeMessage
end tell
on getTinyURL(U)
do shell script "curl --stderr /dev/null \"http://tinyurl.com/api-create.php?url=" & U & quote
end getTinyURL
Got it. No debugging in a folder action. Everything about my day today is reinforcing that. Got this to work. Fantastic. So my final question is…the output of this in the email is something like:
It depends on the application. In TextEdit or a browser you can embed the link in HTML.
Why do you want to do this? tinyURL does just redirect the short link to the long link.
Try it with a real one
The thing is the reason I need the link is for them to copy the link that comes and then paste it into a website for a file reference. I think many of my users will forget to click it and will paste in instead the tinyurl link! So to make it foolproof I would have the tinyURL link really just say something like: Click here!
It is ridiculous, but many of my users will probably consistently mess this up unless I can do the above…
If you can send HTML emails, embedding the link in proper HTML is probably be best way to get the URL across intact. Otherwise, for plain text, you might try wrapping the URL in angle brackets or double quotes (see Appendix C. Delimiting a URI in Context). In the past I have had some luck using angle brackets to help the end-user’s email client pick out the original, long URL after it was broken up in transit.
I believe if you are using Mac OSX Mail, it will convert web addresses once it’s sent. Try one sent to yourself to see if it converts to a link on the fly.