Capturing feed entries

A few months ago I wrote a script to capture entries from feeds subscribed to in DevonThink.

I use it as follows: I select the first entry of the feed in DEVONThink and decide whether or not I want to import the associated web page. When I want to import the page I press the down arrow key to advance to the next feed, when I don’t want to import the page I press delete to remove the entry from the feed. When I have ‘read’ all the entries in the feed, I select the feed and let the script import the entries.

I hope this script is useful to some of you.

Copy the script, paste it in a new window in your AppleScript Editor and save it in Script format under an appropriate name (what about ‘Capture entries from selected feeds’?) in a convenient temporary place (such as the desktop or your downloads folder). Open DEVONThink, select ‘Open Scripts Folder’ in its Scripts menu and drag your new script from its convenient temporary place to DEVONThink’s ‘Scripts’ folder or one of its subfolders (can I suggest the ‘Feeds’ subfolder?). Return to DEVONThink, choose ‘Update Scripts Menu’ and you are ready.

To use it select a feed in your database and activate the script in DEVONThink’s Scripts menu.


-------------------------------------------------------------------------
-- capture entries from selected feeds
-- Arno Wouters, 5 January 2013
-------------------------------------------------------------------------
tell application id "com.devon-technologies.thinkpro2"
	
	try
		set theSelection to the selection as reference
	on error errMsg number errnum
		if errnum = -1700 then
			error "Please select one or more feeds before running this script."
		else
			error "An unexpected error occurred:  " & return & return & "Error " & errnum & ": " & errMsg & return
		end if
	end try
	
	
	repeat with thisFeed in theSelection
		
		if (type of thisFeed) is feed then
			
			set theDatabase to the database of theSelection
			
			set theTrash to the trash group of theDatabase
			
			set theDestination to the parent of thisFeed as reference
			
			set FeedEntries to the children of thisFeed
			
			repeat with thisEntry in FeedEntries
				set theurl to the URL of thisEntry
				create web document from theurl in theDestination
				move record thisEntry to theTrash
			end repeat
			
		end if
		
	end repeat
	
end tell

A few days ago Jeff Porter from Wordius called my attention to Pinboard’s RSS feeds. It occurred to me that these feeds can be used to send articles from my iPad to DEVONThink. After all, all the relevant apps on my iPad (Mr. Reader, Instapaper, iCab mobile and Safari) allow me to bookmark pages on pinboard and tag them with ‘todevon’. So I created an RSS feed with the name ‘Pinboard’ in DEVONThink’s global inbox to capture the bookmarks tagged to ‘todevon’ and modified the above script to automatically import the bookmarked pages when activated (no need to select the feed before calling the script).

This is the modified script:


-------------------------------------------------------------------------
-- capture entries from Pinboard's feed
-- Arno Wouters, 4 July 2013
-------------------------------------------------------------------------
-- set the properties needed to locate the relevant feed
-------------------------------------------------------------------------
-- 1) set nameofDatabase to the name of the database that contains the feed
--     use "" if the feed is in the global inbox

set nameofDatabase to "" 


-- 2) set pathtoFeed to the path to the feed
--     e.g. the path to the feed 'Pinboard feed' in the 'My feeds' group is
--        "/My feeds/Pinboard feed"

set pathtoFeed to "/Pinboard"

-------------------------------------------------------------------------
-- don't change anything below this heading
-------------------------------------------------------------------------
tell application id "com.devon-technologies.thinkpro2"
	
	if nameofDatabase is "" then
		set theDatabase to inbox -- the global inbox
	else
		set theDatabase to database named nameofDatabase
	end if
	
	set theTrash to the trash group of theDatabase
	
	set theFeed to get record at pathtoFeed in theDatabase
	
	set theDestination to incoming group of theDatabase
	
	set FeedEntries to the children of theFeed
	
	repeat with thisEntry in FeedEntries
		set theurl to the URL of thisEntry
		create web document from theurl in theDestination
		move record thisEntry to theTrash
	end repeat
	
end tell
-------------------------------------------------------------------------

As it is, this script works with the feed “Pinboard” at the root level of the global inbox. To use it with a feed with another name or at another location you have to modify the first two lines of the script in your AppleScript Editor:

  1. in the first active line of the script (set nameofDatabase to “”): put the name of your database between the quotes (if your database is named ‘My Database’ this line should read: set nameofDatabase to “My Database”)

  2. in the second active line of the script (set pathtoFeed to “/Pinboard”) replace /Pinboard with the path to your feed (if the script should capture the entries of the feed ‘Instapaper’ in the ‘My Feeds’ group, this line should read: set pathtoFeed to “/My Feeds/Instapaper”)

Don’t change anything else!

Comments, criticisms and suggestions for improvement are welcome!

Very nice, @arnow, thank you.

An couple of options for “theDestination” group could be


set theDestination to create location "/Saved Feeds/This Feed/" in theDatabase

