Clipboard Stopped Working ... AHK Scripts Won't Run

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Clipboard Stopped Working ... AHK Scripts Won't Run

03 Apr 2024, 16:55

Virtually all of my AHK scripts center around writing to and reading from the clipboard. I use Ditto as my clipboard manager, and Windows 11 as my operating system.

I'm writing to see if anyone else in the AHK community is experiencing problems with their Windows 11 clipboard and - if so - how they've fixed it so they can keep using their AHK scripts.

Specifically, around 10 times a day, my clipboard suddenly stops writing new clips (both the basic Windows clipboard and Ditto clipboards). I created the following AHK script to fix corrupted files (the recommended fix on the Microsoft discussion groups):

Code: Select all

if not A_IsAdmin
{
   Run *RunAs "%A_ScriptFullPath%"  ; Requires v1.0.92.01+
ExitApp
}
Run cmd.exe /c DISM.exe /Online /Cleanup-image /Restorehealth,,show
Run cmd.exe /c sfc /scannow,,show
That occasionally works for a little while, but then the clipboard stops again after a short while.

I also have AHK scripts to close all unnecessary programs and processes, I run defragmentation programs, delete all of the clips in Ditto, etc. But nothing works. So I have to reboot several times a day.

This never happened with previous versions of Windows.

Does anyone have any ideas?
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

03 Apr 2024, 20:03

Hello,

I have not seen a clipboard issue.

Are you asking a question about Microsoft Windows, a question about an AutoHotkey script, a question about AutoHotkey, or none of these? What does "AHK scripts won't run" mean? What actually happens? What is the script that will not run?

To test the Windows clipboard, close Ditto, and then see if your clips are captured. I would test by copying and then pasting what you copied. Before your test, close all scripts, all keyboard utilities, and all clipboard managers.

Some things that could cause a clip to fail:
  1. Ctrl+C is remapped or reassigned by some software.
  2. Keyboard is broken.
  3. No selection has been made.
  4. The target program has blocked copying.
  5. The clipboard is cleared or reassigned before the clip can be used.
  6. The clipboard is used before it has time to copy the selection.
  7. Copying a selection was never attempted.
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

03 Apr 2024, 20:44

Hi Mikeyww,

I think I've solved the problem (although I'm still testing). I had 2 different AHK scripts with administrator privileges. I think that created some sort of conflict.

When I removed the administrator privileges from one of those scripts, it seemed to fix the clipboard issue.

I apologize for wasting your time!
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

06 Apr 2024, 00:46

Upon further testing, it appears at this point as if the problem was corrupted files. Chkdsk /r seems (so far) to have fixed the problem.
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

16 May 2024, 14:45

Nope, problem still occurring.
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

16 May 2024, 14:58

Although I asked four questions, you answered none of them. I also provided a specific suggestion for your approach to troubleshooting, but whether you tried that is unstated here. My earlier comments stand, so I have no additional comments at this time.
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

16 May 2024, 15:50

Mikeyww, I don't want to waste your time, so I will call Microsoft support and HP support to see if it is a Windows or HP issue.

But briefly, here are the answers to your questions:

"Are you asking a question about Microsoft Windows, a question about an AutoHotkey script, a question about AutoHotkey, or none of these?

I thought I was asking a question about an AutoHotKey script. But after months more testing, I think it is a Windows or HP problem, since I've determined that - most of the time - the Windows clipboard has stopped working when I'm experiencing these problems.

"What does "AHK scripts won't run" mean? "

Many of my scripts loop through files, and store information (such as the file path) into clipboard and then into a variable, which is used later in the script. When this issue arises (around 3 times a day), the ahk scripts post the following error message: "Error: SetClipboardData".

Then my ahk scripts which use the clipboard won't work (and even my built in Windows clipboard often doesn't work) until I do one or more - sometimes all - of the following things: (1) reboot my computer, (2) defrag my hard drive, (3) clean my registry, (4) clear the windows clipboard, or (5) run windows diagnostic commands like run cmd.exe /c dism.exe /online /cleanup-image /restorehealth,,show and run cmd.exe /c sfc /scannow,,show

But OCCASSIONALLY, the issue will resolve itself - and the clipboard and ahk scripts which rely on the clipboard will start working again - even if I don't do anything.

"What actually happens?"
Please see previous answer.

"What is the script that will not run?"
Around 30 of my scripts won't run when this happens. Here is one example, for a script to search through my Outlook emails (I'VE INDICATED WHERE IT BREAKS):

Code: Select all

#SingleInstance Force 
SetTitleMatchMode, 2  
If A_IsAdmin && !RegExMatch(DllCall("GetCommandLine", "str"), " /restart(?!\S)") {
 Try Run "%A_AhkPath%" /restart "%A_ScriptFullPath%"
 ExitApp
}
Process, Priority, , Realtime
SetTimer, ForceExitApp,  240000 

lst := "" 
Attachments := "" 
From := "" 
To := "" 
cc := "" 
Subject := "" 

Loop, files, C:\Clients\*, R
	If !InStr(A_LoopFileDir, "\Closed") & !InStr(A_LoopFileDir, "\Potential") & (A_LoopFileName = "Clip Box.txt")
		{
		FileReadLine, var, % A_LoopFileFullPath, 2
		lst .= var "|"
		}
sort, lst, D|

Gui, Font, s10

Gui, Add, Text, xm w70, Client:
Gui, Add, DropDownList,  x+m w250 r100 vVarX, % lst
Gui, Add, Text, xm w70, Subject
Gui, Add, ComboBox, x+m w250 vMySubject, ClientShortestName|MatterShort|MatterLong
Gui, Add, Text, xm w70, Attachments:
Gui, Add, DropDownList, x+m w246 vMyAttachment,|hasattachments:yes
Gui, Add, Text, xm w70, From:
Gui, Add, ComboBox,  x+m w250 vMyFrom, Client|OC
Gui, Add, Text, xm w70, To:
Gui, Add, ComboBox, x+m w250 vMyTo, Client|OC
Gui, Add, Text, xm w70, cc:
Gui, Add, ComboBox, x+m w250 vMycc, Client|OC
Gui, Add, Text, xm w70, Free Form:
Gui, Add, Edit, x+m w250 vFreeFormSubjectVar
Gui, Add, Text, xm w70, 
Gui, Add, Text, xm w70, Date Range
Gui, Add, DropDownList,  x+m w250 vDateRange, |On Date|Before Date|After Date|Between Dates
Gui, Add, MonthCal, vStartDate
Gui, Add, MonthCal, vEndDate
Gui, Add, Text, xm w70,
 Gui, Add, Button, x+m  w250, Done

Gui, Show,, Client?

Return

ButtonDone:
   Gui, Submit, Hide

FormatTime, StartDate, %StartDate%, M/d/yyyy 
FormatTime, EndDate, %EndDate%, M/d/yyyy 

Sleep, 1000

FileRead, Contents, C:\Clients\%VarX%\Client and Party Info\Clip Box.txt

Sleep, 1000

Clipboard:= Contents ; HERE IS WHERE THE SCRIPT BREAKS AROUND 3 TIMES A DAY AND THROWS AN ERROR MESSAGE.  THE SCRIPT WON'T START WORKING AGAIN UNTIL I GO THROUGH THE WHOLE SONG AND DANCE MENTIONED ABOVE.

Sleep, 100

Var1 := % StrSplit(clipboard,"`r").1  
Var2 := % StrSplit(clipboard,"`r").2  
Var3 := % StrSplit(clipboard,"`r").3  
Var8 := % StrSplit(clipboard,"`r").8  
Var10 := % StrSplit(clipboard,"`r").10 
Var11 := % StrSplit(clipboard,"`r").11  
Var14 := % StrSplit(clipboard,"`r").14  

if (MyFrom = "Client")
   { 
      RealFrom = From:%Var8%
   }

if (MyFrom = "OC")
   { 
      RealFrom = From:%Var14%
   }
if !(MyFrom = "OC") & !(MyFrom = "Client") & !(MyFrom = "")
   { 
      RealFrom = From:%MyFrom%
   }

if (MyTo = "Client")
   { 
      RealTo = To:%Var8%
   }

if (MyTo = "OC")
   { 
      RealTo = To:%Var14%
   }

if !(MyTo = "OC") & !(MyTo = "Client") & !(MyTo = "")
   { 
      RealTo = To:%MyTo%
   }

if (Mycc = "Client")
   { 
      Realcc = cc:%Var8%
   }

if (Mycc = "OC")
   { 
      Realcc = cc:%Var14%
   }

if !(Mycc = "OC") & !(Mycc = "Client") & !(Mycc = "")
   { 
      Realcc = cc:%Mycc%
   }

if (MySubject = "ClientShortestName")
   { 
      RealSubject = subject:%Var1%
   }

if (MySubject = "MatterShort")
   { 
      RealSubject = subject:%Var10%
   }

if (MySubject = "MatterLong")
   { 
      RealSubject = subject:%Var11%
   }

if !(MySubject = "ClientShortestName") & !(MySubject = "MatterShort") & !(MySubject = "MatterLong") & !(MySubject = "")
   { 
      RealSubject = subject:%MySubject%
   }


if (DateRange = "On Date")
   { 
     
 RealDateRange =  Received:%StartDate%
   }

if (DateRange = "Before Date")
   { 
     
 RealDateRange =  Received:<%StartDate%

}

if (DateRange = "After Date")
   { 
     
 RealDateRange =  Received:>%StartDate%
   }



if (DateRange = "Between Dates")
   { 
     
 RealDateRange =  Received:%StartDate%..%EndDate%
}

 
FreeFormSubjectVar := RegExReplace(FreeFormSubjectVar,"\.? *(\n|\r)+","")
MyAttachment := RegExReplace(MyAttachment,"\.? *(\n|\r)+","")
RealFrom := RegExReplace(RealFrom,"\.? *(\n|\r)+","")
RealTo := RegExReplace(RealTo,"\.? *(\n|\r)+","")
Realcc := RegExReplace(Realcc,"\.? *(\n|\r)+","")
RealSubject := RegExReplace(RealSubject,"\.? *(\n|\r)+","")

 Gui, Submit, Hide 

 if !(RealFrom="")
{
RealFrom = AND (%RealFrom%)
}

if !(RealTo="")
{
RealTo = AND (%RealTo%)
}

if !(Realcc="")
{
Realcc = AND (%Realcc%)
}

service := "WSearch"
RunWait % "sc.exe CONFIG " service " start=auto",, Hide
RunWait % "sc.exe CONFIG " service " start",, Hide
RunWait % "sc.exe START "   service,, Hide
RunWait % "sc.exe CONTINUE "   service,, Hide
 
IfWinExist ahk_exe OUTLOOK.EXE
WinActivate, Outlook ahk_class rctrl_renwnd32
else
	run, "C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE"  
WinWait ahk_exe OUTLOOK.EXE
WinActivate, Outlook ahk_class rctrl_renwnd32
WinWaitActive ahk_exe OUTLOOK.EXE

Sleep, 1000

SendInput, !q

Sleep, 900

SendInput, ^a

Sleep, 500

Send {Backspace}

Sleep, 500
clipboard = %FreeFormSubjectVar%%RealSubject% %RealFrom%%RealTo%%Realcc% %MyAttachment%%RealStartDate%%RealDateRange%

SendInput, %FreeFormSubjectVar%%RealSubject% %RealFrom%%RealTo%%Realcc% %MyAttachment%%RealStartDate%%RealDateRange%
 
Return

ForceExitApp:
SetTimer,  ForceExitApp, Off
ExitApp
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

16 May 2024, 21:12

Others here will be more familiar with that error and can chime in.

