【Java】【Windows】調用Typora把md文件轉成html並帶上css樣式,理論上可以調用任何exe程序


 

 

1、利用移動鼠標模擬用戶點擊功能,可使用任何程序

 1 package com.xiaostudy.server;
 2 
 3 import java.awt.*;
 4 import java.awt.datatransfer.StringSelection;
 5 import java.awt.event.InputEvent;
 6 import java.awt.event.KeyEvent;
 7 import java.io.File;
 8 import java.io.IOException;
 9 
10 public class MyTest {
11 
12     public static void main(String[] args) throws IOException {
13         test1();
14     }
15 
16     private static void test1() {
17 
18         try {
19             String command = "D:/Program Files/Typora/Typora.exe D:/test/md2html/1.md";
20             String htmlPath = "D:/test/md2html/1.html";
21             File htmlFile = new File(htmlPath);
22             boolean htmlExist = htmlFile.exists();
23             // 執行命令返回執行的子進程對象
24             Runtime.getRuntime().exec(command);
25             // 休息0.5秒鍾
26             Thread.currentThread().sleep(500);
27 
28             Robot robot = new Robot();
29             //設置Robot產生一個動作后的休眠時間,否則執行過快
30             robot.setAutoDelay(150);
31 
32             // 最大化窗口,讓程序滿屏,起點在0,0
33             robot.keyPress(KeyEvent.VK_WINDOWS);
34             robot.keyPress(KeyEvent.VK_UP);
35             robot.keyRelease(KeyEvent.VK_UP);
36             robot.keyRelease(KeyEvent.VK_WINDOWS);
37 
38             // 程序距離0,0位置,最大化時就是0偏移
39             int x = 0;
40             int y = 0;
41 
42             // 快捷鍵:文件(安心Alt+F鍵然后松開)
43             robot.keyPress(KeyEvent.VK_ALT);
44             robot.keyPress(KeyEvent.VK_F);
45             robot.keyRelease(KeyEvent.VK_F);
46             robot.keyRelease(KeyEvent.VK_ALT);
47 
48             // 第一個移動鼠標是讓鼠標回到0,0點,第二個移動鼠標到(導出)
49             robot.mouseMove(-1, -1);
50             robot.mouseMove(23+x, 450+y);
51             // 點擊鼠標左鍵,然后松開
52             System.out.println("鼠標左鍵");
53             robot.mousePress(InputEvent.BUTTON1_MASK);
54             robot.mouseRelease(InputEvent.BUTTON1_MASK);
55 
56             // 鼠標移動到HTML
57             robot.mouseMove(-1, -1);
58             robot.mouseMove(300+x, 475+y);
59             // 點擊鼠標左鍵,然后松開
60             System.out.println("鼠標左鍵");
61             robot.mousePress(InputEvent.BUTTON1_MASK);
62             robot.mouseRelease(InputEvent.BUTTON1_MASK);
63 
64             // 選擇保存到具體位置
65             // 復制
66             //聲明一個StingSelection 對象,並使用String的參數完成實例化;
67             StringSelection stringSelection = new StringSelection(htmlFile.getAbsolutePath());
68             //使用Toolkit對象的setContents將字符串放到粘貼板中 ;
69             Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
70             // 粘貼
71             robot.keyPress(KeyEvent.VK_CONTROL);
72             robot.keyPress(KeyEvent.VK_V);
73             robot.keyRelease(KeyEvent.VK_V);
74             robot.keyRelease(KeyEvent.VK_CONTROL);
75             // 回車
76             robot.keyPress(KeyEvent.VK_ENTER);
77             robot.keyRelease(KeyEvent.VK_ENTER);
78 
79             if (htmlExist) {
80                 // 保存覆蓋
81                 robot.keyPress(KeyEvent.VK_Y);
82                 robot.keyRelease(KeyEvent.VK_Y);
83             }
84             // 關閉
85             robot.keyPress(KeyEvent.VK_CONTROL);
86             robot.keyPress(KeyEvent.VK_W);
87             robot.keyRelease(KeyEvent.VK_W);
88             robot.keyRelease(KeyEvent.VK_CONTROL);
89 
90         } catch (Exception ex) {
91             ex.printStackTrace();
92         } finally {
93         }
94     }
95 
96 }

下面動圖執行程序后鼠標是程序調用,直到程序關閉后,打開html文件是手動的。

 

2、全程使用快捷鍵,不用移動鼠標,但是要設置導出HTML的快捷鍵

文件->偏好設置->通過->打開高級設置->打開conf.user.json文件

"HTML":"Ctrl+Alt+E"
 1 package com.xiaostudy.server;
 2 
 3 import java.awt.*;
 4 import java.awt.datatransfer.StringSelection;
 5 import java.awt.event.KeyEvent;
 6 import java.io.File;
 7 
 8 public class MyTest {
 9 
10     public static void main(String[] args) {
11         test2();
12     }
13 
14     private static void test2() {
15 
16         try {
17             String command = "D:/Program Files/Typora/Typora.exe D:/test/md2html/1.md";
18             String htmlPath = "D:/test/md2html/1.html";
19             File htmlFile = new File(htmlPath);
20             boolean htmlExist = htmlFile.exists();
21             // 執行命令返回執行的子進程對象
22             Runtime.getRuntime().exec(command);
23             // 休息5秒鍾
24             Thread.currentThread().sleep(500);
25 
26             Robot robot = new Robot();
27             //設置Robot產生一個動作后的休眠時間,否則執行過快
28             robot.setAutoDelay(150);
29 
30             // 剛設置好的快捷鍵,導出->HTML(Ctrl+Alt+E)
31             robot.keyPress(KeyEvent.VK_CONTROL);
32             robot.keyPress(KeyEvent.VK_ALT);
33             robot.keyPress(KeyEvent.VK_E);
34             robot.keyRelease(KeyEvent.VK_E);
35             robot.keyRelease(KeyEvent.VK_ALT);
36             robot.keyRelease(KeyEvent.VK_CONTROL);
37 
38             // 復制
39             //聲明一個StingSelection 對象,並使用String的參數完成實例化;
40             StringSelection stringSelection = new StringSelection(htmlFile.getAbsolutePath());
41             //使用Toolkit對象的setContents將字符串放到粘貼板中 ;
42             Toolkit.getDefaultToolkit().getSystemClipboard().setContents(stringSelection, null);
43 
44             // 粘貼
45             robot.keyPress(KeyEvent.VK_CONTROL);
46             robot.keyPress(KeyEvent.VK_V);
47             robot.keyRelease(KeyEvent.VK_V);
48             robot.keyRelease(KeyEvent.VK_CONTROL);
49             // 回車
50             robot.keyPress(KeyEvent.VK_ENTER);
51             robot.keyRelease(KeyEvent.VK_ENTER);
52 
53             if (htmlExist) {
54                 // 保存覆蓋
55                 robot.keyPress(KeyEvent.VK_Y);
56                 robot.keyRelease(KeyEvent.VK_Y);
57             }
58             
59             // 關閉
60             robot.keyPress(KeyEvent.VK_CONTROL);
61             robot.keyPress(KeyEvent.VK_W);
62             robot.keyRelease(KeyEvent.VK_W);
63             robot.keyRelease(KeyEvent.VK_CONTROL);
64 
65         } catch (Exception ex) {
66             ex.printStackTrace();
67         } finally {
68         }
69     }
70 
71 }

然后關閉Typora再重新打開,就可以看到快捷鍵了

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM