Add Typora widget to the right-click menu

Add Typora widget to the right-click menu

1. Reference materials

  1. Add right-click menu function to all files under Windows
  2. Add the right-click menu to run the program and transfer the full name of the right-click file
  3. bat pass parameters
  4. Several methods for batch processing to determine whether it is a file or a folder
  5. %~dp0 meaning in Chinese
  6. Java丨Cmd has garbled code when running Jar
  7. 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

image-20210204215214347

Before I wrote a blog will Sublime Text added to the right-click menu , so I whim:

  1. Package the Java program into an executable jar package
  2. Write typora-tools.bat batch program to start this jar package and pass in the storage path of the notes
  3. 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

  1. The first line: the text displayed in the right-click menu bar
  2. Second line: the icon displayed in the right-click menu bar
  3. The third line: Click on the program to be executed in the right-click menu bar, which %1is used to pass parameters (file path)

image-20210204220341279

4. Solution steps

Program packaging

Package the Java program into an executable jar package, here is a packaging tutorial: IDEA package executable jar package

image-20210205000320833

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 %1whether the acquisition of the first argument to the bat

image-20210205000602310


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

image-20210205170632188

But when I opened the Markdown notes, it was garbled. . .

image-20210205170515695

Solve the garbled problem

Odd strange, I debugging in IDEA is also no problem, the use of java -jarexecution appeared garbled, Java code uses a UTF8coding, Typora saved md notes also using UTF8coding

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-8the parameters, the garbage problem is resolved

image-20210205171402016


Modify typora-tools.bat batch program

In typora-tools.bat batch program, execute java -jarthe Add command -Dfile.encoding=utf-8parameters

@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]

image-20210205101205521

The effect after adding to the right-click menu bar

image-20210205165735147

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

image-20210205104619980

5. Project address

oneby1314/typora-tools

Guess you like

Origin blog.csdn.net/oneby1314/article/details/113706310