zoukankan      html  css  js  c++  java
  • 【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再重新打开,就可以看到快捷键了

  • 相关阅读:
    mysql外键添加error1215
    shell命令获取最新文件的名称
    centos7 apache提供文件下载
    centos7 时间设置
    微服务通信的类型
    angular-cli
    npm
    模块相关
    加油!冲冲冲
    软件评测
  • 原文地址:https://www.cnblogs.com/xiaostudy/p/12905945.html
Copyright © 2011-2022 走看看