General script for saving web archives (uses Growl)

The following script will work for all browsers. I recommend using FastScripts Lite (an excellent, free product) for binding this script to Cmd-I in all of your browsers.

It will save the current page as a webarchive in your DEVONthink – in the background – and then display a Growl notification on successful completion.

Uncomment the Camino and/or Opera sections if you use those browsers. If you use OmniWeb, replace the word Safari in the following script to OmniWeb.

tell application "GrowlHelperApp"
	-- Make a list of all the notification types 
	-- that this script will ever send:
	set the allNotificationsList to ¬
		{"Add webarchive"}
	
	-- Make a list of the notifications 
	-- that will be enabled by default.      
	-- Those not enabled by default can be enabled later 
	-- in the 'Applications' tab of the growl prefpane.
	set the enabledNotificationsList to ¬
		{"Add webarchive"}
	
	-- Register our script with growl.
	-- You can optionally (as here) set a default icon 
	-- for this script's notifications.
	register as application ¬
		"WebImporter" all notifications allNotificationsList ¬
		default notifications enabledNotificationsList ¬
		icon of application "DEVONthink Pro"
end tell

on getCurrentURL()
	tell application "System Events"
		set myApp to name of first application process whose frontmost is true
		if myApp is "Camino" then
			-- tell application "Camino"
			-- URL of window 1
			-- end tell
		else if myApp is "firefox-bin" then
			tell application "Firefox"
				«class curl» of window 1
			end tell
		else if myApp is "Safari" then
			tell application "Safari"
				URL of document 1
			end tell
		else if myApp is "Opera" then
			-- tell application "Opera"
			-- set myInfo to GetWindowInfo of window 1
			-- item 1 of myInfo
			-- end tell
		else
			return
		end if
	end tell
end getCurrentURL

tell application "DEVONthink Pro"
	set this_url to my getCurrentURL()
	
	set theArchive to create record with {name:this_url, type:html, URL:this_url} ¬
		in incoming group
	try
		with timeout of 60 seconds
			set data of theArchive to download web archive from this_url
			try
				set this_source to source of theArchive
				if this_source is not missing value then
					set this_title to get title of this_source
					if this_title is not missing value and this_title is not "" then
						set the name of theArchive to this_title
					end if
				end if
			end try
		end timeout
		
		tell application "GrowlHelperApp"
			notify with name "Add webarchive" title this_title ¬
				description "Added to DEVONthink Pro" application name "WebImporter"
		end tell
	on error msg
		tell application "GrowlHelperApp"
			notify with name "Add webarchive" title this_title description msg ¬
				application name "WebImporter"
		end tell
	end try
end tell

Hmmm…doesn’t seem to be working for me in Firefox. Copied and pasted your code into Apple Script Editor, saved it to my User Scripts Folder, but when I invoke it using the drop-down script menu in my menu bar (on the right), nothing happens. I must be doing something wrong? Hope I can get it to work as this script is exactly what I’ve been looking for…

After the “set this_url” line, add this:


display dialog this_url

To see if at least it’s getting the URL from Firefox.

John

Hi John,

Hmmm… still doesn’t seem to work for some reason…

Just to be sure, here’s the code I’m using:

tell application "GrowlHelperApp"
	-- Make a list of all the notification types
	-- that this script will ever send:
	set the allNotificationsList to ¬
		{"Add webarchive"}
	
	-- Make a list of the notifications
	-- that will be enabled by default.     
	-- Those not enabled by default can be enabled later
	-- in the 'Applications' tab of the growl prefpane.
	set the enabledNotificationsList to ¬
		{"Add webarchive"}
	
	-- Register our script with growl.
	-- You can optionally (as here) set a default icon
	-- for this script's notifications.
	register as application ¬
		"WebImporter" all notifications allNotificationsList ¬
		default notifications enabledNotificationsList ¬
		icon of application "DEVONthink Pro"
end tell

on getCurrentURL()
	tell application "System Events"
		set myApp to name of first application process whose frontmost is true
		if myApp is "Camino" then
			-- tell application "Camino"
			-- URL of window 1
			-- end tell
		else if myApp is "firefox-bin" then
			tell application "Firefox"
				«class curl» of window 1
			end tell
		else if myApp is "Safari" then
			tell application "Safari"
				URL of document 1
			end tell
		else if myApp is "Opera" then
			-- tell application "Opera"
			-- set myInfo to GetWindowInfo of window 1
			-- item 1 of myInfo
			-- end tell
		else
			return
		end if
	end tell
end getCurrentURL

