Explorer copy to Dropbox via AutoHotKey

I’ve updated my dropbox library with the following function that copies a file or folder in windows explorer to your public directory. To use it, you’ll need AutoHotKey and a Dropbox account. For the previous post about this library, see Share current iTunes song with AutoHotKey and Dropbox. But first, usage:

Usage

Download the file to wherever you keep the autohotkey file you usually run. Somewhere at the top of your script, be sure to have the following line:

#Include dropbox.ahk

Somewhere else in your script, include the following:

#IfWinActive ahk_class CabinetWClass
	;; copy the current file to the dropbox
	^+d::
		CopySelectedFileOrFolderToDropbox()
	Return
#IfWinActive

The code for that function is after the break.

How it works

This works by opening the properties dialog of a folder and copying the information out of it, and running the FileCopy function to place a copy in your dropbox. I’ve moved the following out into their own functions, as both are used in this and the previous script:

GetDropboxFolderLocation()
{
	;; read a file in the appdata folder
	FileReadLine dropbox_b64, %A_AppData%\Dropbox\host.db, 2
	
	;; unencode the line
	return InvBase64(dropbox_b64)
}

;; generate url
GetDropboxPublicURL(user_id, filename)
{
	;; urlencode the filename
	file_url := uriEncode(filename)
	
	;; assemble the thing
	public_url = http://dl.dropbox.com/u/%user_id%/%file_url%
	
	return %public_url%
}

Here’s the function itself:

CopySelectedFileOrFolderToDropbox()
{
	;; get the location of the dropbox
	dropbox := GetDropboxFolderLocation()
	
	;; get the current file's location
	;; open the property dialog
	Send {AppsKey}r
	WinWaitActive ahk_class #32770
	DetectHiddenText, On
	;; get the text from it
	WinGetText, tVar, ahk_class #32770
	StringSplit tVar_arr, tVar, `n, `r
	;; close the property dialog
	Send {esc}
	
	;; we have to check if it's a folder
	IfInString tVar_arr5, File folder
	{
		;; parse the folder name together			
		foldername = %tVar_arr7%\%tVar_arr3%
		
		;; BE SURE TO INCLUDE THE DESTINATION FOLDER NAME
		;; if you don't, it'll just flatten the directory structure
		FileCopyDir %foldername%, %dropbox%\Public\%tVar_arr3%\
		TrayTip Dropbox, Copied %foldername% to %dropbox%\Public\%tVar_arr3%\
	}
	else
	{
		;; it's a file
		Send {esc}
		;; get the filename
		filename = %tVar_arr10%\%tVar_arr3%
		
		;; copy the file
		FileCopy %filename%, %dropbox%\Public\
		if ( ErrorLevel > 0 )
		{
			;; give up
			TrayTip Dropbox, Failed to copy %tVar_arr3% to %dropbox%\Public
			Return
		}
		
		;; get the public url
		user_id = 3771881			
		public_url := GetDropboxPublicURL(user_id, tVar_arr3)
	
		;; tell the user what we just did
		TrayTip Dropbox, Copied %filename% to %dropbox%\Public\%tVar_arr3%`n`nClipboard: %public_url%
		
		;; put the url on the clipboard
		clipboard = %url%
	}
}

Andrew Guyton
http://www.disavian.net/

Leave a Reply