save PDF from Safari to DT Pro

I haven’t found an easy way to import a PDF doc being displayed with Apple’s PDF support in Safari into DTP, so I wrote this script.

Perhaps other people will find it useful…

Or perhaps suggest improvements…


try
	-- Get information about the PDF to save
	tell application "Safari"
		if not (exists document 1) then error "No document is open."
		set v_doc to document of front window
		set v_name to name of v_doc
		set v_url to URL of v_doc
		set AppleScript's text item delimiters to "/"
		set v_filename to the last text item of v_url
		set AppleScript's text item delimiters to ""
	end tell
	
	-- Create the temporary file name for Safari's "Save As ..."
	if v_filename does not end with ".pdf" then
		set v_filename to "dtimport.pdf"
	end if
	set v_tmpfile to ((path to temporary items as string) & v_filename)
	set v_hack to (v_tmpfile & ".download")
	
	-- Make sure we have a clean slate
	tell application "Finder"
		if (file v_tmpfile exists) then
			delete file v_tmpfile
		end if
		if (file v_hack exists) then
			delete file v_hack
		end if
	end tell
	
	-- Get a copy of the doc without downloading it again
	tell application "Safari"
		save v_doc in file v_tmpfile
	end tell
	
	-- Cleanup after Safari
	tell application "Finder"
		if (file v_hack exists) then
			set the name of file v_hack to v_filename
		end if
	end tell
	
	-- Import the file into DTP
	tell application "DEVONthink Pro"
		activate
		set v_dest to display group selector "Destination" buttons {"Cancel", "OK"}
		set v_rec to import v_tmpfile to v_dest type PDF without converting and unstyled
		set the URL of v_rec to v_url
		--set the name of v_rec to v_name
	end tell
	
on error e_msg number e_num
	display dialog e_msg buttons {"OK"} default button 1
end try

You could also create a smart folder that would import content into DT as it was added and save it there.

I’ve been saving pdfs in the Finder and batch importing them.

Thanks for the script. I’ll check it out.

:slight_smile:

This script worked well for me for a time. Now I get this error message when attempting to use it to capture a pdf loaded in Safari:

The variable v_rec is not defined.

This message appears when a pdf is loaded or if I test the script without a pdf loaded (plain HTML page loaded in Safari).

Any ideas?

tx

The two lines that define and then use v_rec are:


      set v_rec to import v_tmpfile to v_dest type PDF without converting and unstyled 
      set the URL of v_rec to v_url

I think the import into DTP is failing so the first line above never defines v_rec, and the second line fails. Why the import doesn’t trigger the ‘try…error’ exception catch, I don’t know.

Did the DTP Window->Log include any errors?

Do the OSX log files [console.log, system.log, or com.devon-technologies.thinkpro.log] contain anything useful?

I’m not at all applescript savvy. Does anyone have a suggestion for this one?

cheers

:slight_smile:

This is working again, with a recent DTPro version update (prior to the 10-27-05 update). I haven’t tried today’s update yet.

Does this mean it must be DTPro?

If the only thing that changed was DTPro, then yes, most likely it was DTPro.

DTPro 1.1B3 has broken this script again.

Does anyone at DevonTech know why this breaks from time to time?

Great script BTW…

thanks

Do you have an example for me? Maybe the filename of the PDF contains URL escapes and/or is not a valid HFS+ filename.

Christian,

I’ll try a few random pdf’s today prior to upgrading to the latest beta.

In the past, when it worked on one pdf, it worked on every one I tried to save. When it didn’t work on a single pdf, it never worked on any.

I’ll post back with more info.

thanks!
:slight_smile: