Path: csiph.com!goblin3!goblin.stu.neva.ru!news.tu-darmstadt.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Jolly Roger Newsgroups: comp.sys.mac.system,alt.comp.lang.applescript Subject: Re: Applescript for Safari > TinyURL relay Date: 11 Aug 2015 21:55:48 GMT Organization: People for the Ethical Treatment of Pirates Lines: 118 Message-ID: References: <2015081113194714113-xxx@yyyzzz> X-Trace: individual.net vo2NhoMiwbshi7xLlwRykwxND33E6mQrY/BqbtA1r+OZl5tvJi Cancel-Lock: sha1:xvQAA4p/Arh4LLDUkNiYka621MI= X-Face: _.g>n!a$f3/H3jA]>9pN55*5<`}Tud57>1Y%b|b-Y~()~\t,LZ3e up1/bO{=-) User-Agent: slrn/1.0.1 (Darwin) Xref: csiph.com comp.sys.mac.system:78562 alt.comp.lang.applescript:1 On 2015-08-11, gtr 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 "
" > set tinyURL to text item 3 of theResult > set AppleScript's text item delimiters to "" > 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 "
" 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:
http://slashdot.org/
has a length of 20 characters and resulted in the following TinyURL which has a length of 22 characters:
http://tinyurl.com/6cr

[Open in new window]
Or, give your recipients confidence with a preview TinyURL:
http://preview.tinyurl.com/6cr
[Open in new window]

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 "
" to get the tiny URL. To do that, just change these two lines like so: set AppleScript's text item delimiters to "
" 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