texstudio-org/texstudio

Powershell Get-Clipboard script freezes TeXStudio when copying text from TeXStudio's document LaTeX code.

Closed this issue · 11 comments

Environment

  • TeXstudio: 4.8.1
  • Qt: Version 6.7.1, compiled with Qt 6.7.1 R
  • OS: Windows 11
  • TeX distribution: Miktex

Context

I created a script in TeXStudio(in the "Macros" section) with the following code(with the "Script" button option selected) that simply stores what is in the clipboard but into a powershell variable. Yes, this script really does nothing in TeXStudio, but it is just for a testing to use it in another script. This is what the Script in the Macros section looks like:

image

And this is what the testScript.ps1 looks like(which is not the problem):

image

Expected behavior

The expected behavior is to trigger the script with "//."(as per the image), and do nothing on TeXStudio(this is just testing for a bigger project). The script should run and be finished instantly.

Actual behavior

What actually happens is that after copying a text from TeXStudio itself (say, \begin{document}) and then trigger the script with "//.", the app freezes, an overlay of white occurs, and then the top of the application says that TeXStudio is "Not Responding". I must wait about a minute for the app to unfreeze and then everything comes back to normal. The Messages window says "Process exited normally". The problem is that the freezing takes too much time.

image

image

image

image

I have tested other texts, from browsers, other apps and software, and none freeze TeXStudio.

How to reproduce

To reproduce, copy and paste the code from my macro script into TeXStudio script, as well as create the powershell script and give its path to the TexStudio script and put a trigger to trigger the script. Before triggering the script in TeXStudio, copy some text from the TeXStudio code and watch it freeze.

my guess would be that Get-Clipboard wants to interact with txs whereas txs is stuck at executing the script.
Any particular reason why you don't interact with the selection/clipboard on the txs script side ?

my guess would be that Get-Clipboard wants to interact with txs whereas txs is stuck at executing the script.

Any particular reason why you don't interact with the selection/clipboard on the txs script side ?

For now, I don't want the script to do anything on the TeXStudio side(yet). I am working on a bigger script, but want the most fundamental part to work properly.

This script works for any text of any other app or browser or notepad text, etc. It's specifically the text on TeXStudio that freezes the app. Perhaps the encoding of the text? The font? The style? I'm not sure at this point.

qt uses one main thread for GUI and basic interaction. This thread calls powershell and waits for it to finish, being completely blocked. Powershell executes "Get-Clipboard" which now asks txs to provide the clipboard. However, that would be the task of the main thread which is blocked.
The power shell works fine, if it is not called via the systemCmd from txs.

qt uses one main thread for GUI and basic interaction. This thread calls powershell and waits for it to finish, being completely blocked. Powershell executes "Get-Clipboard" which now asks txs to provide the clipboard. However, that would be the task of the main thread which is blocked.

The power shell works fine, if it is not called via the systemCmd from txs.

So how do I fix it? Any thoughts or ideas?

well, leave away cmd.waitForFinished() ?

well, leave away cmd.waitForFinished() ?

While this does solve the problem for this script(which is doing nothing for TeXStudio), it will not work if the script were to give an output or some sort of information necessary for the macro/script to work because cmd.waitForFinished() waits for the powershell script to be finished before going to the next line.

you should look into a different approach to exchange data between txs and your script.

you should look into a different approach to exchange data between txs and your script.

Yes, I have tried implementing User Commands. Which brings me 2 questions:

  • Can one trigger a macro through User Commands? I have checked documentation and have not found anything on that topic.
  • When using PowerShell through User Commands, does the script being called through User Commands use systemCMD? Just asking because you(sunderme) stated:

The power shell works fine, if it is not called via the systemCmd from txs.

  1. no
  2. it is a similar mechanism but it is detached (similar like leaving away waitForFinished)

From my point of view, this is answered