notepad-plus-plus / notepad-plus-plus

Notepad++ official repository

Home Page:https://notepad-plus-plus.org/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

AutoHotKey script copying, modifying, pasting text via clipboard not working

kriegaex opened this issue · comments

Is there an existing issue for this?

  • I have searched the existing issues

Description of the Issue

My AutoHotKey script is copying something to the clipboard using keyboard shortcuts, modifying the clipboard content and pasting it back into any text editor. This works fine with all IDEs, editors and text processors, even text fields in web browsers. Only in NotePad++, it is very unreliable. On average, I have to execute the command 5-10 times until it actually works. What can I do to make it work? What is so special in NotePad++ that it is the only application refusing to do the job? Do I need to set any special options? Adjust my AHK script to send the keys in a different way?

Sorry to ask here and not in the AHK forum, but this problem is specific to NotePad++ only.

Steps To Reproduce

This is my AHK script:

+^x::
  JAR := USERPROFILE "\Documents\java-src\TextMailFormatter\target\text-mail-formatter-1.0-SNAPSHOT.one-jar.jar"
  EnvGet, TEMP, TEMP
  TEMP_FILE := TEMP "\clip.txt"
  FileDelete, %TEMP_FILE%
;  Clipsaved := clipboardall
  Clipboard =
  SendInput ^c
  Clipwait, 1
  if clipboard=
  {
    SendInput ^a^c
    Clipwait, 1
  }
  FileAppend, %clipboard%, *%TEMP_FILE%,UTF-8
  ; Java 18 problem with UTF-8 by default: Either use full path to Java < 18
  ; or omit "*P65001" from FileRead command below.
  ; Otherwise special characters will be broken.
  ; RunWait %comspec% /c ""c:\Program Files\Java\jdk-17\bin\java.exe" -Dfile.encoding=UTF-8 -jar "%JAR%" "%TEMP_FILE%" > "%TEMP_FILE%.x""
  ; RunWait wt.exe %comspec% /c "java -Dfile.encoding=UTF-8 -jar "%JAR%" "%TEMP_FILE%" > "%TEMP_FILE%.x""
  RunWait %comspec% /c "java -Dfile.encoding=UTF-8 -jar "%JAR%" "%TEMP_FILE%" > "%TEMP_FILE%.x""
  Clipboard =
  ; Make sure that according to https://stackoverflow.com/a/41416262/1082681
  ; you set the Autorun command to "chcp 65001" for cmd.exe.
  ; Otherwise "*P65001" will not work.
  ;FileRead, clipboard, *P65001 %TEMP_FILE%.x
  FileRead, clipboard, %TEMP_FILE%.x
  Clipwait, 1
  SendInput ^v
;  Clipboard := clipsaved 
  Return

BTW, the external Java program I am executing does the actual reformatting. You just need to know that it modifies the text buffer, writes it into a file and AHK reads that file into the clipboard and pastes it back into the text. Please do not point your finger to the external program or file I/O. If that was unreliable, it would not work perfectly in all the other applications. It must be something about keyboard shortcuts and/or clipboard handling in NP++.

Current Behavior

See above

Expected Behavior

See above

Debug Information

Notepad++ v8.6.5   (64-bit)
Build time : Mar 29 2024 - 17:04:43
Path : C:\Program Files\Notepad++\notepad++.exe
Command Line : 
Admin mode : OFF
Local Conf mode : OFF
Cloud Config : OFF
Periodic Backup : ON
OS Name : Windows 10 Pro (64-bit
OS Version : 22H2
OS Build : 19045.4291
Current ANSI codepage : 1252
Plugins : 
    mimeTools (3.1)
    NppConverter (4.6)
    NppExport (0.4)

Anything else?

No response