Script occasionally breaks?

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
chifutbol
Posts: 21
Joined: 01 Nov 2016, 09:24

Script occasionally breaks?

06 Apr 2017, 12:36

Hey everyone.

So to start off here is my script

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^v::
Sendinput, {ctrl+enter}%Clipboard%
return
return
I am using this to copy a column of values in excel, and paste them into a separate program (seems to be using an old version of excel, but I am also pasting into a column). When it works, I can see each individual entry being pasted into its own spot in the program. However I have been having issues where the script will mess up. Sometimes it will combine multiple cells in Excel and try to paste them into one cell in my program. Other times it seems to either skip or just give up halfway through pasting everything.

I use this script to paste anywhere from 20 to 1000 values at once. Sometimes when I restart my computer it seems to fix the issue for a little bit.

Anyone have any ideas how to fix this issue? One thing I was thinking was changing my code so that it copies and pastes each cell one by one, instead of copying the range of 600 values and pasting it all at once. This just seems like it would take forever. Thoughts?
(note: not sure if this would change anything, but I am copying from Excel in windows 7, and pasting into my program which runs on the virtual xp machine.)
User avatar
aztec3
Posts: 177
Joined: 07 Apr 2014, 12:05

Re: Script occasionally breaks?

06 Apr 2017, 13:57

First, there MIGHT be a problem with the amount of data copied into the clipboard, but I doubt that would cause the intermittent errors you encounter, but just a thought to begin with.

Here's a link to a question someone rose regarding this:
http://stackoverflow.com/questions/2236 ... tents-size

Second, when I ran your script (why two return lines???) it would skip a line when pasting.

Third, here is a simple re-write of your script...this will also copy over any highlighting of the cell too.

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^v::
Send, {ctrl}{enter}
return
Hope this is helpful, good luck.

UPDATE: To clarify, I assume you have already copied the range of cells you want to paste prior to invoking this script (so the cells data is in the clipboard). Running Win 7 Pro and using Excel 2016.
chifutbol
Posts: 21
Joined: 01 Nov 2016, 09:24

Re: Script occasionally breaks?

06 Apr 2017, 15:34

Hey Aztec,

I have wondered about copy size as well. I will add that while I may copy say 1000 cells in excel at once, each cell contains maximum 3 digits in it. I'm not sure how that converts to memory, but that means I'm copying maximum 3000 characters.
I can also confirm that I do successfully manage to run this script no problem with large clipboards being pasted. This error seems to happen randomly. And once it happens it won't fix itself unless I get lucky with a very small sample size (10 cells) or I restart my computer.

As for your concerns with what is in the script : This old program is a little stubborn. I need to hit (ctrl+enter) to access the cell, and I can't use a normal paste command like (ctrl+v). In order to paste, I would need to right click, and left click the option "paste." %Clipboard% seems to perfectly paste inside my cell. Finally, the double return: The first return is to accept the entry, and the second return is to jump down to the next cell. And when the script works like it should, it goes through that exact process again and goes through every bit of data I had copied.

