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 & " "
.]
8 comments:
Very nice, thanks!
Just one small thing: if you leave the default tag completely blank, the preferences file won't contain a newline character, so the script won't read the username/password the next time round. Trivial top fix I know.. just thought I'd point it out.
Thanks for pointing that out, I had neglected to test that possibility. A blank default tag should work with the newer version of the script.
Cool thanks. I actually changed the script slightly for my own use, as I like to be able to type in my own tags for each item I post. So instead of reading from the file, it prompts for the tags every time.
I'm having a strange problem. I used the script which worked fine, then tweaked a little to be able to enter my own tags and notes for each entry and it worked the first time but then it stopped working at all. Here is the code. Any thoughts on why this is happening?
[code]
property usernamePasswordString : ""
property tagsString : ""
on run
checkUsernameAndPassword()
postToDelicious()
end run
on postToDelicious()
tell application "NetNewsWire"
if exists selectedHeadline then
set tags to text returned of (display dialog "Please enter your tags" default answer "toread ")
set notes to text returned of (display dialog "Please enter some notes" default answer " ")
set tagsString to tags & " "
set notesString to notes & " "
set u to "\"?&url=" & (URL of selectedHeadline) & ¬
"&description=" & (title of selectedHeadline) & ¬
"&extended=" & notesString & ¬
"&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
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")
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
end try
end checkUsernameAndPassword
[/code]
babak: I think I see where the problem is. When I ran your original script, the preference file the script saved was blank. I think this is because the line [code]write username & ":" & pass & {return} & tags to file prefFile[/code] references the tags variable, which in your version doesn't yet exist when it is run. Just change that line to [code]write username & ":" & pass & {return} to file prefFile[/code] and your code should run fine. Another way of avoiding errors like this would be to initialize the variable beforehand by adding [code]property tags : ""[/code] at the top of the script.
How do you install the script?
You should first open Script Editor and paste the script into a new script file. Then, save the script to the folder that opens when you select "Open Scripts Folder" from the Scripts menu in NetNewsWire.
Hi Andrew!
Post a Comment