which will place the web archives in a location other than the parent group of the feeds. (The group name above is an example, not a requirement.)

or


set theDestination to display group selector

which will prompt each time for the destination group – the advantage here being that the destination group can be in any open database, including optionally a new group.

Thanks Korm, glad you like it!

I especially like the idea to use the group selector in connection with the ‘capture entries of selected feeds’ script! I put that variant besides the older one in my DEVONThink’s Scripts folder.

Below is a modified ‘capture entries from a feed’ script (the one I use for my pinboard feed) that puts the captured feed entries in the inbox of the database (as did version 1) but can easily be changed to another destination: just put the path to the destination group between the quotes in the third active line (set pathtoDestination to “”).


-----------------------------------------------------------------
-- capture entries from a feed, version 2.1
-- Arno Wouters, 12 March 2014
-- version 1 4 july 2013
-- version 2 modified in response to comments from Korm, 8 July 2013
-- version 2.1 fixed a bug located by korm, 25 Jul 2013
-----------------------------------------------------------------
-- set the properties needed to locate the relevant records
-----------------------------------------------------------------
-- 1) set nameofDatabase to the name of the database that contains the feed
--     use "" if the feed is in the global inbox

set nameofDatabase to ""

-- 2) set pathtoFeed to the path to the feed
--     e.g. the path to the feed 'Pinboard feed' in the 'My feeds' group is
--        "/My feeds/Pinboard feed"

set pathtoFeed to "/Pinboard"

-- 3) set pathtoDestination to the path to the group in which to place the entries
--     e.g. the path to the group 'Captured feeds' in the 'My feeds' group is
--        "/My feeds/Captured feeds"
--     use "" to collect the entries in the inbox of the database with the feed

set pathtoDestination to ""

-----------------------------------------------------------------
-- don't change anything below this heading
-----------------------------------------------------------------
tell application id "com.devon-technologies.thinkpro2"
	
	if nameofDatabase is "" then
		set theDatabase to inbox -- the global inbox
	else
		set theDatabase to database named nameofDatabase
	end if
	
	set theTrash to the trash group of theDatabase
	
	if the pathtoDestination is "" then
		set theDestination to incoming group of theDatabase
	else
		set theDestination to create location pathtoDestination in theDatabase
	end if
	
	set theFeed to get record at pathtoFeed in theDatabase
	
	set FeedEntries to the children of theFeed
	
	repeat with thisEntry in FeedEntries
		set theurl to the URL of thisEntry
		create web document from theurl in theDestination
		move record thisEntry to theTrash
	end repeat
	
end tell
-----------------------------------------------------------------

Note (12 March 2014): I just fixed the bug reported by Truhe » Tue 23 Jul 2013 19:50 and located by by korm » Thu 25 Jul 2013 3:58.

Hello,

I’m getting

when using the last script variant. I manually created a group in the root level of the database and put /groupname into the script:


set pathtoDestination to "/Filmbewertungen"

I’m getting the error in this line:


set theDestination to create location pathtoDestination in database theDatabase

I tried it with no destination (uses the inbox) and with the group picker (uses the selected group).

Any idea what I’m doing wrong?

Bye,
Oliver

That error frequently means there is an error in a statement that sets a path. For example, what assignment do you have on this line:


set nameofDatabase to ""

If there is a value other than a null string ("") – and that value is an invalid path – then this code section might fail in the “else” section

if nameofDatabase is "" then
		set theDatabase to inbox -- the global inbox
	else
		set theDatabase to database named nameofDatabase
	end if
		set nameofDatabase to "test"
		set pathtoFeed to "/Filmbewertungen/IMDB-Import"
		set pathtoDestination to ""

It can read the feed and only breaks, when I write something like this:

		set nameofDatabase to "test"
		set pathtoFeed to "/Filmbewertungen/IMDB-Import"
		set pathtoDestination to "/Filmbewertungen"

This happens:

As mentioned the script can read the feed which is also stored in the group “/Filmbewertungen”, but it fails when it has to set this as a path for creating new files.

This is the code:

set nameofDatabase to "test"
set pathtoFeed to "/Filmbewertungen/IMDB-Import"
set pathtoDestination to "/Filmbewertungen"

