Repeating applescript crashes

Hi, I have developed a script that sets the clipboard and pastes the result with keystrokes. I did it like this because its the fastest way, I have not added any delays in any part of the script, the script works fine but once it get over 60 repeats it simply stops doing the task in hand, the script appears to continue running but its not true I have to force quit applescript in order to stop the script witch is not doing anything.

here is a copy of the script:


repeat 10000 times
	set the clipboard to bigcode
	tell application "System Events"
		keystroke "v" using command down
		keystroke return
	end tell
end repeat

I did this without using the clipboard and it worked but its half the speed. Any idea to fix this will help.

Are you sure that the clipboard is correctly filled when the script is asked to paste ?
Are you sure that the application in which you paste is ready to receive the new paste command when you ask it to do ?

When I work with Pages or Numbers, I must add some trickery in my scripts because pasting a large set of data may take a lot of time and the script is unaware of that.

I must add some trickery too when I fill the clipboard thru Copy but at first look it doesn’t apply to your example.

I’m just writing about that because I imagine that your posted script is just a quick and dirty one which may be written to ask about a slightly different problem.
At first read it seems really odd to fill 10000 times the clipboard with the same value.

set the clipboard to bigcode
repeat 10000 times
   tell application "System Events"
       keystroke "v" using command down
       keystroke return
   end tell
end repeat

would be more logical.

Try to run it.
Maybe your problem will disappear. If it does, we may assume that it’s filling the clipboard many times which is the wrongdoer. I may imagine that the system has a lot of tasks to execute when we fill the clipboard and that it is fooled when it’s urged to do that 10000 times in a continuous flow.

Yvan KOENIG (VALLAURIS, France) jeudi 26 mars 2015 10:03:53

Hi.

Another factor in this case is likely to be the sheer number of keystrokes being thrown into the keyboard buffer all at once.

Thank you for your feed back, the code set to the clipboard is plain text (alphabet and numbers) not more than 40 characters, They change in every repeat thats why the set clipboard needs to bee inside the repeat command. I added an if inside to delay the script every 50 repetitions but it still crashes :frowning:

is there a way of flushing the AppleScript cache? I think the problem is there.

Hi, I set the clipboard to 1 and it still crashes so its no the amount of text thrown into the keyboard buffer all at once.

Here is the code I added to it to give the script a chance to rest.

	set X to i as text
	if X contains "50" then
		tell application "Finder"
			activate
		end tell
		set the clipboard to ""
		delay 3
	else
		if X contains "00" then
			tell application "Finder"
				activate
			end tell
			set the clipboard to ""
			delay 3
		end if
	end if

but it still crashes! :frowning:

I made several experiments, The clipboard is definitely the factor. its the only thing that makes my script crash. :frowning:

Hi,

Note that the clipboard is not always at it seems. It’s not just text that you copy, but a record I think!!! If I’m wrong, then disregard. Hope this is helpful.

gl,
kel

Thank you for replaying. nop simple text generated within the applescript, no spaces no tabs nothing.-- just some dashes!

Hi Brian,

But, when you ‘set the clipboard’ through AppleScript and when you copy, those can be different. When you copy, it might be a record while setting it through AppleScript might be just text. If I’m way off on this then disregard. Just looking at maybes.

Edited: and ps. I’ve often tested with situations and mixed them up. Like If I originally wanted to copy something, but later on used something else to set the clipboard, then there is a big difference. If that’s not the case then disregard.

gl,
kel

Hi, I think I got it working. I added a tell finder and set the clipboard as text and it worked:


	try
		tell application "Finder"
			set the clipboard to serial as text
		end tell
	on error
		log X
		say "error"
		display dialog "paste error"
		delay 15
	end try

…so setting the clipboard from applescript its different in some way!

Thank you all for your help!

:smiley:

Hi Brian,

I forgot about this but, glad you got it working and also if you want to see what is actually on the clipboard is try to get clipboard info. Gotta go.

gl,
kel

The useful change is to coerce the data to string before passing it to the clipboard.

I ran this script :

set bigCode to 0

repeat 10000 times
	set bigCode to bigCode + 1
	set the clipboard to bigCode as text
	tell application "System Events"
		keystroke "v" using {command down}
		keystroke return
	end tell
end repeat

and it pasted 10000 numerical strings in the Scripts Editor window.

Yvan KOENIG (VALLAURIS, France) jeudi 26 mars 2015 22:45:33