Finally - I actually usually leave this script running for most of the day. I just tried copying excel data with script off, turning script on, and then pasting into my program. Still go the same problem :( (for the record, Win 7 Pro, Excel 2013.)
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Script occasionally breaks?

06 Apr 2017, 16:17

My experience with random but repeatable errors (i.e. it only sometimes goes wrong, but once it does it goes wrong every time) is that they are usually timing issues - something in your script is happening faster than the program can keep up with it in certain circumstances.

The first thing I would try is splitting it to paste one cell at a time - try having it happen as fast as possible, and if that doesn't work, add sleeps. Try doing 25 or 50 cells at a time with no sleep until you find a nice balance between the amount of sleep needed for the script to not break and having it go by as fast as possible. You also might be surprised how little sleep is needed; just a quick Sleep 50 can make the difference between something working or not.
chifutbol
Posts: 21
Joined: 01 Nov 2016, 09:24

Re: Script occasionally breaks?

06 Apr 2017, 16:22

@MaxAstro

This was actually my initial thought. It seems like the script is trying to paste the next data value before it has even left the cell.

I've actually tried messing around with that. Not sure if I did it right...

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^v::
Sendinput, {ctrl+enter}%Clipboard%
return
Sleep 8000 ; ms
return
I've put in different numbers, but I put in 8000 because I never saw any difference in the script. Do I need to adjust something?
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Script occasionally breaks?

06 Apr 2017, 16:40

You seem to be misunderstanding how a return statement works; return tells the script to stop doing anything, so the Sleep 8000 never gets used.

In order to split up the cells you will have to manually split the contents of the clipboard across multiple send commands.

THAT SAID, before you get into all of that I would also try SendPlay and see if it is more reliable. It's a bit slower, but my experience with Adobe Acrobat is that SendPlay never moves on until it has finished sending.

Another thing you could try is using ^v instead of sending the contents of the clipboard directly.
chifutbol
Posts: 21
Joined: 01 Nov 2016, 09:24

Re: Script occasionally breaks?

06 Apr 2017, 16:58

Misunderstanding is quite possible! I got help from this website years ago and had that script written for me.

I don't really understand a whole lot about scripting.

This is what I need to do to manually paste data:
(Ctrl+Enter)
Right click, select paste option (substituted in script with %Clipboard%
Enter
Enter

I was going to use mouse tracker to right click and paste, because ^v doesn't work. For some reason when I use %Clipboard% it does work. No idea why, I just rolled with it.

So how do I need to change my current script to Enter (hitting the actual enter button on my keyboard)?
User avatar
Exaskryz
Posts: 2882
Joined: 17 Oct 2015, 20:28

Re: Script occasionally breaks?

06 Apr 2017, 17:00

So how do I need to change my current script to Enter (hitting the actual enter button on my keyboard)?
Send {Enter}
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Script occasionally breaks?

06 Apr 2017, 17:13

Sending Enter won't work because the way you have it set up right now, the entire contents of the clipboard are sent at once. If ^v doesn't work, you are probably going to have to split the clipboard, which is a huge pain.

Definitely see if using SendPlay instead of SendInput works, first.
chifutbol
Posts: 21
Joined: 01 Nov 2016, 09:24

Re: Script occasionally breaks?

07 Apr 2017, 15:22

I'm not sure what you mean try SendPlay instead of SendInput :(

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode  [color=#4040FF]Play [/color]  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^v::
Sendinput, {ctrl+enter}%Clipboard%
return
Sleep 8000 ; ms
return

Code: Select all

#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn  ; Enable warnings to assist with detecting common errors.
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

^v::
[color=#4040FF]SendPlay[/color], {ctrl+enter}%Clipboard%
return
Sleep 8000 ; ms
return
I tried changing both of those and neither worked.

Any other ideas?

Would one way to do this be to just copy cell -> alt tab -> paste -> alt tab -> move down 1 cell -> copy -> loop
MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Script occasionally breaks?

07 Apr 2017, 15:29

I meant the second one, using SendPlay, {ctrl+enter}%Clipboard%. If that didn't work then that's probably it for simple solutions.

You could do it the way you suggest, although that is likely to be VERY slow. Hm... Here, do this: Change your script to just Send, %clipboard%, and then copy ~20 cells, go into notepad, and activate your hotkey.

Whatever shows up in Notepad, post it here exactly as it appears. I might be able to figure out a way to parse the clipboard...
chifutbol
Posts: 21
Joined: 01 Nov 2016, 09:24

Re: Script occasionally breaks?

07 Apr 2017, 15:38

Hmm... I tried doing

Code: Select all

Send, %clipboard%

Code: Select all

SendPlay, {ctrl+center}%clipboard%

Code: Select all

SendInput, {ctrl+enter}%clipboard%
The only one of those that worked was

Code: Select all

SendInput, {ctrl+enter}%clipboard%
When that was executed, the 20 cells copied looked like this :

Code: Select all

15

33

7

222

222

29

222

222

222

21

33

10

222

41

37

222

222

18

222

45

222

MaxAstro
Posts: 557
Joined: 05 Oct 2016, 13:00

Re: Script occasionally breaks?

07 Apr 2017, 15:52

Okay... as I thought it's storing it in plain text with fairly simple formatting. Now the real question is will Excel handle it the other way around? Basically, if you copy that info from Notepad and paste it into Excel, does it paste correctly or does it try to do something weird like putting it all in one cell?
chifutbol
Posts: 21
Joined: 01 Nov 2016, 09:24

Re: Script occasionally breaks?

07 Apr 2017, 16:01

All right so, when I copy what I pasted into notepad, and then using the script :

In excel 2013, Paste in cell 1, skip 3 lines, paste in cell 4, and proceeds to finish everything copied.

In my program, it pastes in cell 1, skips 2 lines, paste in cell 3, and proceeds to finish everything copied.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: No registered users and 277 guests