开发者

Applescript to parse flickr url? Change size and add page link

开发者 https://www.devze.com 2023-02-06 20:28 出处:网络
What I mean, is if I have this in my clipboard for instance: \"http://farm6.static.flickr.com/5290/5377008438_8e3658d75f_m.jpg\"

What I mean, is if I have this in my clipboard for instance:

"http://farm6.static.flickr.com/5290/5377008438_8e3658d75f_m.jpg"

can I use applescript to change that to

"http://farm6.static.flickr.com/5290/5377008438_8e3658d75f_b.jpg"

(changing the "m" to a "b") ?

This would be handy becaus开发者_C百科e then I could just right click/copy the photo url from the thumbnails page without having to drill down. Yes, it's only a few clicks to get from the thumbnails page to the large size, but any I can save would be nice.

Also, could I copy out the photo id so that I can link to the main photo page?

ex:

copy the "5377008438" and paste into a main link "http://www.flickr.com/photos/dbooster/5377008438"

I only say applescript because that comes to mind, but anything that I could call from text expander would work.


Manipulating the URL can be done like this:

set baseURL to "http://farm6.static.flickr.com/5290/5377008438_8e3658d75f_m.jpg"

set modURL to (characters 1 through -6 of baseURL as text) & "b.jpg"

set fileName to last item of (tokens of baseURL between "/")
set photoID to first item of (tokens of fileName between "_")
set mainPhotoPage to "http://www.flickr.com/photos/dbooster/" & photoID

{modURL, fileName, photoID, mainPhotoPage}

on tokens of str between delimiters
    set oldTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to delimiters
    set strtoks to text items of str
    set text item delimiters of AppleScript to oldTIDs
    return strtoks
end tokens

When I run that script, I get

{"http://farm6.static.flickr.com/5290/5377008438_8e3658d75f_b.jpg", "5377008438_8e3658d75f_m.jpg", "5377008438", "http://www.flickr.com/photos/dbooster/5377008438"}

I'm not really clear whether you need help with interacting with the clipboard. But it's easy at any rate, you can just use get and set:

get the clipboard
set the clipboard to "example"


I think this should be your starting point:

changeUrl("http://farm6.static.flickr.com/5290/5377008438_8e3658d75f_m.jpg")

on changeUrl(theUrl)
  set theNewUrl to do shell script "echo " & theUrl & "| tr '_m.jpg' '_b.jpg' "
end changeUrl

...though I´m not exactly sure, what you´re trying to achieve in the end - do you want to generate a link ending in "_b.jpg" from "5377008438" being read from your clipboard?


Thanks for the replies everyone!

I apologize for not giving more details. This morning I used the code Michael gives, modifying it to take two vars (the direct photo url and the photo title) and spit out the reference end of some markdown code for displaying the photo and link, prompting me for photo dimensions and border.

It seems to work pretty well. Here is what I wrote.

copy (the clipboard) to URL_TITLE

set baseURL to first text item of (tokens of URL_TITLE between " ")
set baseTitle to ("\"" & second text item of (tokens of URL_TITLE between "\"") & "\"")

set modURL to (characters 1 through -6 of baseURL as text) & "b.jpg"

set fileName to last item of (tokens of baseURL between "/")
set photoID to first item of (tokens of fileName between "_")
set mainPhotoPage to "http://www.flickr.com/photos/dbooster/" & photoID

set photoWidth to the text returned of (display dialog "Photo Width?" default answer "980")
set photoHeight to the text returned of (display dialog "Photo Height?" default answer "605")
set photoBorder to the text returned of (display dialog "Border Size?" default answer "10")

set var6 to ("[photo]: " & modURL & " " & baseTitle & " width=" & photoWidth & "px" & " height=" & photoHeight & "px" & " style=\"border: " & photoBorder & "px solid black\" class=\"aligncenter shadow\" & "\n" & [photopage]: " & mainPhotoPage & " target=\"_blank\" rel=\"nofollow\"")

set the clipboard to var6

on tokens of str between delimiters
    set oldTIDs to text item delimiters of AppleScript
    set text item delimiters of AppleScript to delimiters
    set strtoks to text items of str
    set text item delimiters of AppleScript to oldTIDs
    return strtoks
end tokens

Like I said, all works great. I have a firefox plugin that copies the direct photo link and photo title (URL "TITLE") into the clipboard. I then assigned this script a hotkey with Fastscripts and it returns the result to the clipboard. Then I can paste the result to my post template file.

I couldn't get it to work with text expander, but Fastscripts calls it quickly enough so there is no problem.

To be honest... I have very little idea what I did. I don't know AppleScript at all. I only suggested it for my question because text expander supports it and it seemed easy enough. So even tho the script I wrote seems to work, it is nothing but me fiddling around with the code Michael gave.

So this is to say, if you see a much more efficient way to do what I did, I am all ears.

Thanks again for the help, guys!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号