Add Typora widget to the right-click menu
1. Reference materials
- Add right-click menu function to all files under Windows
- Add the right-click menu to run the program and transfer the full name of the right-click file
- bat pass parameters
- Several methods for batch processing to determine whether it is a file or a folder
- %~dp0 meaning in Chinese
- Java丨Cmd has garbled code when running Jar
- Java operating system clipboard (Clipboard) realizes copy and paste
2. Foreword
I wrote a Typora gadget before, it is not very convenient to use, every time you have to enter IDEA to modify the note storage path, and then click [Run] to run the program
Before I wrote a blog will Sublime Text added to the right-click menu , so I whim:
- Package the Java program into an executable jar package
- Write typora-tools.bat batch program to start this jar package and pass in the storage path of the notes
- Add typora-tools.bat batch program to the right-click menu bar
Damn it, pretty good!
3. Solutions
How to add a custom program to the right-click menu bar?
That dry on to dry before adding Sublime Text to the right-click menu when exposed to the following batch program, we can Yihuhuhuapiao
@ECHO OFF & PUSHD %~DP0 & TITLE
>NUL 2>&1 REG.exe query "HKU\S-1-5-19" || (
ECHO SET UAC = CreateObject^("Shell.Application"^) > "%TEMP%\Getadmin.vbs"
ECHO UAC.ShellExecute "%~f0", "%1", "", "runas", 1 >> "%TEMP%\Getadmin.vbs"
"%TEMP%\Getadmin.vbs"
DEL /f /q "%TEMP%\Getadmin.vbs" 2>NUL
Exit /b
)
SET /P ST=输入a添加右键菜单,输入d删除右键菜单:
if /I "%ST%"=="a" goto Add
if /I "%ST%"=="d" goto Remove
:Add
reg add "HKEY_CLASSES_ROOT\*\shell\Sublime Text" /t REG_SZ /v "" /d "用 &Sublime Text 打开" /f
reg add "HKEY_CLASSES_ROOT\*\shell\Sublime Text" /t REG_EXPAND_SZ /v "Icon" /d "%~dp0sublime_text.exe" /f
reg add "HKEY_CLASSES_ROOT\*\shell\Sublime Text\command" /t REG_SZ /v "" /d "%~dp0sublime_text.exe \"%%1\"" /f
exit
:Remove
reg delete "HKEY_CLASSES_ROOT\*\shell\Sublime Text" /f
exit
Batch processing program analysis
Adding the registry is just three batch programs
- The first line: the text displayed in the right-click menu bar
- Second line: the icon displayed in the right-click menu bar
- The third line: Click on the program to be executed in the right-click menu bar, which
%1
is used to pass parameters (file path)
4. Solution steps
Program packaging
Package the Java program into an executable jar package, here is a packaging tutorial: IDEA package executable jar package
Batch start jar package
By default, bat can only get 1~9 parameters, which are quoted by %1
, %2
... respectively %9
. If more than 9 parameters are passed to bat, shift must be used.
We have to test %1
whether the acquisition of the first argument to the bat
Execute the jar package through the bat batch program and pass in the note storage path
Write the typora-tools.bat batch program to start the jar package (the previous lesson told me that the jar package here cannot use a relative path), and pass in the storage path of the notes ( %1
)
In order to prevent misuse of the folder, only a single file is allowed to operate here, so as not to recurse the entire note directory. . .
@echo off
if exist %1\ (echo dir or null is not permitted) else (java -jar C:\Users\Heygo\Desktop\Codes\typora-tools\typora-tools.jar %1)
pause
Batch program testing
Execute typora-tools.bat "C:\Users\Heygo\Desktop\TestEncoding.md"
commands, test batch program is correct, the results from the following problem - it seems like there
But when I opened the Markdown notes, it was garbled. . .
Solve the garbled problem
Odd strange, I debugging in IDEA is also no problem, the use of java -jar
execution appeared garbled, Java code uses a UTF8
coding, Typora saved md notes also using UTF8
coding
Under cmd, the common command to run the jar package is java -jar typora-tools.jar
, but this may cause incorrect encoding of java runtime and cause various errors. At this time, we can specify the encoding parameters when running the jar package
java -Dfile.encoding=utf-8 -jar typora-tools.jar
Test garbled problem
Add on -Dfile.encoding=utf-8
the parameters, the garbage problem is resolved
Modify typora-tools.bat batch program
In typora-tools.bat batch program, execute java -jar
the Add command -Dfile.encoding=utf-8
parameters
@echo off
if exist %1\ (echo dir not permitted) else (java -Dfile.encoding=utf-8 -jar C:\Users\Heygo\Desktop\Codes\typora-tools\typora-tools.jar %1)
pause
Add to the right-click menu bar
Add the typora-tools.bat batch program to the right-click menu bar, follow the gourd drawing, and change it according to the batch program that adds Sublime Text to the right-click menu bar
@ECHO OFF & PUSHD %~DP0 & TITLE
>NUL 2>&1 REG.exe query "HKU\S-1-5-19" || (
ECHO SET UAC = CreateObject^("Shell.Application"^) > "%TEMP%\Getadmin.vbs"
ECHO UAC.ShellExecute "%~f0", "%1", "", "runas", 1 >> "%TEMP%\Getadmin.vbs"
"%TEMP%\Getadmin.vbs"
DEL /f /q "%TEMP%\Getadmin.vbs" 2>NUL
Exit /b
)
SET /P ST=输入a添加右键菜单,输入d删除右键菜单:
if /I "%ST%"=="a" goto Add
if /I "%ST%"=="d" goto Remove
:Add
reg add "HKEY_CLASSES_ROOT\*\shell\typora-tools" /t REG_SZ /v "" /d "用 &typora-tools 执行" /f
reg add "HKEY_CLASSES_ROOT\*\shell\typora-tools" /t REG_EXPAND_SZ /v "Icon" /d "%~dp0typora-tools.ico" /f
reg add "HKEY_CLASSES_ROOT\*\shell\typora-tools\command" /t REG_SZ /v "" /d "%~dp0typora-tools.bat \"%%1\"" /f
exit
:Remove
reg delete "HKEY_CLASSES_ROOT\*\shell\typora-tools" /f
exit
%~dp0
It means to change the current directory to the directory of the batch process itself. Now, I copied all the files I need to the same level directory of the batch file [!Add/Delete right-click menu.bat]
The effect after adding to the right-click menu bar
Click [Execute with typora-tools],
Program optimization: Copy Markdown notes to the pasteboard after execution
Java program operating system pasteboard: realize the function of copy and paste
/**
* @ClassName ClipboardUtil
* @Description TODO
* @Author Oneby
* @Date 2021/2/5 10:39
* @Version 1.0
*/
public class ClipboardUtil {
/***
* @description: 将文本的复制到系统的粘贴板中
* @param: text 复制到粘贴板中的文本
* @return: void
* @author Oneby
* @date: 10:42 2021/2/5
*/
public static void setClipboardString(String text) {
// 获取系统剪贴板
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// 封装文本内容
Transferable trans = new StringSelection(text);
// 把文本内容设置到系统剪贴板
clipboard.setContents(trans, null);
}
/***
* @description: 获取系统粘贴板的内容
* @param:
* @return: java.lang.String 粘贴板中的内容
* @author Oneby
* @date: 10:42 2021/2/5
*/
public static String getClipboardString() {
// 获取系统剪贴板
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
// 获取剪贴板中的内容
Transferable trans = clipboard.getContents(null);
if (trans != null) {
// 判断剪贴板中的内容是否支持文本
if (trans.isDataFlavorSupported(DataFlavor.stringFlavor)) {
try {
// 获取剪贴板中的文本内容
String text = (String) trans.getTransferData(DataFlavor.stringFlavor);
return text;
} catch (Exception e) {
e.printStackTrace();
}
}
}
return null;
}
}
Copy the contents of the md file to the pasteboard at the end of the program