AppleScript: PixelPipe Uploader

Mir hat diese Sache aus dem AppleScript-Forum keine Ruhe gelassen auch wenn ein Plug-in für Safari da wohl etwas mehr her gemacht hätte. Zur Zeit ist aber leider nix anderes drin außer AppleScript.
Es gibt 2 Versionen von dem Skript, eine als Skript-Datei, die man direkt aus Safari oder einem anderen Browser aus starten kann und eines das via Drag&Drop Dateien zu Pixelpipe hoch lädt. Die Script-Datei holt sich den Link zur einem Bild aus der Zwischenablage, lädt dieses herunter und pumpt es dann hoch auf PixelPipe. Das Programm lädt jede Bild-Datei hoch, die zuvor darauf gezogen wurde…
Ich für meinen Teil werde Pixelpipe wohl kaum nutzen, aber in dem Quellcode sind so einige nützliche Sachen drin (URL-Escapeing, Curl-Form-Upload, Keychain-Scripting…)

Skript-Datei und Skript-Programm

hubionmac's PixelPipe Uploader (545 downloads)

Quellcode der Script-Datei

Code zum markieren einmal anklicken Code im Skript-Editor öffnen

— ©hubionmac.com 10.01.2011 

— Copy an image URL to the Clipboard and the Script will upload the image to your PixelPipe Account

–Version 0.1 -> First release (11.01.2011)