tell application "DEVONthink Pro"
	set this_url to my getCurrentURL()
	display dialog this_url
	set theArchive to create record with {name:this_url, type:html, URL:this_url} ¬
		in incoming group
	try
		with timeout of 60 seconds
			set data of theArchive to download web archive from this_url
			try
				set this_source to source of theArchive
				if this_source is not missing value then
					set this_title to get title of this_source
					if this_title is not missing value and this_title is not "" then
						set the name of theArchive to this_title
					end if
				end if
			end try
		end timeout
		
		tell application "GrowlHelperApp"
			notify with name "Add webarchive" title this_title ¬
				description "Added to DEVONthink Pro" application name "WebImporter"
		end tell
	on error msg
		tell application "GrowlHelperApp"
			notify with name "Add webarchive" title this_title description msg ¬
				application name "WebImporter"
		end tell
	end try
end tell

It sounds like your script isn’t being invoked at all.

Try putting this at various points throughout the script, just to verify it’s running:


display dialog "Got here!"

John

OK, it’s seizing just before:

tell application "DEVONthink Pro"
   set this_url to my getCurrentURL()
   display dialog this_url
   set theArchive to create record with {name:this_url, type:html, URL:this_url} ¬
      in incoming group
   try
      with timeout of 60 seconds
         set data of theArchive to download web archive from this_url
         try
            set this_source to source of theArchive
            if this_source is not missing value then
               set this_title to get title of this_source
               if this_title is not missing value and this_title is not "" then
                  set the name of theArchive to this_title
               end if
            end if
         end try
      end timeout

If i put the “got here!” command immediately after this, i don’t see it. If I put “got here!” immediately before this section, I do see it.

Cheers for your help -

David

Are you not using DEVONthink Pro, but rather Personal or Pro Office? You may need to change what’s in the “tell application” line…

John

Using Pro Office. Tried to change, but no luck. “Got here” still in same place as previous post and not getting that far.

Thanks anyway, John. Don’t want to take up any more of your time, but appreciate the help thus far.

This is a great script.
The use of growl is inspired as is the FastScripts suggestion.

In order to allow saving to the current rather than the new notes group change last line in


tell application "DEVONthink Pro" 
   set this_url to my getCurrentURL() 
    
   set theArchive to create record with {name:this_url, type:html, URL:this_url} ¬ 
      in incoming group 

to


tell application "DEVONthink Pro" 
   set this_url to my getCurrentURL() 
    
   set theArchive to create record with {name:this_url, type:html, URL:this_url} ¬ 
      in current group 

The web archive should appear in the current group.

I use both this modified and the original script bound to Cmd-I and Cmd-opt-I. In this way I can send either to the new notes or current group.

Note that this script does not switch the app context bringing DT to the fore like some scripts tend to. I’m not sure if this is a consequence of the FastScripts context switching detection abilities or not.

It would be good to be able to trigger the script with the mouse off the menu or toolbars. Any suggestions on this?

Jonathan

Hello,

I receive the following message when using this script:

“Error number: -1728
Message: DEVONthink Pro got an error: Can’t get incoming group.”

Any suggestions?

Thanks,
Jim

Hi Jim

Try experimenting with the setting for New notes in Preferences - Import (it’s at the very bottom of the sheet).

I think this setting directs DevonThink to use this group for incoming notes.

Jonathan

Hi Jonathan,

Thanks for the help! That solved the problem.

Jim

Forgive the newbie question, but what would this look like with a line “uncommented”?

I cannot get this to work with either Firefox or Omniweb (haven’t tried another browser). Looks like I’m having the “same issue” as a previous poster.
I don’t see anything being invoked in the console.

thanks

:slight_smile:

A commented line in AppleScript begins with –
Therefore, to activate the code for camino and opera the code segment becomes:


on getCurrentURL() 
   tell application "System Events" 
      set myApp to name of first application process whose frontmost is true 
      if myApp is "Camino" then 
          tell application "Camino"  
            URL of window 1 
          end tell 
      else if myApp is "firefox-bin" then 
         tell application "Firefox" 
            «class curl» of window 1 
         end tell 
      else if myApp is "Safari" then 
         tell application "Safari" 
            URL of document 1 
         end tell 
      else if myApp is "Opera" then 
          tell application "Opera"  
            set myInfo to GetWindowInfo of window 1 
            item 1 of myInfo
          end tell
      else 
         return 
      end if 
   end tell 
end getCurrentURL 

Jonathan

I think I know why the script wasn’t activating… :blush:

Thank you for the reply.

:slight_smile: