saving the clipboard to a text file = empty file

hello
I did this to save the clipboard to a text file:

tell application "System Events"
	set theFile to "Users:LplChallenge.txt"
	set cl to (the clipboard as string)
	try
		open for access theFile with write permission
		set eof of theFile to 0
		write cl to theFile starting at eof as record
		close access theFile
		
	end try
end tell

I obtain a file (LplChallenge.txt) but empty…:frowning:
What is missing?
thanks!
jean-louis

Model: G5
AppleScript: 1.10.3
Browser: Safari 417.8
Operating System: Mac OS X (10.4)

To find out, put a way of seeing the error in your script:

tell application "System Events"
	set theFile to "Users:LplChallenge.txt"
	set cl to (the clipboard as string)
	try
		open for access theFile with write permission
		set eof of theFile to 0
		write cl to theFile starting at eof as record
		close access theFile
	on error theError
		close access theFile
		display dialog theError
	end try
end tell

What you’ll discover is that AppleScript can’t coerce the string in the clipboard to a record. You have to form a record before you write, or just write the string itself

thanks a lot for your response, but as my applescript is as bad as my english, I’m loosing ground…
Therefore I’m afraid to misunderstand “form a record before you write, or just write the string itself”…
And now let us see a donkey in action:
So, listening to your “form a record before you write”, I replaced my previous "set cl to (the clipboard as string)"with :
set cl to (text of ((the clipboard as string) as record)) as string

But nothing more…
Then, I tried to replace the clipboard by a simple string (following your “just write the string itself”):
set cl to “MypoorHead”
Thefile is empty and I obtain always an error with your very useful utility
As I need a quick solution can you unveil that little riddle ?
OOOOh! thank you…
jean-louis

Are you looking for something like this?

set theFile to file specification ((path to desktop as Unicode text) & "LplChallenge.txt")
set cl to the clipboard as record

try
	open for access theFile with write permission
	set eof of theFile to 0
	write cl to theFile starting at eof
	close access theFile
on error theError
	try
		close access theFile
	end try
	display dialog theError
end try

If you only want text, you could also use this:

try
	get POSIX path of ((path to desktop as Unicode text) & "test.txt")
	do shell script "/usr/bin/pbpaste > " & quoted form of result
on error errorMsg number errorNum
	display alert "Error " & errorNum message errorMsg buttons "Cancel" default button 1
end try

Thank you very much Bruce for the second one which saves my night(s)!
The first one put a strange mess into the text file ( a mixture of the clipboard and …I don’t know what)
Cheers!
jean-louis

The first one assumed that you wanted a record and produces a record file that is not generally very readable. In AppleScript a record looks like this:

set Person_1 to {FirstName:"Adam", SurName:"Bell"}
set Person_2 to {FirstName:"Bruce", SurName:"Phillips"}

-- FirstName by itself is not defined, but:

Person_1's SurName -- or SurName of Person_1
--> Bell

Person_2's FirstName -- or FirstName of Person_2
--> "Bruce"

thanks for the light!
In fact a record is the AppleScript equivalent of lists I use everyday with Lingo (macromedia director).
sincerely
jean-louis