tell application id "com.devon-technologies.thinkpro2"
	
	if nameofDatabase is "" then
		set theDatabase to inbox -- the global inbox
	else
		set theDatabase to database named nameofDatabase
	end if
	
	set theTrash to the trash group of theDatabase
	
	set theFeed to get record at pathtoFeed in theDatabase
	
	set FeedEntries to the children of theFeed
	
	repeat with thisEntry in FeedEntries
		
		-- Use this block to get the Instapaper page
		--set theRawUrl to URL of thisEntry
		--set search_string to "http://"
		--set replacement_string to ""
		--set AppleScript's text item delimiters to search_string
		--set text_item_list to every text item of theRawUrl
		--set AppleScript's text item delimiters to replacement_string
		--set theRawUrl to text_item_list as string
		--set theurl to "http://www.instapaper.com/text?u=http%3A%2F%2F" & theRawUrl
		
		-- Or use this line to get the full page
		set theurl to the URL of thisEntry
		
		set thisText to rich text of thisEntry
		set rating to ""
		set newLabel to ""
		set newTags to ""
		set rating to last word of thisText
		if the pathtoDestination is "" then
			set theDestination to incoming group of theDatabase
		else
			set theDestination to create location pathtoDestination in database theDatabase
		end if
		
		
		if rating is "1" then
			set newLabel to 6
			set newTags to "0/5;1/10"
		else if rating is "2" then
			set newLabel to 5
			set newTags to "1/5;2/10"
		else if rating is "3" then
			set newLabel to 4
			set newTags to "2/5;3/10"
		else if rating is "4" then
			set newLabel to 4
			set newTags to "2/5;4/10"
		else if rating is "5" then
			set newLabel to 3
			set newTags to "3/5;5/10"
		else if rating is "6" then
			set newLabel to 3
			set newTags to "3/5;6/10"
		else if rating is "7" then
			set newLabel to 2
			set newTags to "4/5;7/10"
		else if rating is "8" then
			set newLabel to 2
			set newTags to "4/5;8/10"
		else if rating is "9" then
			set newLabel to 1
			set newTags to "5/5;9/10"
		else if rating is "10" then
			set newLabel to 1
			set newTags to "5/5;10/10"
		else
			set theDestination to incoming group of theDatabase
		end if
		
		set createDate to creation date of thisEntry
		set newDoc to create web document from theurl in theDestination
		if newLabel is not "" then
			set label of newDoc to newLabel
			set the tags of newDoc to newTags
			set unread of newDoc to false
			
		end if
		set creation date of newDoc to createDate
		move record thisEntry to theTrash
		exit repeat
	end repeat
	
end tell
-----------------------------------------------------------------

This


set theDestination to create location pathtoDestination in database theDatabase

should be this


set theDestination to create location pathtoDestination in theDatabase

Ah, I see. Copied that error from arnow. Thanks!

Korm and Truhe, thanks for reporting and locating the bug! Unfortunately, the e-mail notifying me of your comments had escaped my attention until today.

I have edited the script of 08 Jul 2013 8:08 in order to repair the bug.

The following version not only fixes the bug but also checks for the existence of the relevant database and feed (a possible problem pointed out by korm in his comment of Tue 23 Jul 2013 20:43).


-----------------------------------------------------------------
-- Capture entries from a feed, version 2.2
-- Arno Wouters, 12 March 2014
-- version 1 4 july 2013
-- version 2 modified in response to comments from Korm, 8 July 2013
-- version 2.1 fixed a bug located by korm (25 Jul 2013), 12 March 2014
-- version 2.2 added checks for the existence of the relevant database and feed, 12 March 2014
-----------------------------------------------------------------
-- set the properties needed to locate the relevant records
-----------------------------------------------------------------
-- 1) set nameofDatabase to the name of the database that contains the feed
--  use "" if the feed is in the global inbox

set nameofDatabase to ""

-- 2) set pathtoFeed to the path to the feed
--  e.g. the path to the feed 'Pinboard feed' in the 'My feeds' group is
--    "/My feeds/Pinboard feed"

set pathtoFeed to "/Pinboard"

-- 3) set pathtoDestination to the path to the group in which to place the entries
--  e.g. the path to the group 'Captured feeds' in the 'My feeds' group is
--    "/My feeds/Captured feeds"
--  use "" to collect the entries in the inbox of the database with the feed

set pathtoDestination to ""

-----------------------------------------------------------------
-- don't change anything below this heading
-----------------------------------------------------------------
tell application id "com.devon-technologies.thinkpro2"
	
	if nameofDatabase is "" then
		set theDatabase to inbox -- the global inbox
	else
		try
			set theDatabase to database named nameofDatabase
		on error errMsg number errnum
			if errnum = -1728 then
				error "Database \"" & nameofDatabase & "\" doesn't exist"
			else
				error "An unexpected error occurred:" & return & return & " Error " & errnum & ": " & errMsg & return
			end if
		end try
	end if
	
	set theTrash to the trash group of theDatabase
	
	if the pathtoDestination is "" then
		set theDestination to incoming group of theDatabase
	else
		set theDestination to create location pathtoDestination in theDatabase
	end if
	
	set theFeed to get record at pathtoFeed in theDatabase
	
	if not (exists theFeed) then error "Feed \"" & pathtoFeed & "\" of database \"" & name of theDatabase & "\" does not exist."
	
	set FeedEntries to the children of theFeed
	
	repeat with thisEntry in FeedEntries
		set theurl to the URL of thisEntry
		create web document from theurl in theDestination
		move record thisEntry to theTrash
	end repeat
	
end tell
-----------------------------------------------------------------

@Arnow, welcome back … been a long time :slight_smile: Thanks for the updated script.