English is not my native language, so the explanation may be insufficient.
Before the question, let’s write down the current problem code.
The following is not an actual example, but a scenario created by exaggerating.
repeat
...
set the clipboard to mystr
delay ??
keystroke "v" using {command down}
delay ??
set the clipboard to mystr
delay ??
keystroke "v" using {command down}
delay ??
set the clipboard to mystr
delay ??
keystroke "v" using {command down}
delay ??
set the clipboard to mystr
delay ??
keystroke "v" using {command down}
delay ??
set the clipboard to mystr
delay ??
keystroke "v" using {command down}
delay ??
...
end repeat
If it is said that repeated copy paste operation is necessary due to some necessity, theoretically Is the time delay necessary between copying and pasting by keystroke and also pasting and copying?
If it is necessary, by what criteria should the time delay value be determined?
I wonder if there is any formula
When you use command set the clipboard to and then paste using keystroke, the delay implements your script itself, because the AppleScript waits for respond from command set the clipboard to. So no need to add additional delay. More important is to be the destination window frontmost:
tell application "TextEdit"
make new document
activate
end tell
set the clipboard to "mystr"
tell application "System Events" to keystroke "v" using {command down}
Now other example:
Make new window in the Script Debugger (which runs the given script) and paste:
tell application "Script Debugger"
make new document
activate
end tell
set the clipboard to "mystr"
tell application "System Events" to keystroke "v" using {command down}
This way you can manage custom log window when run your scripts, save it to read later and so on.
The limitation is one: you can’t paste to window of running script.
repeat
my typeInNormalText(theStr)
end repeat
on typeInNormalText(theStr)
tell application "System Events"
tell process "Pages"
set the clipboard to theStr …………………………..(A)
keystroke "v" using {command down} …….…….(B)
delay 0.3 <———— According to you, It is not necessary? ……………(C)
end tell
end tell
end typeInNormalText
Suppose (C) is omitted
Then, since the typeInNormalText() function is repeated intensively in the loop statement, may there be a situation where (B) is not executed occasionally?
Depending on the speed of computer, may or may not cause the problem?
Yes, after keystroke depending on the speed of computer, how many apps is running and so on, may cause the problem.
This is the reason to not use the keystroke “v” when possible. With Pages for example, it would be better to set value of the cell directly to the clipboard.
NOTE: when using keystroke it is always correct telling to “System Events” and not to application process, or to its window. This enhances the speed as well:
repeat
my typeInNormalText(theStr)
end repeat
on typeInNormalText(theStr)
set the clipboard to theStr
tell application "System Events"
tell process "Pages"
set theWindow to window 1
end tell
keystroke "v" using {command down} -- telling to System Events
end tell
delay 0.3 -- criteria may be only one: the text of cell became equal to text of the clipboard
-- or if the old text value of destination cell changed from "" to some other text
-- In any way, you should check what happens with value of destination object
end typeInNormalText