Sunday, November 29, 2020

#1 2020-10-29 05:34:34 am

farmingmoney
Member
Registered: 2020-01-13
Posts: 15

Is the time delay necessary between copying and pasting by keystroke?

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.

Applescript:

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

Offline

 

#2 2020-10-29 06:56:24 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1444

Re: Is the time delay necessary between copying and pasting by keystroke?

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:

Applescript:


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:

Applescript:


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.

Last edited by KniazidisR (2020-10-29 07:21:50 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-10-29 07:21:33 am

farmingmoney
Member
Registered: 2020-01-13
Posts: 15

Re: Is the time delay necessary between copying and pasting by keystroke?

Thanks KniazidisR
I have read your answers well

Applescript:


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?

Offline

 

#4 2020-10-29 07:29:40 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1444

Re: Is the time delay necessary between copying and pasting by keystroke?

farmingmoney wrote:


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:

Applescript:


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

Last edited by KniazidisR (2020-10-29 10:20:04 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#5 2020-10-29 05:17:26 pm

farmingmoney
Member
Registered: 2020-01-13
Posts: 15

Re: Is the time delay necessary between copying and pasting by keystroke?

'Time delay is essential',
'The length of the required time delay should be determined by testing'
'It is better not to use keystroke'

Are the above three things you want to say?
Okay. Thanks KniazidisR

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)