Bridging the Web Browser and Filesystem

This isn’t specifically a DTpro question, but someone here may be able to comment.

Despite using Bookends, I still prefer to use Safari when searching PubMed and browsing large result sets. It would be helpful if I could quickly update the browser page to indicate which refs I already have as a local PDF. I keep my PDFs in a single Attachments folder for Bookends, and I include the unique PMID in the filenames.

Using Javascript, it is easy to extract all the PMIDs from a web based search result. But is it possible to interrogate the filesystem, then alter the markup of the PMIDs in the web page (e.g. highlight or link)? Does the web browser built into DT or DA have scripting hooks to make this easier?

I suppose it would have to be done from an Applescript wrapper, using the “do JavaScript” command to interact with the browser web page. It would just be convenient to have the script operate automatically when a page loads (e.g. from a custom stylesheet calling JavaScript), but I don’t believe that type of trigger is feasible from AppleScript. Or at least activate via a bookmarklet, but JS cannot call an applescript, AFAIK.

PS. Just in case there is a plug-in, note that I do not want to use Firefox.

– mpm

AppleScript can use JavaScript and can access the filesystem. In addition, the AppleScript suites of DEVONthink Pro and DEVONagent include several commands for simple HTML parsing.

It should also be possible to modify the DOM document visible in Safari via the AppleScript/JavaScript combo, here’s a super-simple example:


tell application "Safari"
	do JavaScript "document.write(\"<body>Hello</body>\");" in current tab of window 1
end tell

Greetings,

I have an RSS feed to a pub med query that updates daily. Once I have identified the articles I would like to read (e.g., 10-15), I would like to create a list of the PMIDs from each of the articles. Ideally, I would love to have a script that would connect to my reference manager, Papers, so that they could be automatically imported. In my mind, this sounds like an easy task to accomplish I just lack the knowledge to do so. Any help in accomplishing any of these goals are greatly appreciated.

Cheers,
Jason

What’s the URL of the feed?

Thanks for the quick response.

The url for the feed is:
feed://www.ncbi.nlm.nih.gov/entrez/euti … 6DrtcG69lT

I tried but could not attach a screenshot to help clarify the other aspects of the question I posed.

Here’s a simple script to retrieve the PMIDs from the selected items:


tell application id "DNtp"
	set theSelection to the selection
	set theList to {}
	repeat with theRecord in theSelection
		set theText to plain text of theRecord
		if theText contains "PMID: " then
			set AppleScript's text item delimiters to "PMID: "
			set thePMID to (text item 2 of theText) as string
			if thePMID contains " [" then
				set AppleScript's text item delimiters to " ["
				set thePMID to (text item 1 of thePMID) as string
				set theList to theList & thePMID
			end if
		end if
	end repeat
	return theList
end tell

Thank you!

This was extremely helpful. Now all I have to do is work out the way to compile these into a list and then have them read by Papers or BibTex so they can be imported as references…

With a little time I hope to develop some competency with AppleScripts…

Greetings Christian,

The simple script you created has been immensely helpful - thank you. However, as you can see from my attempts embedded below your original code, I’ve been trying to turn the results from your script into a list of some sort that I will then try to import into the Papers preference manager. Any help you can provide would be greatly appreciated. In fact, if you know of some good exercises where I can get practice with this sort of thing please let me know. As always, many thanks for your help.


-- ############################################
--
-- 		ORIGINALLY CREATED BY:	Created by Christian Grunenberg 
--		ORIGIANNLY CREATED ON:	FEB 26, 2015
--           									
--           	COMMENTS BY:	JASON CRAGGS	
-- 		EDITED ON:		2015_02_26
--		MODIFIED ON:	2015_03_03 		
-- 										
-- 		COMMENTS:		USED TO EXTRACT PMID INFORMATION FROM RSS FEEDS IN DTpro			
-- 		COMMENTS:		TRYING TO PUT THE EXTRACTED PMIDS INTO A LIST								
--                                              			
-- ############################################
-- 
-- 
-- 


tell application id "DNtp"
	set theSelection to the selection
	set theList to {}
	set theCnt to 0 --added this section
	repeat with theRecord in theSelection
		set theText to plain text of theRecord
		if theText contains "PMID: " then
			set AppleScript's text item delimiters to "PMID: "
			set thePMID to (text item 2 of theText) as string
			if thePMID contains " [" then
				set AppleScript's text item delimiters to " ["
				set thePMID to (text item 1 of thePMID) as string
				set theList to theList & thePMID
				set theCnt to theCnt + 1 -- added this section
			end if
		end if
	end repeat
	return theList
	
	tell application "System Events"
		keystroke "c" using {command down}
		delay 0.5
	end tell
end tell

(*
	set this_app to (path to frontmost application as string)
	
	try
		if this_app does not contain "DEVONthink Pro" then
			tell application this_app to activate
			tell application "System Events"
				keystroke "c" using {command down}
				delay 0.5
				tell application id "DNtp" to paste clipboard
			end tell
		end if
	on error error_message number error_number
		if error_number is not -128 then display alert "DEVONthink Pro" message error_message as warning
	end try
	*)





(*
	
	
	
	
	set clipboard to theList
	tell application "TextEdit" to activate --make sure TextEdit is running
	tell application "System Events"
		keystroke "c" using {command down}
		delay 0.5
		tell application id DNtp to paste clipboard
	end tell
		
	--	make new document
	--	make new paragraph at end of document 1 with theList
		--	make new paragraph at end of document 1 with data (return & (the clipboard))
	end tell
	end tell
	
	*)

--return theCnt
-- item 1 of {theList}
--return theList & theCnt
-- set the clipboard to {theList}

(*
	-- added the whole next if-then statement
	if (theCnt > 0) then
		create record with {name:(theCnt as string) & " comments", type:txt, plain text:theList} in current group
		return name
		return theList
	end if
	
	*)




(*

-- set the clipboard to "Add this sentence at the end."
set the clipboard to {theList}
tell application "TextEdit"
	activate --make sure TextEdit is running
	make new document
	-- make new paragraph at end of document 1 with data (return & (theList))
	--	make new paragraph at end of document 1 with data (return & (the clipboard))
end tell


*)

(*
tell application "TextEdit"
	activate --make sure TextEdit is running
	make new document
	item 1 of {theList}
end tell
*)

(*
tell application "TextWrangler"
	activate
	make new text document
	paste theList
	
end tell
*)



Here’s a simple revision of the first script creating a text document containing the list:


tell application id "DNtp"
	set theSelection to the selection
	set theList to ""
	repeat with theRecord in theSelection
		set theText to plain text of theRecord
		if theText contains "PMID: " then
			set AppleScript's text item delimiters to "PMID: "
			set thePMID to (text item 2 of theText) as string
			if thePMID contains " [" then
				set AppleScript's text item delimiters to " ["
				set thePMID to (text item 1 of thePMID) as string
				set theList to theList & thePMID & return
			end if
		end if
	end repeat
	create record with {name:"List", type:text, content:theList} in (incoming group of current database)
end tell

Thank you!
This is extremely helpful. I can see I was close but yet sooooo far!