Applescript or Automator support?

A place for users to ask each other questions, make suggestions, and discuss Bookends.
Post Reply
Mike
Posts: 9
Joined: Tue Jul 05, 2005 11:39 am

Applescript or Automator support?

Post by Mike »

I would like to make a script to automatically import references from external sources (web of science) by dropping the file on a script or creating a folder action. Apparently bookends does not have direct support for applescript or automator. Is there some other way to accomplish this that I don't know about (Perl, command line interface, etc) ?
Cheers
Mike
Jon
Site Admin
Posts: 10104
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Post by Jon »

You could try GUI scripting...

Jon
Sonny Software
JamesAFoster
Posts: 30
Joined: Thu Jan 05, 2006 7:03 pm
Location: Idaho
Contact:

Post by JamesAFoster »

Are there any plans to add Automator support? This would be very, very nice.
james a. foster
Jon
Site Admin
Posts: 10104
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Post by Jon »

Isn't that GUI scripting?

Jon
Sonny Software
jweaks
Posts: 9
Joined: Sat Oct 08, 2005 3:42 am
Location: Kansas City
Contact:

Post by jweaks »

Using UI scripting is most often quite a kludge. I do it sometimes when I have to work with an app that's not applescriptable. Results are rarely great. I could take a look at the UI of Bookends, if I heard more about what you're looking for.

UI scripting in Applescript is unrelated to Automator actions. The only relation is that an Automator action can run an Applescript, a script which could be using UI scripting to interface with an app.

I could consider creating a scripting library for Bookends. I'd have to think through needed routines. And, it depends on how standard the interface is. I think Bookends is written in Cocoa, right? That helps.
Jon
Site Admin
Posts: 10104
Joined: Tue Jul 13, 2004 6:27 pm
Location: Bethesda, MD
Contact:

Post by Jon »

No, Carbon.

Jon
Sonny Software
Sanderso
Posts: 8
Joined: Tue May 23, 2006 8:31 pm

Post by Sanderso »

This is an Applescript I created to automate importing PubMed references from Safari. Half the script is manipulating Safari but it also shows how to use UI scripting to control import from the clipboard in Bookends. It should be relatively easy to modify it to import files (or simply scipt opening the file in textedit and copy the information onto the clipboard).

And yes I do know that Bookends supports drag n drop and other mechanisms of importing from Pubmed. However, this gets around a problem with authenticating proxies that prevent those nice features working. A bug that should be fixed in the next (hopefully) release of Bookends.

Btw I don't code for a living. This was written on a rainy afternoon and not extensively tested. It worked for me but it's use at own risk. Feel free to use it anyway you like, but note that several utility functions are copied from the Applescript resources page (noted in the code).

Code: Select all

-- PubMed to Bookends import applescript
-- Created by Kevin Sanderson on 23/09/06

property refresh_delay : 120 -- wait loop for page refresh - increase if slow connection
property UI_delay : 1 -- delay in seconds to let the UI scripting actions happen - increase/decrease if required

-- check that UI scripting is available and turned on - if not then exit
-- NOTE: it's only the second part (actually importing into Bookends that needs this)
if not my check_can_run() then return

tell application "Safari"
	-- get the URL of the frontmost page/tab in Safari
	set theURL to URL of document 1
	
	-- check to see if the URL makes sense ie it's a PubMed page
	if theURL does not contain "www.ncbi.nlm.nih.gov" then
		display dialog "Unable to proceed. The chosen page does not appear to be PubMed or the format has changed." & return with icon caution buttons {"Quit"} default button "Quit"
		return
	end if
	-- doble check it's an AbstractPlus view
	if theURL does not contain "dopt=AbstractPlus" then
		display dialog "Unable to proceed. The chosen page does not appear to be a PubMed AbstractPlus view or the format has changed." & return with icon caution buttons {"Quit"} default button "Quit"
		return
	end if
	
	-- format is http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?db=pubmed&cmd=Retrieve&dopt=AbstractPlus&list_uids=16973271&query_hl=1&itool=pubmed_docsum
	-- replace "AbstractPlus" with "medline" and reload the URL to get the medline view
	set theURL to my replace_chars(theURL, "AbstractPlus", "medline")
	set URL of document 1 to theURL
	
	-- wait until the page has loaded
	my page_loaded(refresh_delay)
	
	--get just the data we want from the page using the DOM tree. Info we want is in a <pre></pre> tag
	set this_script to "var myNode=document.getElementsByTagName('pre');" & return & ¬
		"myNode[0].lastChild.nodeValue"
	set medline_source to do JavaScript this_script in document 1
	
	(* Begin munge the end of line marks *)
	-- NOTE if we don't do this Bookends crashes which would be bad
	-- (taken from http://www.macosxhints.com/article.php?story=20031018164326986)
	-- first we ask to put all the lines into a list (fParagraphs). AppleScript doesn't care if it is Unix, Mac or Windows.
	set fParagraphs to paragraphs of medline_source
	-- now we tell AppleScript that lists will be concatenated using return characters when the list is converted to a string 	
	set AppleScript's text item delimiters to return
	-- here we convert the list of paragraphs to strings using the text item delimiters
	set fContentsNew to fParagraphs as string
	-- here we reset the text item delimiters to "" which is default.
	set AppleScript's text item delimiters to ""
	-- finally copy the data onto the clipboard
	set the clipboard to fContentsNew
	(* end of munge the end of line marks *)
end tell

-- make sure Bookends is running and bring it to the front
activate application "Bookends"
tell application "System Events"
	tell process "Bookends"
		--bring up the import dialog
		keystroke "I" using {command down, shift down}
		delay UI_delay * 2 -- slower action so increase the delay a bit
		
		tell window "Import References"
			-- 1) select pubMed import
			click pop up button 1 --first select the menu
			pick menu item "PubMed" of menu of pop up button 1 --then select PubMed import
			delay UI_delay
			
			-- 2) select the clipboard button
			click radio button 1
			delay UI_delay
			
			--3) select the OK button
			click button "OK"
			delay UI_delay * 2 -- slower action so increase the delay a bit 
		end tell
		
		-- finally click OK in the result window
		tell window "Importing References"
			click button "OK"
		end tell
	end tell
end tell

-- utility functions taken from http://www.apple.com/macosx/features/applescript/resources.html
on replace_chars(this_text, search_string, replacement_string)
	set AppleScript's text item delimiters to the search_string
	set the item_list to every text item of this_text
	set AppleScript's text item delimiters to the replacement_string
	set this_text to the item_list as string
	set AppleScript's text item delimiters to ""
	return this_text
end replace_chars

on page_loaded(timeout_value)
	delay 2
	repeat with i from 1 to the timeout_value
		tell application "Safari"
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				return true
			else if i is the timeout_value then
				return false
			else
				delay 1
			end if
		end tell
	end repeat
	return false
end page_loaded

on check_can_run()
	set check to true
	
	tell application "System Events"
		if not ((system attribute "sysv") ? 4144) then -- not Mac OS X 10.3.0+
			beep
			display dialog "This computer can't run this script" & return & return & "The script uses Apple's GUI Scripting technology, which requires an upgrade to Mac OS X 10.3 \"Panther\" or newer." with icon caution buttons {"Quit"} default button "Quit"
			set check to false
		else if not (UI elements enabled) then
			beep
			display dialog "GUI Scripting is not enabled" & return & return & "Open System Preferences and check \"Enable access for assistive devices\" in the Universal Access preference pane, then run this script again." with icon caution buttons {"Quit"} default button "Quit"
			set check to false
		end if
	end tell
	return check
end check_can_run
Post Reply