如何使用某个字符集将文本复制到剪贴板

时间:2017-08-27 10:09:58

标签: powershell batch-file encoding character-encoding


我一直在尝试使用批处理文件和Quick Macros的组合来制作一个程序,它会自动为我的母语(捷克语)中的一段文本添加变音符号。 我已经把一切都搞定了;

  • 用户在屏幕上选择文字
  • 按下键盘快捷键
  • 将文本复制到文件
  • 然后将包含该文本的POST请求发送到server(使用批处理脚本)
  • 响应存储在响应文件中
  • 相同的批处理脚本修剪响应文件以仅获取 固定文字
  • 并将其存储在另一个文件中

这是我遇到问题的地方。捷克语变音符号使用ISO-8859-2字符集,但复制文件内容时会获得ANSI编码,在随后粘贴时会破坏重音字符以替换用户原始选定的文本。
目前我通过让QM打开一个Notepad ++实例来解决这个问题,手动浏览菜单来更改字符集,然后选择并复制文本,然后关闭Notepad ++。 正如你所看到的那样,这是非常不优雅的,每次运行脚本时都会在我的屏幕上弹出一个窗口,等待占总运行时间的80%左右。

有谁知道如何使用某个字符集将文本文件中的文本导入剪贴板?最好是批量还是PowerShell? (我知道如果在已经在正确的字符集中显示它的情况下复制它会有效,所以我想它在某种程度上是可能的。)

我的所有代码:
QM 由键盘快捷键触发(位看起来很乱,但每行都有注释)

/create variable 's' and copy current selection to it
str s.getsel;
/if nothing was selected, the program ends
if (empty(s))
    end
/remember which window the selection is from
int orWin = win
/create "params.txt" from content of 's'
s.setfile("D:\AppData\Local\CZaccent\params.txt");
/wait until "params.txt" is created (yes, this is the right way to do it)
rep
    if(FileExists("D:\AppData\Local\CZaccent\params.txt")) break
/run "endline_purge.bat" minimised
run "D:\AppData\Local\CZaccent\endline_purge_min";
/wait for "endling_purge.bat" to close (any command window being destroyed triggers this)
wait -1 WD win("C:\Windows\System32\cmd.exe");
/copy new content of "params.txt" to variabel 's'
s.getfile("D:\AppData\Local\CZaccent\params.txt");
/remove "params.txt"
del- "D:\AppData\Local\CZaccent\params.txt";
/run "request.bat" minimised, passing 's' as arguments
run "D:\AppData\Local\CZaccent\request_min" s;
/wait for "request.bat" to close
wait -1 WD win("C:\Windows\System32\cmd.exe");
/remove "response.html"
del- "D:\AppData\Local\CZaccent\response.html"
/open "output.txt" (created by "request.bat") in Notepad++
run "C:\Program Files\Notepad++\notepad++.exe" "D:\AppData\Local\CZaccent\output.txt"
/wait for Notepad++ to open
wait -1 WA win("D:\AppData\Local\CZaccent\output.txt - Notepad++" "Notepad++")
/use shortcut keys to select ISO-8859-2 charset
'An cY eY
/if a dialog opens, confirm it (error occurs if it is not open)
act(win("Save Current Modification"))
err goto search_cont
'Y
 search_cont
/select all
'Ca
/copy selection to 's'
s.getsel
/close Notepad++
clo
/delete "output.txt"
del- "D:\AppData\Local\CZaccent\output.txt"
/restore the original window where selection is active
act(orWin)
/trim trailing endline from 's' (13=CR 10=NL)
str CRNL.format("%c%c", 13 10)
s.rtrim(CRNL)
/paste content of 's' (into the active selection, replacing it)
'(s)

endline_purge.bat

@echo off
setlocal EnableDelayedExpansion
set "line="
for /F "delims=" %%a in (params.txt) do set "line=!line!\n%%a"
echo !line:~2!>params.txt

request.bat (还会从html响应中删除不需要的内容)

@echo off
call "C:\Program Files\cURL\curl.exe" --data "text=%*" https://nlp.fi.muni.cz/cz_accent/ > D:\AppData\Local\CZaccent\response.html
SETLOCAL ENABLEDELAYEDEXPANSION
set content=
for /f "delims=" %%i in (response.html) do set content=!content! %%i
set content=!content:~391,-993!
echo !content:^<p^> =^

! > output.txt

1 个答案:

答案 0 :(得分:0)

chcp 28592

此命令使cmd.exe显示(和复制)ISO-8859-2中的文本,正确显示重音字符。