A cross-platform text Clipboard using Dropbox

Jun 30, '11 07:30:00AM

Contributed by: atnbueno

This hint describes how to set up a cross-platform text-only clipboard using Dropbox.

I've found myself frequently having a piece of text in one computer and wanting to have it in another one. I use Dropbox all the time, so the 'easiest' was to save the text to a text file in the Dropbox folder and open it in the other computer. Hey, it's like a Dropbox Clipboard!

See, the thing I haven't mentioned is that the two machines in question are a MacBook and a Windows 7 box, so that leaves out any Mac- or Windows-only solution. I also want something simple (so Synergy is out too) and tweakable. Full clipboard support is not simple, but fortunately I just need plain text and AppleScript (for OS X) and AutoHotkey (for Windows) can do everything I'll need, so let's code a bit.

One of AutoHotkey's strengths is its complete keyboard control so it can do it all in one punch:

;
; AutoHotkey Version: AutoHotkey_L 1.1 (Unicode)
; Language:           English
; Platform:           Win7 SP1
; Author:             Antonio Bueno 
; Last Mod:           2011-06-19
;
; Script Function:
;  - Copies (and cuts) to and pastes from a file in Dropbox, using Shift+Ctrl+C, X and V
;  - All the operations are in plain text (and encoded using UTF-8)
;  - The main objective is to get a plain-text clipboard across several computers
;  - As a side effect it works as a system-wide "paste without format" function
;  - In Windows Explorer, copying files results in a filelist with full paths

#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases
SendMode Input ; Recommended for new scripts due to its superior speed and reliability
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory

Menu, Tray, Icon, %A_WinDir%\system32\shell32.dll, 217 ; Shows a clipboard icon in the system tray
; NOTE: The clipboard icon index is different for each Windows version

FileEncoding, UTF-8-RAW ; Text file operations use UTF-8 encoding without BOM

; Hardcoded location of Dropbox folder (proper method requires sqlite3 queries or base64 decoding)
DropboxFolder = %A_MyDocuments%\My Dropbox

; Shift+Ctrl+c copies (and Shift+Ctrl+x cuts) to DropboxClipboard.txt
+^c::
+^x::
  FileDelete, %DropboxFolder%\DropboxClipboard.txt
  ClipBoard =
  StringRight CopyOrCut, A_ThisHotKey, 1
  Send ^%CopyOrCut%
  ClipWait 2
  If !ErrorLevel
    FileAppend, %ClipBoard%, %DropboxFolder%\DropboxClipboard.txt
Return

; Shift+Ctrl+v pastes from DropboxClipboard.txt
+^v::
  IfExist, %DropboxFolder%\DropboxClipboard.txt
    FileRead, ClipBoard, %DropboxFolder%\DropboxClipboard.txt
  Else
    ClipBoard =
  Send ^v
  Sleep 50
Return
As the comments in the script mention, not only do I get a clipboard across all my Dropbox-linked computers, but I also get a system-wide 'paste without format' function.

NOTE: The script above requires a Unicode build of AutoHotkey_L. If you don't want to install it just to test this, just download the binaries and drop the script on the .exe.

AppleScript has no problems with the clipboard, but does not handle the keyboard shortcuts. I solved it with FastScripts, associating the keys Shift+Command+C, Shift+Command+X, and Shift+Command+V to these three scripts below.

Copy:
-- Hardcoded location of Dropbox folder (proper method requires sqlite3 or base64 decoding)
set DropboxFolder to ((path to home folder) as string) & "Dropbox"
set DropboxClipboard to DropboxFolder & ":DropboxClipboard.txt" as string

tell application "System Events"
  keystroke "c" using command down
end tell
try
  set outFile to open for access DropboxClipboard with write permission
  set eof outFile to 0
  write (the clipboard as «class utf8») to outFile
  close access outFile
on error
  close access outFile
end try
Cut:
-- Hardcoded location of Dropbox folder (proper method requires sqlite3 or base64 decoding)
set DropboxFolder to ((path to home folder) as string) & "Dropbox"
set DropboxClipboard to DropboxFolder & ":DropboxClipboard.txt" as string

tell application "System Events"
  keystroke "x" using command down
end tell
try
  set outFile to open for access DropboxClipboard with write permission
  set eof outFile to 0
  write (the clipboard as «class utf8») to outFile
  close access outFile
on error
  close access outFile
end try
Paste:
-- Hardcoded location of Dropbox folder (proper method requires sqlite3 or base64 decoding)
set DropboxFolder to ((path to home folder) as string) & "Dropbox"
set DropboxClipboard to DropboxFolder & ":DropboxClipboard.txt" as string

try
  set inFile to open for access DropboxClipboard
  set the clipboard to (read inFile)
  close access inFile
on error
  close access inFile
end try
tell application "System Events"
  keystroke "v" using command down
end tell
NOTE: For these scripts to work you probably need to 'Enable access for assistive devices' in the Universal Access System Preference pane.

This is a standalone and simplified version of the scripts I really use, for the sake of clarity. The ones I use get the Dropbox folder path from Dropbox's configuration files, encrypts and decrypts the text in the file, and do a couple of extra things with files inside the Dropbox folder, all this accompanied with notifications, so I know what's happening.

I hope someone else find this useful, and if there are better ways to do it, please tell about it in the comments.

P.S. I really, really would love to have something similar in my iPhone. Right now I get by giving DropboxClipboard.txt a star in the Dropbox app and using the system clipboard, but it's not the same.

[crarko adds: I haven't tested this one.]

Comments (11)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20110617041836290