Bug with 'Copy clipboard to variable'???

G’day.

The following seems to pick up a bug in Applescript.

I’m trying to save & restore the clipboard, while using the clipboard to paste & print a mail message.

Doesn’t matter if I use

copy (the clipboard) to ClipBoardStore
or
Set the ClipBoardStore to the clipboard

The line ‘set the clipboard to the theEmail’ does not set the clipboard, but instead the clipboard retains it’s previous info.

EDIT I should have added that most of the time the routine works, but sometimes doesn’t

try copying something to the clipboard, and running the script twice or more.

Any thoughts on what to do please?

Here’s a condensed version.



property DesiredPrinter : "Stylus Photo R390"

set theEmailline to "This is a printing test" & return & "We're testing the output of printer '" & DesiredPrinter & "'" & return
set theEmail to theEmailline

repeat with x from 1 to 8
	set theEmail to theEmail & theEmailline
end repeat

my PrintTheDarnThing(theEmail)

on PrintTheDarnThing(theEmail)
	copy (the clipboard) to ClipBoardStore
	set the clipboard to the theEmail
	tell application "TextEdit"
		activate
		try
			if not (exists document 1) then make new document at front
			tell application "System Events" to tell process "TextEdit"
				keystroke "a" using command down
				keystroke "v" using command down
			end tell
			set the clipboard to ClipBoardStore
		end try
	end tell
end PrintTheDarnThing


Model: G5 1.8 GHz
AppleScript: 2.1.1
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

set the clipboard to "AppleScript Works"
set TheClip to the clipboard
if the class of TheClip as string is "String" then say TheClip

look in the dictionary standard addition you will find the clipboard commands
there you will see "set the clipboard to"must be used
get with “the clipboard” object
getting the class may help with what to do next, if you don’t already know.

G’day Bevos, thanks for trying, but as I already said, using ‘Set variable to the clipboard’ sometimes doesn’t work either.
You might have to run the script a few times, but eventuaaly it won’t work. No rhyme or reason.


property DesiredPrinter : "Stylus Photo R390"

set theEmailline to "This is a printing test" & return & "We're testing the output of printer '" & DesiredPrinter & "'" & return
set theEmail to theEmailline

repeat with x from 1 to 8
	set theEmail to theEmail & theEmailline
end repeat

my PrintTheDarnThing(theEmail)

on PrintTheDarnThing(theEmail)
	set ClipBoardStore to the clipboard
	set the clipboard to the theEmail
	tell application "TextEdit"
		activate
		try
			if not (exists document 1) then make new document at front
			tell application "System Events" to tell process "TextEdit"
				keystroke "a" using command down
				keystroke "v" using command down
			end tell
			set the clipboard to ClipBoardStore
		end try
	end tell
end PrintTheDarnThing


Hi Santa,

your clipboard scripting works flawlessly here,
but why do you use the clipboard and GUI scripting anyway?
It’s much easier to paste the text directly

on PrintTheDarnThing(theEmail)
	tell application "TextEdit"
		activate
		try
			if not (exists document 1) then make new document at front
			set text of document 1 to theEmail
		end try
	end tell
end PrintTheDarnThing

G’day Stefan

Once again you’ve come to my rescue, and taught me something new.

Thank you very much! :slight_smile:

Regards

Santa

Stefan’s approach is obviously the right one, but as far as the clipboard commands are concerned, bevos’s observation that they’re the multi-word commands set the clipboard to and the clipboard shouldn’t be forgotten.

It used also to be the case that whatever application wanted to access the clipboard had to be frontmost, but that doesn’t usually seem to be necessary nowadays.

I think the fact that Santa’s original script sometimes works and sometimes doesn’t (even with the correct commands) is due to timing. With GUI Scripting, the script only hangs around long enough for System Events to acknowledge carrying out the commands it receives. In this case, the commands are simply to put the keystrokes Command-“a” and Command-“v” into the keyboard buffer. What happens as a result of those keystrokes doesn’t affect System Events or, therefore, the script. The script’s probably going ahead and restoring the old clipboard value before TextEdit’s finished reacting to the keystrokes.

The StandardAdditions’ clipboard commands are originally from Jon˜s Commands. The story goes that Apple gave Jon Pugh a new computer in exchange for permission to include them in its own OSAX. :slight_smile:

This is a good point. GUI scripting often requires judicious use of the “delay” command to make the script pause at strategic spots long enough for the displayed GUI elements to catch-up. Depending on the speed of the system the script is running on, this can be as little as “delay 0.3” and as much as “delay 1.” I’ve even had to go as high as “delay 3” on occassion.

Any time I get a GUI script that “sometimes works, sometimes doesn’t” I start putting “delay delayVar” in key spots. Then I up the value of “delayVar” until 100% consistancy is acheived, then I start removing the delays one by one and retesting…usually the culprit is only one or two needed pauses in the end.

And yes, I have had to go so far as to put a delay after every single GUI command, get it working, then slowly remove them. It’s a necessary evil IMHO.

Hello

Two details bother me in the original code.

  • 1 - in “set the clipboard to the theEmail” it seems that there is an extraneous “the”.
    I would have coded:
    set the clipboard to theEmail

  • 2 - I would have reset the original clipboard after the “end try” instruction.

on PrintTheDarnThing(theEmail)
   copy (the clipboard) to ClipBoardStore
   set the clipboard to theEmail -- EDITED
   tell application "TextEdit"
       activate
       try
           if not (exists document 1) then make new document at front
           tell application "System Events" to tell process "TextEdit"
               keystroke "a" using command down
               keystroke "v" using command down
           end tell
       end try
           set the clipboard to ClipBoardStore -- MOVED
   end tell
end PrintTheDarnThing

Yvan KOENIG (from FRANCE samedi 5 mai 2007 20:25:36)

Excellent, thanks… a few years later the clipboard advice still helped me.
I’m in 10.5 and this works well, except “the clipboard” has a string type of “text” rather than “String” for me…

set the clipboard to “AppleScript Works”
set TheClip to the clipboard
if the class of TheClip as string is “text” then say TheClip --(class apparently is “String” in 10.4)

Sorry, don’t remember for sure how Tiger worked, but

in 10.6 “the clipboard” returns the text with class “text” if both text and something else is on the clipboard – like if you copy an item in the finder that has text (name) and an icon, (image).

If only an image is on the clipboard, it returns the image or a list with images in it {tiff,quickdraw}, class “record”

Browser: Firefox 3.6.3
Operating System: Mac OS X (10.6)

Just for the record, if you want ONLY text on the clipboard, you can use the command line for this:

do shell script "echo " & quoted form of myString & " | pbcopy"

Or to get the contents:

do shell script "pbpaste"

Again, this returns ONLY text, no formatting, no images, nothing but raw text. Of course, getting raw text can be useful sometimes, too. :wink:

-SuperScripter