Read text from txt file & copy it to the clipboard?

I’m trying to cobble together an applescript that reads a txt file, then copies the text to the clipboard.
I managed to come up with the script below, but it only seems to copy the first couple of words and then cuts off the end. The text files will usually only contain a single URL, but it cuts those short too.

tell application "Finder"
	set theFile to the selection as alias
	
	open for access theFile
	set fileContents to (read theFile)
	close access theFile
end tell

set the clipboard to fileContents

Anyone know why it’s only copying just the beginning?

Hi,

try this

try
	tell application "Finder" to set theFile to item 1 of (get selection)
	set the clipboard to (read (theFile as alias))
on error
	display dialog "Nothing selected" buttons {"Cancel"} default button "Cancel"
end try

Hello Stefan!

Now that, that is just so cute, that I have to snag it! :smiley:

Great, thanks a lot!

The problem of not all the text being copied appears to be a bug with Mountain Lion and selecting a file on the desktop, but I still appreciate the help cleaning up the script. Thanks again :slight_smile:

I don’t think so, this works in 10.8

tell application "Finder"
	set theFile to item 1 of (get selection)
	set fileRef to open for access (theFile as alias)
	set fileContents to (read fileRef)
	close access fileRef
end tell
set the clipboard to fileContents

Strange, that won’t compile in AppleScript Editor for me. I get;

“Syntax Error
An unknown token can’t go here.”

Compiles and runs fine with AppleScript Editor 2.5 and Script Debugger 5

Don’t copy/paste it from your browser into AppleScript Editor. Click the “Open this Scriplet in your Editor:” link.

Must be a problem at my end. I click the “Open this Scriplet in your Editor:” on my Lion and Mountain Lion machines and get the same problem.

Not to worry, I won’t be using this to click txt files on the desktop, and the original script works fine. Thanks.