8 comments Tuesday, March 18, 2008

Larry from ScriptingForLawyers.com has submitted a much-improved version of the NetNewsWire to del.icio.us script. His version uses the cURL terminal command to post to del.icio.us in the background, so it works without the help of Safari or Firefox. It asks you for your username and password the first time you run the script, then stores it to a preference file thereafter. Furthermore, it includes error handling to alert you if your NetNewsWire headline did not post correctly.

Here's the improved script:


property usernamePasswordString : ""
property tagsString : ""

on run
checkUsernameAndPassword()
postToDelicious()
end run

on postToDelicious()
tell application "NetNewsWire"
if exists selectedHeadline then
set u to "\"?&url=" & (URL of selectedHeadline) & ¬
"&description=" & (title of selectedHeadline) & ¬
"&tags=" & tagsString & "\""
set curlStatement to "/usr/bin/curl -u " & usernamePasswordString & " -d " & u & " https://api.del.icio.us/v1/posts/add"
set retValue to do shell script curlStatement
if retValue contains "wrong" then
display dialog "Headline did not post to del.icio.us. Something went wrong."
end if
else
display dialog "Please select a headline to post to del.icio.us"
end if
end tell
end postToDelicious

on checkUsernameAndPassword()
-- Check to see if the file where our username and password are stored exists
try
do shell script "cd " & POSIX path of (path to preferences as text) & "; ls | grep com.larrystaton.toread.txt"
try
set prefFile to ((path to preferences as text) & "com.larrystaton.toread.txt")
open for access file prefFile with write permission
set prefs to read file prefFile using delimiter {return}
close access file prefFile
set usernamePasswordString to item 1 of prefs
set tagsString to item 2 of prefs
on error e
close access file prefFile
end try
on error
set username to text returned of (display dialog "Please enter your del.icio.us username" default answer "username")
set pass to text returned of (display dialog "Please enter your del.icio.us password" default answer "password")
set tags to text returned of (display dialog "Please enter any desired default tags" default answer "toread ")
try
set prefFile to ((path to preferences as text) & "com.larrystaton.toread.txt")
open for access file prefFile with write permission
set eof of file prefFile to 0
write username & ":" & pass & {return} & tags to file prefFile
close access file prefFile
on error e
close access file prefFile
end try
set usernamePasswordString to username & ":" & pass
set tagsString to tags & " "
end try
end checkUsernameAndPassword


[Update: Larry and I have improved the script even further to fix a bug with the preference file, and to add a prompt to ask you your desired default tags the first time the script is run. If you want to later change your username/password or default tags, just trash the preference file named com.larrystaton.toread.txt in your ~/Library/Preferences/ folder, and it will ask you again for this information the next time the script is run.]

[Update 2: As Mario pointed out in the comments, the script would get into trouble when the default tag was left completely blank (it would ask for your username/password/default tag every time). The new version of the script avoids this problem by inserting a space after the default tags. I simply changed set tagsString to tags to set tagsString to tags & " ".]

5 comments Saturday, March 15, 2008

[Update: Check out a much-improved version of this script submitted by Larry of ScriptingForLawyers.com.]

I've modified existing NetNewsWire to del.icio.us Applescripts to save the currently-selected news item to del.icio.us with the tag "toread". You can easily change this tag to whatever tag you use to mark items you wish to read later on.

The script works by running JavaScript in Safari that opens a pop-up window that sends your URL and tags to del.icio.us and then closes that window. Currently, I have the window set to stay open for 0.5 seconds (delay 0.5), but you can adjust this if it wasn't enough time.

To adjust the tags that are sent to del.icio.us, edit the line containing "&tags=toread " & (subject of selectedHeadline). You can set multiple preset tags by separating them with spaces. For instance, to add tags "readlater" and "for:user1234", change this line to "&tags=readlater for:user1234 " & (subject of selectedHeadline). If you don't want the subject(s) of the new item to be added to the tags, simply remove & (subject of selectedHeadline).

tell application "NetNewsWire"
set u to (title of selectedHeadline) & ¬
"&url=" & (URL of selectedHeadline) & ¬
"&tags=toread " & (subject of selectedHeadline)
end tell

tell application "Safari"
do JavaScript "javascript:void(open('https://api.del.icio.us/v1/posts/add?description=" & u & "','delicious','toolbar=no,width=150,height=100'));" in document 1
delay 0.5
close current tab of front window
end tell


I personally run this script using a Trigger in QuickSilver.