–basic idea for curl file login/upload by Martin(http://siarp.de/node/196)-

property MyUserName : “” –%40 is @

property MyPassword : “”

property LoginSite : “https://pixelpipe.com/signin”

property MyUploadPage : “http://pixelpipe.com/upload”

property LogoutSite : “http://pixelpipe.com/signout”

property CookieFile : “~/.pixelpipe.cookie”

–only query login data from keychain if necessary (first run)

if MyUserName is “” then

set mylogindata to getLoginData()

set MyUserName to my returnURLEscaped((item 1 of mylogindata) as text)

set MyPassword to my returnURLEscaped((item 2 of mylogindata) as text)

end if

set mypipes to getPixelPipes()

set mylocalfile to my download_img_from_URL()

upload2pixelpipe(mylocalfile, getPostData(mylocalfile), mypipes)

on upload2pixelpipe(filepath, postdata, mypipes)

if filepath is not false then

–login&store Cookie

set curlLogin to “curl”

set curlLogin to curlLogin & ” -k”

set curlLogin to curlLogin & ” -c ” & CookieFile

set curlLogin to curlLogin & ” -b ” & CookieFile

set curlLogin to curlLogin & ” -d ’email=” & MyUserName & “&password=” & MyPassword & “&defpipe=false&noexpire=true&from=&login=Sign+In&p3next=’ ” & LoginSite

do shell script curlLogin

–getUploadCookie Stuff

set curlAddForm to “curl”

set curlAddForm to curlAddForm & ” -L”

set curlAddForm to curlAddForm & ” -c ” & CookieFile

set curlAddForm to curlAddForm & ” -b ” & CookieFile

set curlAddForm to curlAddForm & ” ” & MyUploadPage

set a to do shell script curlAddForm

–upload files

set thefile to quoted form of (POSIX path of (thefile as alias))

set curlAddForm to “curl”

set curlAddForm to curlAddForm & ” -L ” & MyUploadPage

set curlAddForm to curlAddForm & ” -c ” & CookieFile

set curlAddForm to curlAddForm & ” -b ” & CookieFile

set curlAddForm to curlAddForm & ” -F fileName_1=@” & quoted form of filepath –“/Users/hubi/Desktop/bla.png”

set curlAddForm to (curlAddForm & ” -F ‘title_1=” & (item 1 of postdata) as text) & “‘”

set curlAddForm to (curlAddForm & ” -F ‘caption_1=” & (item 2 of postdata) as text) & “‘”

set curlAddForm to (curlAddForm & ” -F ‘keywords_1=” & (item 3 of postdata) as text) & “‘”

repeat with mypipe in mypipes

set curlAddForm to curlAddForm & ” -F pipes=” & mypipe

end repeat

set curlAddForm to curlAddForm & ” -F filter_dest=”

set curlAddForm to curlAddForm & ” -F idSequence=1″

set curlAddForm to curlAddForm & ” -F upload=Upload”

do shell script curlAddForm

–signout

set curlLogout to “curl”

set curlLogout to curlLogout & ” ” & LogoutSite

set curlLogout to curlLogout & “  -L”

set curlLogout to curlLogout & ” -c ” & CookieFile

set curlLogout to curlLogout & ” -b ” & CookieFile & ” –output ~/out.html”

do shell script curlLogout

activate

display dialog “upload done” giving up after 1

else

tell application “Safari” to error “No Image detected under mouse cursor”

end if

end upload2pixelpipe

on download_img_from_URL()

(**does not work on properly

tell application “Safari”

activate

tell document 1

delay 1

do JavaScript “

function retunsrc(event)

{

blafasel= (event.target.src)

}

document.onmousemove = retunsrc;

delay 1

set imgsrc to do JavaScript “blafasel”**)

set imgsrc to the clipboard as text

if imgsrc ends with “.png” or imgsrc ends with “.jpg” or imgsrc ends with “.gif” or imgsrc ends with “.tif” or imgsrc ends with “.jpeg” then

set filename to do shell script “basename ” & quoted form of imgsrc

do shell script “cd /tmp/;curl -O ” & quoted form of imgsrc

return “/tmp/” & filename

else

return false

end if

end download_img_from_URL

on getLoginData()

–returns list of  username and password for pixelpipe

tell application “Keychain Scripting”

set myKeyChain to keychain “login.keychain”

set mykeys to every Internet key of myKeyChain whose name is “pixelpipe_upload_script”

if (count of mykeys) > 1 then

tell application “Safari”

activate

error “Too many passwords… I am confused!”

end tell

else if (count of mykeys) = 0 then

my inputNewPassword(myKeyChain)

else if (count of mykeys) = 1 then

return my return_username_and_password(item 1 of mykeys, myKeyChain)

end if

end tell

end getLoginData

on return_username_and_password(mykey, myKeyChain)

tell application “Keychain Scripting”

tell myKeyChain

return {account of mykey, password of mykey}

end tell

end tell

end return_username_and_password

on inputNewPassword(myKeyChain)

tell application “Safari”

activate

repeat until 1 = 0

set MyUserName to text returned of (display dialog “Username (Email) for Pixelpipe.com” default answer “”)

if MyUserName contains “@” then

exit repeat

end if

end repeat

repeat until 1 = 0

set MyPassword to text returned of (display dialog “Password for ” & MyUserName & ” at Pixelpipe:” default answer “” with hidden answer)

if MyUserName is not “” then

exit repeat

end if

end repeat

end tell

tell application “Keychain Scripting”

tell myKeyChain

make new Internet key with properties {server:”pixelpipe.com”, comment:”keychain login for pixelpipe upload script”, name:”pixelpipe_upload_script”, account:MyUserName, password:MyPassword}

tell application “Safari”

activate

error “Data was saved in kechain. Please restart script.”

end tell

end tell

end tell

end inputNewPassword

on returnURLEscaped(myinput)

return do shell script “echo \”” & myinput & “\\c\”| perl -p -e ‘s/([^-_.~A-Za-z0-9])/sprintf(\”%%%02X\”, ord($1))/seg'”

end returnURLEscaped

on getPixelPipes()

try

set mypipes to every paragraph of (do shell script “cat ~/.pixelpipe.pipes”)

on error

tell application “Safari”

open location “http://pixelpipe.com/destinations”

tell document 1

repeat until 1 = 0

if (do JavaScript “document.title”) = “Pixelpipe – Destinations” then

set mypipes to do JavaScript

k = new Array();

for (var i=0;i<document.links.length;i++){

if (document.links[i].href.indexOf(‘/destination/settings/’)>-1){

var tmp = document.links[i].href.replace( /.*\\//, ” )

if (k.join(‘ ‘).indexOf(tmp)==-1)

k.push(tmp + ‘ (‘ + document.links[i].text + ‘)’)

}

}

k”

exit repeat

else if (do JavaScript “document.title”) = “Pixelpipe – Sign In” then

error “Please login!”

else

delay 0.25

end if

end repeat

if (count of mypipes) = 0 then

error “No Pipes found…”

end if

end tell

end tell

set AppleScript‘s text item delimiters to

set mypipes to mypipes as text

set AppleScript‘s text item delimiters to “”

do shell script “echo ” & quoted form of mypipes & “> ~/.pixelpipe.pipes”

set mypipes to every paragraph of mypipes

end try

set mypipes to (choose from list mypipes with prompt “Post to Pipes:” with multiple selections allowed and empty selection allowed)

set r to {}

repeat with mypipe in mypipes

set r to r & word 1 of mypipe

end repeat

return r

end getPixelPipes

on getPostData(filepath)

set mytitle to text returned of (display dialog “Please enter Title:” default answer (do shell script “basename ” & quoted form of filepath))

set mybody to text returned of (display dialog “Please enter BodyText:” default answer “”)

set mytags to text returned of (display dialog “Please enter Tags:” default answer “”)

return {mytitle, mybody, mytags}

end getPostData

Und weil mir immer noch langweilig ist, gibt es auch

ein Video dazu: