tell application "System Events" (* Grab bookmarks tagged "to_blog?", "to_blog", & "blogged" as xml from the personalAccount. Store as files in dropbox folder for blog. *) (* Note: Double Quotes & Back Slashes must be escaped in shell commands called from AppleScript. *) (* API Token used instead of User Name & Password for a balance of ease of use and security. *) do shell script "curl \"https://api.pinboard.in/v1/posts/all?tag=to_blog&auth_token=personalAccount:NNNNNN\" -o ~/dropbox/blog\\ folder/pinboard_links/to_blog.xml" do shell script "curl \"https://api.pinboard.in/v1/posts/all?tag=to_blog%3F&auth_token=personalAccount:NNNNNN\" -o ~/dropbox/blog\\ folder/pinboard_links/to_blog?.xml" do shell script "curl \"https://api.pinboard.in/v1/posts/all?tag=blogged&auth_token=personalAccount:NNNNNN\" -o ~/dropbox/blog\\ folder/pinboard_links/blogged.xml" (* Load "to_blog" bookmarks. There is only one xml element. *) set xData to XML element 1 of contents of XML file "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:to_blog.xml" (* Parse and Send to the blogAccount. *) repeat with n from 1 to count of XML elements of xData set theURL to (get value of XML attribute named "href" of XML element n of xData) as Unicode text set theTime to (get value of XML attribute named "time" of XML element n of xData) as Unicode text (* Oddly enough, the Pinboard "Title" field is labeled "description" in the xml, while the "Description" field is labeled "extended". This is a legacy from mimicking the Delicious API. *) set theTitle to (get value of XML attribute named "description" of XML element n of xData) as Unicode text (* Characters that would be illegal in an URL must be replaced with their equivalent codes. Pinboard will auto-magically translate them back. I'm using TextWrangler's grep option for this. It is by far the biggest performance bottleneck, but is easy to implement in AppleScript.*) tell application "TextWrangler" set theTitle to replace " " using "%20" searchingString theTitle set theTitle to replace "?" using "%3F" searchingString theTitle set theTitle to replace "(" using "%28" searchingString theTitle set theTitle to replace ")" using "%29" searchingString theTitle set theTitle to replace "\"" using "%22" searchingString theTitle --Double Quotes must be escaped. set theTitle to replace ":" using "%3A" searchingString theTitle set theTitle to replace "'" using "%27" searchingString theTitle end tell set theDescription to (get value of XML attribute named "extended" of XML element n of xData) as Unicode text tell application "TextWrangler" (* Line Breaks are not represented in the xml. Use double spaces in the bookmark as an alternative encoding method. ie: text •••• text *) set theDescription to replace " " using "%0A" searchingString theDescription --Double Spaces set theDescription to replace " " using "%20" searchingString theDescription --Single Space set theDescription to replace "?" using "%3F" searchingString theDescription set theDescription to replace "(" using "%28" searchingString theDescription set theDescription to replace ")" using "%29" searchingString theDescription set theDescription to replace "\"" using "%22" searchingString theDescription set theDescription to replace ":" using "%3A" searchingString theDescription set theDescription to replace "'" using "%27" searchingString theDescription end tell (* In order to send only the "to_blog" tag and related post tags, the "tag" xml element must be parsed. *) set oldTag to (get value of XML attribute named "tag" of XML element n of xData) as Unicode text do shell script "echo " & oldTag & "> ~/dropbox/blog\\ folder/pinboard_links/tag_temp.txt" tell application "TextWrangler" (* Unlike the verb "replace", "find" will not work on arbitrary strings. One must send it files. Using "open for access" allows TextWrangler to operate on the file in the background. *) open for access file "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:tag_temp.txt" (* The "p:postTitle" tag is the convention for notating the post of which the bookmark is a source. Also, the following TextWrangler command is the worst performing portion of the script overall.*) set postFind to find "p\\W[\\S]*" searching in "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:tag_temp.txt" options {search mode:grep, showing results:false, returning results:true, starting at top:true} (* This portion of the script is only operating on "to_blog" bookmarks, so there is no need to parse for it. Setting the "to_blog" tag first ensures the "p:postTitle" tag will be listed before it. *) set postTag to "to_blog" if found of postFind is true then repeat with n from 1 to count of items of found matches of postFind (* Tags must be separated with a "+" in the url sent to Pinboard. *) set postTag to match_string of item n of found matches of postFind & "+" & postTag end repeat set postTag to replace ":" using "%3A" searchingString postTag end if set newTag to postTag close access file "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:tag_temp.txt" end tell (* Now send the properly encoded and parsed url to the the blogAccount account. *) (* curl will fail if "[]" or "{}" are included in the url. The work around is to turn globbing off. Also, AppleScript does not properly escape Double Quotes in shell scripts that are assembled with AppleScript variables. Therefore, Single Quotes are used to quote the url; this works just as well for the script. *) do shell script "curl --globoff 'https://api.pinboard.in/v1/posts/add?url=" & theURL & "&description=" & theTitle & "&extended=" & theDescription & "&tags=" & newTag & "&dt=" & theTime & "&replace=yes&auth_token=blogAccount:NNNNNN'" end repeat (* Load "to_blog?" bookmarks. Lines 15 thru 95 are repeated. *) set xData to XML element 1 of contents of XML file "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:to_blog?.xml" (* Parse and Send to the blogAccount *) repeat with n from 1 to count of XML elements of xData set theURL to (get value of XML attribute named "href" of XML element n of xData) as Unicode text set theTime to (get value of XML attribute named "time" of XML element n of xData) as Unicode text set theTitle to (get value of XML attribute named "description" of XML element n of xData) as Unicode text tell application "TextWrangler" set theTitle to replace " " using "%20" searchingString theTitle set theTitle to replace "?" using "%3F" searchingString theTitle set theTitle to replace "(" using "%28" searchingString theTitle set theTitle to replace ")" using "%29" searchingString theTitle set theTitle to replace "\"" using "%22" searchingString theTitle set theTitle to replace ":" using "%3A" searchingString theTitle set theTitle to replace "'" using "%27" searchingString theTitle end tell set theDescription to (get value of XML attribute named "extended" of XML element n of xData) as Unicode text tell application "TextWrangler" set theDescription to replace " " using "%0A" searchingString theDescription set theDescription to replace " " using "%20" searchingString theDescription set theDescription to replace "?" using "%3F" searchingString theDescription set theDescription to replace "(" using "%28" searchingString theDescription set theDescription to replace ")" using "%29" searchingString theDescription set theDescription to replace "\"" using "%22" searchingString theDescription set theDescription to replace ":" using "%3A" searchingString theDescription set theDescription to replace "'" using "%27" searchingString theDescription end tell set oldTag to (get value of XML attribute named "tag" of XML element n of xData) as Unicode text do shell script "echo " & oldTag & "> ~/dropbox/blog\\ folder/pinboard_links/tag_temp.txt" tell application "TextWrangler" open for access file "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:tag_temp.txt" set postFind to find "p\\W[\\S]*" searching in "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:tag_temp.txt" options {search mode:grep, showing results:false, returning results:true, starting at top:true} set postTag to "to_blog?" if found of postFind is true then repeat with n from 1 to count of items of found matches of postFind set postTag to match_string of item n of found matches of postFind & "+" & postTag end repeat set postTag to replace ":" using "%3A" searchingString postTag end if set newTag to postTag close access file "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:tag_temp.txt" end tell do shell script "curl --globoff 'https://api.pinboard.in/v1/posts/add?url=" & theURL & "&description=" & theTitle & "&extended=" & theDescription & "&tags=" & newTag & "&dt=" & theTime & "&replace=yes&auth_token=blogAccount:NNNNNN'" end repeat (* Load "blogged" bookmarks. Again the script is repeated. *) set xData to XML element 1 of contents of XML file "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:blogged.xml" (* Parse and Send to the blogAccount *) repeat with n from 1 to count of XML elements of xData set theURL to (get value of XML attribute named "href" of XML element n of xData) as Unicode text set theTime to (get value of XML attribute named "time" of XML element n of xData) as Unicode text set theTitle to (get value of XML attribute named "description" of XML element n of xData) as Unicode text tell application "TextWrangler" set theTitle to replace " " using "%20" searchingString theTitle set theTitle to replace "?" using "%3F" searchingString theTitle set theTitle to replace "(" using "%28" searchingString theTitle set theTitle to replace ")" using "%29" searchingString theTitle set theTitle to replace "\"" using "%22" searchingString theTitle set theTitle to replace ":" using "%3A" searchingString theTitle set theTitle to replace "'" using "%27" searchingString theTitle end tell set theDescription to (get value of XML attribute named "extended" of XML element n of xData) as Unicode text tell application "TextWrangler" set theDescription to replace " " using "%0A" searchingString theDescription set theDescription to replace " " using "%20" searchingString theDescription set theDescription to replace "?" using "%3F" searchingString theDescription set theDescription to replace "(" using "%28" searchingString theDescription set theDescription to replace ")" using "%29" searchingString theDescription set theDescription to replace "\"" using "%22" searchingString theDescription set theDescription to replace ":" using "%3A" searchingString theDescription set theDescription to replace "'" using "%27" searchingString theDescription end tell set oldTag to (get value of XML attribute named "tag" of XML element n of xData) as Unicode text do shell script "echo " & oldTag & "> ~/dropbox/blog\\ folder/pinboard_links/tag_temp.txt" tell application "TextWrangler" open for access file "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:tag_temp.txt" set postFind to find "p\\W[\\S]*" searching in "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:tag_temp.txt" options {search mode:grep, showing results:false, returning results:true, starting at top:true} set postTag to "blogged" if found of postFind is true then repeat with n from 1 to count of items of found matches of postFind set postTag to match_string of item n of found matches of postFind & "+" & postTag end repeat set postTag to replace ":" using "%3A" searchingString postTag end if set newTag to postTag close access file "Path:to:Your:Home:Folder:Dropbox:Blog Folder:Pinboard_Links:tag_temp.txt" end tell do shell script "curl --globoff 'https://api.pinboard.in/v1/posts/add?url=" & theURL & "&description=" & theTitle & "&extended=" & theDescription & "&tags=" & newTag & "&dt=" & theTime & "&replace=yes&auth_token=blogAccount:NNNNNN'" end repeat end tell