Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Re: Applescript for Safari > TinyURL relay

26 views
Skip to first unread message

Jolly Roger

unread,
Aug 11, 2015, 5:55:50 PM8/11/15
to
On 2015-08-11, gtr <x...@yyy.zzz> wrote:
> I've had an macro that triggered an applescript that snatched the
> current URL, fed it to TinyURL, and popped the result in the paste
> buffer. It was sweet. I assume I got it from the JR, but am unsure.

I don't remember it if I did write it, which admittedly is possible. : )

I tend to use something simpler anyway. I have a Safari Favorites Bar
bookmark named "TinyURL" with the location set to this single-line
script:

javascript:void(location.href='http://tinyurl.com/create.php?url='+encodeURIComponent(location.href))

To use it I just click the bookmark (it appears as a button on the
Safari Favorites Bar near the top of the browser window), and I am
brought to the Tiny URL page where I can copy the URL.

This script goes a step further and places the URL on the clipboard,
which is nice.

> It gotten broken in recent months, maybe Yosemite, maybe something
> else. I've looked elsewhere for another, found a few, none worked. The
> last known operable one was is below.
>
> Does anyone know of a good replacement or a fix to the script? Many
> thanks for any aid.
>
> tell application "Safari"
> try
> set askURL to "http://tinyurl.com/create.php?url=" & URL of document 1
> on error
> return
> end try
> end tell
> set theResult to do shell script "curl -f -s --connect-timeout 10 -m 20
> '" & askURL & "'"
> set oldDelim to AppleScript's text item delimiters
>
> try
> set AppleScript's text item delimiters to "<blockquote><b>"
> set tinyURL to text item 3 of theResult
> set AppleScript's text item delimiters to "</b>"
> set the clipboard to text item 1 of tinyURL
> set AppleScript's text item delimiters to oldDelim
> on error
> set AppleScript's text item delimiters to oldDelim
> end try

Ok, so the script is looking for "<blockquote><b>" on the page. I just
created a tiny URL to slashdot.com and looked at the resulting page
source. There is no blockquote element on the page. So it looks like it
wasn't Yosemite, but a change on the Tiny URL web page that caused the
script to break.

Instead, I see this:

<div class="indent longurl"><b>http://slashdot.org/</b></div>
has a length of 20 characters and resulted in the following TinyURL
which has a length of 22 characters:
<div class="indent"><b>http://tinyurl.com/6cr</b><div
id="success"></div><br><small>[<a href="http://tinyurl.com/6cr"
target="_blank">Open in new window</a>]</small><div
id="copy_div"></div></div>
Or, give your recipients confidence with a preview TinyURL:
<div
class="indent"><b>http://preview.tinyurl.com/6cr</b><br><small>[<a
href="http://preview.tinyurl.com/6cr" target="_blank">Open in new
window</a>]</small>
</div></p>

So the original URL is in a div.longurl element, and the tiny URL is in
the following div.indent element. The div.indent element after that
holds the preview version of the tiny URL.

So one thing you might do is modify the script to look for "<div
class="indent"><b>" to get the tiny URL. To do that, just change these
two lines like so:

set AppleScript's text item delimiters to "<div class=\"indent\"><b>"
set tinyURL to text item 2 of theResult

Everything else could stay the same.

That's a really inefficient and problem-prone way to do it though,
because if the Tiny URL web page changes again, your script could break
again.

Tiny URL does have an API that you can call to get tiny URLs without
having to parse HTML. When you call the API, the only thing returned is
the tiny URL itself - no HTML or anything else.

So here's the script modified to use the API - you can see it's much
simpler and won't break if the Tiny URL web page changes.

-- begin script
tell application "Safari"
try
set askURL to "http://tinyurl.com/api-create.php?url=" & URL of document 1
set tinyURL to do shell script "curl -f -s --connect-timeout 10 -m 20 '" & askURL & "'"
set the clipboard to tinyURL
on error
beep
return
end try
end tell
-- end script

Here's a more compact single-line version:

tell application "Safari" to set the clipboard to (do shell script "curl
-f -s --connect-timeout 10 -m 20 http://tinyurl.com/api-create.php?url="
& URL of document 1)

--
E-mail sent to this address may be devoured by my ravenous SPAM filter.
I often ignore posts from Google. Use a real news client instead.

JR

gtr

unread,
Aug 11, 2015, 6:12:14 PM8/11/15
to
On 2015-08-11 21:55:48 +0000, Jolly Roger said:

> Ok, so the script is looking for "<blockquote><b>" on the page. I just
> created a tiny URL to slashdot.com and looked at the resulting page
> source. There is no blockquote element on the page. So it looks like it
> wasn't Yosemite, but a change on the Tiny URL web page that caused the
> script to break.

I thought that might be the case so I sent them an email to verify.

> So one thing you might do is modify the script to look for "<div
> class="indent"><b>" to get the tiny URL. To do that, just change these
> two lines like so:
>
> set AppleScript's text item delimiters to "<div class=\"indent\"><b>"
> set tinyURL to text item 2 of theResult

Dood! You totally rool!

> Everything else could stay the same.
>
> That's a really inefficient and problem-prone way to do it though,
> because if the Tiny URL web page changes again, your script could break
> again.

Which is apparently what happened recently anyway.

> Tiny URL does have an API that you can call to get tiny URLs without
> having to parse HTML. When you call the API, the only thing returned is
> the tiny URL itself - no HTML or anything else.
>
> So here's the script modified to use the API - you can see it's much
> simpler and won't break if the Tiny URL web page changes.
>
> -- begin script
> tell application "Safari"
> try
> set askURL to "http://tinyurl.com/api-create.php?url=" & URL of document 1
> set tinyURL to do shell script "curl -f -s --connect-timeout 10 -m 20
> '" & askURL & "'"
> set the clipboard to tinyURL
> on error
> beep
> return
> end try
> end tell
> -- end script

That works good too!

> Here's a more compact single-line version:
>
> tell application "Safari" to set the clipboard to (do shell script "curl
> -f -s --connect-timeout 10 -m 20 http://tinyurl.com/api-create.php?url="
> & URL of document 1)

That doesn't seem to work for me but no matter--two wholly functional
solutions is at least one more than I need already. Many thanks!

0 new messages