Upload Currently Selected Item

Hi ya’ll.

So, I want to be able to select an item in my database-- about any item, in theory (though that’s not perfect yet)-- and upload it to a temporary scratch place so I can “share” it with someone (via email, IM, IRC, whatever I’m using at the time)

These uploads are meant to be “temporary”, and the name random on purpose-- I use it sort of like I sometimes use tinypic.com, to upload a current version of image I’m working on in photoshop and show someone to get their thoughts, while still working on it.

I have it basically working, but thought I’d share to see if anyone else thought of a better way to accomplish it-- I’m actually not that great with AppleScript :slight_smile:


on random_string()
	tell application "Finder"
		set x to {}
		repeat (random number from 8 to 10) times
			set end of x to some item of "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
		end repeat
	end tell
	tell application "DEVONthink Pro" to activate
	return x as text
end random_string

tell application "DEVONthink Pro"
	activate
	if not (exists current database) then error "No database is open."
	set thisDatabase to the current database
	
	set theSelection to the selection
	if (count of theSelection) < 1 then error "Please select one or more items."
	
	repeat with current in theSelection
		set currentType to type of current
		set currentName to name of current
		
		if currentType is html then
			set metaType to html
		else if currentType is rtf then
			set metaType to html
		else if currentType is rtfd then
			set metaType to html
		else if currentType is txt then
			set metaType to txt
		else if currentType is picture then
			set metaType to picture
		else
			set metaType to "error"
		end if
		
		if metaType = "error" then
			display dialog "I don't know how to handle the kind of data '" & currentName & "' is." buttons {"OK"}
		else
			set randomString to my random_string()
			set exportFolder to "/tmp/" & "DX-" & randomString
			
			set exportedFilename to export record current to exportFolder as metaType
			
			if metaType is html then
				set theUploadFilename to "/tmp/DX-" & randomString & "/" & randomString & ".html"
				
				if currentType is not html then
					-- Workaround for "as metaType" not being supported yet.
					set conversionCommand to "textutil -convert html \"" & exportedFilename & "\" -output " & theUploadFilename
					
					set result to do shell script conversionCommand
				end if
			else
				tell application "Finder"
					set the clipboard to exportedFilename
					set myfile to POSIX file exportedFilename as alias
					set oldext to name extension of myfile
					
					set the name of myfile to randomString & "." & oldext
					
					set theUploadFilename to "/tmp/DX-" & randomString & "/" & name of myfile
				end tell
			end if
			
			tell application "Transmit"
				make new document at before front document
				tell current session of document 1
					connect to favorite with name "My iDisk Public Folder"
					upload item POSIX file theUploadFilename as alias with resume mode replace
					quit
				end tell
				quit
			end tell
			
			display dialog "Upload complete." buttons {"OK"}
			
			set uploadFile to POSIX file theUploadFilename as alias
			
			set the clipboard to "http://web.mac.com/ixokai/Scratch/" & name of (info for uploadFile)
		end if
		
	end repeat
end tell