OK with calling Microsoft, though the testing approach that I mentioned would probably require about 10 minutes instead of 2 months. :)

If you shorten your script to the three lines that you would like to test, you could get the time down to 9 minutes! If the script fails, then you have a total of three lines to debug.

If you run your antivirus scan, your wait for that part is 0 minutes. You can start it and then go get some coffee.
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

16 May 2024, 21:22

Mikeyww, thank you. I stopped using Ditto about a week ago to see if that helped.

I will now methodically try your testing procedure.
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

16 May 2024, 22:32

Mikeyww, as always, you have given me brilliant advice. I closed all ahk scripts (and clipboard managers), and Windows copies and pastes flawlessly.

But when I try to use any ahk script which uses the clipboard, it still fails in the same way previously described.

So something is wrong with my ahk scripts ... even after updating to 1.1.37.02.

I also tried older versions of my ahk scripts which I written before I started having these clipboard problems. They are having the same problems.
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

17 May 2024, 06:02

You can now shorten a single test script to three lines, and test only that script, while no other scripts run. If the script fails, you have three lines to debug, and you can also post your revised script to get feedback about it. In your new post, include a description of what you do, what the script then does, and what the script should do instead.
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

20 May 2024, 15:41

Mikeyww, here is a a 4-line script that is failing (I've deleted the sleeps, Clipboard:="", and ClipWaits to simplify it here):

Code: Select all

FileRead, Contents, C:\Clients\z test\Client and Party Info\Clip Box.txt
Clipboard:= Contents
Msgbox %Clipboard%
And here is a 5-line script that is failing:

Code: Select all

FileRead, Contents, C:\Clients\z test\Client and Party Info\Clip Box.txt
Clipboard:= Contents
Var1 := % StrSplit(clipboard,"`r").1 ; This is ClientShortestName
Msgbox %Var1%
These scripts are just trying to read the contents of a text file and then display them in a message box. These scripts - like my longer scripts - work most of the time, but when I run into this weird clipboard issue, they fail.

Specifically, I get the following error message:

"Error: SetClipboardData"

And the error message points to the following line: clipboard :=Contents
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

20 May 2024, 17:40

You indicated that the script usually works. Are you running other scripts or programs that also manage or interact with the clipboard at the same time?

That is my only thought. Others here may have more insight into this.

Incidentally, := does not need to force an expression via % because it is always followed by an expression.

Your script worked here when I put some text into a file for testing.

I suggest that your tests always use a directory that the script can access, such as MyDocuments.

Are you executing this routine repeatedly? Below is a related post.

viewtopic.php?t=4777&start=20
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

20 May 2024, 18:30

Mikeyww,

No, I've closed all other ahk scripts and any other app/program which I think can access the clipboard.

I will check out the related post ...
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

21 May 2024, 19:26

I am still striking out.

I set up the following monitoring script, to tell me what was the last program which was using the clipboard before it crashed:

Code: Select all

^g:: ;get info about the process that last used/is using the clipboard
DetectHiddenWindows, On
vOutput := ""

Loop, 2
{
	if (A_Index = 1)
		hWnd := DllCall("GetClipboardOwner", Ptr)
	else
		hWnd := DllCall("GetOpenClipboardWindow", Ptr)
	WinGet, vPName, ProcessName, % "ahk_id " hWnd
	WinGetClass, vWinClass, % "ahk_id " hWnd
	WinGetTitle, vWinTitle, % "ahk_id " hWnd
	vOutput .= vPName "`r`n" vWinClass "`r`n" vWinTitle ""
}

vOutput := SubStr(vOutput, 1, -2)

MsgBox, %  vOutput
return
But it didn't show anything interesting. Specifically, after my clipboard broke again, it showed I had been using an AHK script. Then I closed all AHK scripts and copied something from Word, and it showed I was using the Word program.

Is there any way to have an AHK script monitor which program/service launches right before the clipboard crashes? Again, my suspicion is that one of the stupid HP or Microsoft "telemetry" processes used to gather info - which are rootkit level and impossible to remove - are what's causing this problem.
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

21 May 2024, 19:30

Others here may know the way.

How and when are you running your four-line script file? It just runs and then exits immediately afterwards? What is in your text file when the script fails? Have you confirmed that the script successfully reads your text file? Can you post a screenshot of the error message that matches the exact script that you have posted? Do you have an example of a text file that leads to a failure? Is the text file being updated and changed much? What updates the text file, when, and how?

You have four lines to debug, so let's eliminate the first line. If you eliminate the FileRead and instead hard-code the contents in the script, does it prevent the error?
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

23 May 2024, 13:48

Mikeyww,

I run scripts perhaps every 5 minutes to search for files, save files, send emails, etc. etc. that use FileRead and clipboard:=contents as a core part of the scripts. The scripts close 2 minutes after I run them.

When I run this script:

Code: Select all

FileRead, Contents, C:\Clients\z test\Client and Party Info\Clip Box.txt
Clipboard:= Contents
Msgbox, % clipboard
I get this error message:
Error Message.jpg
Error Message.jpg (15.89 KiB) Viewed 312 times
This is the contents of C:\Clients\z test\Client and Party Info\Clip Box.txt:
z
z Test
MyCase Number
ClientFullName
ClientContact
ClientAddress1
ClientAddress2 (City,State Zip)
ClientEmail
ClientPhone
z Test
z Test
OppName
OppContactName
OppContactEmail
OppContactPhone
OppContactAddress (Whole Address)
Agreed Retainer: $7,000.00
Retainer Balance: $6,910.00
Past Due Amount Including Replenishing Retainer: $0
Earned Fees and Costs Which Are Not Yet Paid: $$0
Costs: $0.00
Last Invoice No: 09877
Hey Dude,
Yo Dude!
Last Invoice was on 5/13/2024 for $80 fees and $80 costs ($160.00 total) on Invoice 09877
Last Payment was on 5/13/2024 for $2000 on Invoice 09877

cc:
Next task

$/hour
Hard-coding the contents in the script DOES prevent the error.

Also, modifying the 3-line script to avoid the clipboard - and instead store the previously-read contents of the textfile into a variable - also seems to prevent the error:

Code: Select all

FileRead, Contents, C:\Clients\z test\Client and Party Info\Clip Box.txt
Var1:= Contents
Msgbox, %Var1%
So maybe I should experiment by revising all of my scripts to replace Clipboard:= Contents with Var1:= Contents and see what happens?
User avatar
mikeyww
Posts: 27241
Joined: 09 Sep 2014, 18:38

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

23 May 2024, 14:35

That's helpful information and seems to get a step closer to the problem, though I cannot pinpoint it exactly. If the script reads the text file exactly when the file is actually changing, I am not sure what happens. In any case, this information might help other readers to know what is causing the issue. In the meantime, you might want to try something like what you wrote, or the following.

Code: Select all

FileRead contents, C:\Clients\z test\Client and Party Info\Clip Box.txt
Msgbox % contents
Clipboard := contents
I presume that you are then pasting the clipboard's contents somewhere. If not, then I suppose that you do not actually need the clipboard at all.
AlFlo
Posts: 360
Joined: 29 Nov 2021, 21:46

Re: Clipboard Stopped Working ... AHK Scripts Won't Run

28 May 2024, 16:59

mikeyww,

I have changed most of my scripts which use the clipboard so they use a variable instead ... i.e. changed Clipboard:= Contents to Var1:= Contents.

They all seem to work consistently now.

My clipboard still breaks periodically. For example, I've replaced ditto with my own ahk scripts which copy everything I copy using control-c into a text file. When the clipboard breaks, it stop appending to the textfile, and instead just adds blank space.

So I have greatly reduced the impact of the clipboard issue, but haven't really figured out how to stop the clipboard from periodically breaking.

I've found both Microsoft and HP support to be pretty useless ... they suggest some obvious strategies, none of which work.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: j4byers and 130 guests