4kld
1/23/2018 - 7:43 PM

Pinboard archive script for Devonthink Pro that archives bookmarks as web documents rather than just links

Pinboard archive script for Devonthink Pro that archives bookmarks as web documents rather than just links

-- Import Pinboard bookmarks
-- Created by Christian Grunenberg on Mon Jan 23 2006.
-- Modified by Rafael Bugajewski to support Pinboard instead of Delicious on Sun Dec 19 2010.
-- Modified by Andreas Zeitler on Sun Mar 03 2011 to display user feedback when finished
-- Modified by Mitchell Hewes on 2017-04-03 to archive bookmarks as PDF documents
-- Copyright (c) 2006-2014. All rights reserved.

-- If the properties pUser and pPassword are undefined then a dialog pops up asking for these properties
property pUser : ""
property pPassword : ""

tell application id "com.devon-technologies.thinkpro2"
	try
		if (pUser is "") or (pPassword is "") then
			repeat while (pUser is "") or (pPassword is "")
				set theResult to display authentication dialog "Pinboard" & return & "https://api.pinboard.in/v1/posts/all"
				set pUser to user of theResult
				set pPassword to |password| of theResult
			end repeat
		end if
		
		set theXML to download markup from "https://api.pinboard.in/v1/posts/all" user pUser password pPassword encoding "UTF-8"
		
		if theXML is missing value or theXML is "" then
			error "Download failed."
		else if theXML contains "503 Service Temporarily Unavailable" then
			error "503 Service Temporarily Unavailable"
		else
			try
				tell application "System Events"
					set x to make new XML data with data theXML
					set theElements to XML elements of (XML element 1 of x) -- posts
					tell application id "com.devon-technologies.thinkpro2"
						set theGroup to get record at "/Pinboard"
						if theGroup is missing value or type of theGroup is not group then
							set theGroup to create location "/Pinboard"
							set thumbnail of theGroup to "http://pinboard.in/bluepin.gif"
						end if
					end tell
					repeat with theElement in theElements
						set theUrl to (value of XML attribute named "href" of theElement) as string
						set theName to (value of XML attribute named "description" of theElement) as string
						set theTag to (value of XML attribute named "tag" of theElement) as string
						set theComment to (value of XML attribute named "extended" of theElement) as string
						
						set {od, AppleScript's text item delimiters} to {AppleScript's text item delimiters, " "}
						try
							set theTags to (text items of theTag)
						on error
							set theTags to {}
						end try
						set AppleScript's text item delimiters to od
						
						tell application id "com.devon-technologies.thinkpro2"
							if not (exists record with URL theUrl) then
								try
									set theRecord to create web document from theUrl name theName in theGroup without pagination
									set tags of theRecord to theTags
									set URL of theRecord to theUrl
									set comment of theRecord to theComment
								on error
									create record with {name:theName, type:bookmark, URL:theUrl, tags:theTags, comment:theComment} in theGroup
								end try
							end if
						end tell
					end repeat
				end tell
			on error msg
				set pUser to ""
				set pPassword to ""
				error "Invalid user/password."
			end try
		end if
		
		-- when the script is finished it should tell the user
		tell application "Finder"
			if exists POSIX file "/System/Library/Sounds/Glass.aiff" then
				do shell script "afplay /System/Library/Sounds/Glass.aiff"
			end if
			display dialog "Import has finished."
		end tell
	on error error_message number error_number
		if the error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
end tell