Applescript: Read contents from text file, copy contents to clipboard

user2690304 picture user2690304 · Aug 16, 2013 · Viewed 8.3k times · Source

I am incredibly new to Applescript, first of all, so bear with me.

I currently have a plain-text file in a Dropbox folder. The file is set to be filled with text (and only text). I am trying to create an Automator workflow (or an Applescript script) that will read the contents from the text file, and write the contents to the clipboard. This way, I can copy text on my iOS devices, paste them into the plain-text file in Dropbox, and then use my mac (and this script), to paste the text to the mac. Basically, using Dropbox to sync clipboards between OS X and iOS.

I imagine the Applescript can't be more than a few lines, but I haven't found the proper way to write it. Any help is appreciated!

Answer

scohe001 picture scohe001 · Aug 16, 2013
set the_file to choose file
set the_text to (do shell script "cat " & quoted form of (POSIX path of the_file))
set the clipboard to the_text

or alternately using only AS:

set the_file to choose file
set the clipboard to (read the_file)

(This second piece of code throws an End of file error for me for certain files and I'm not sure why, so I'd use the first one)