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 & " "
.]