logo头像
Snippet 博客主题

AutoHotkey1.0实现Typora效率翻倍

本文于 407 天之前发表,文中内容可能已经过时。

一、AutoHotkey下载

AutoHotkey官网

AutoHotkey下载地址

AutoHotkey 阿里网盘下载地址


二、Typora

Typora中文站

Typora下载地址


三、AutoHotkey的安装方法

1、启动AutoHotkey_xxx_setup.exe,选择自定义安装

image-20230417005020518

2、选择AutoHotkey与您电脑相配的程序

image-20230417005025447

3、更改安装路径

image-20230417005029254

4、点击【install】软件就会安装

image-20230417005033226

5、AutoHotkey安装完成

image-20230417005037721


四、Typora颜色快捷键

4.1、快捷键脚本

新建后缀为ahk 文件 MyHotkeyScript.ahk,将以下代码复制进去

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#Requires AutoHotkey v1.1+
#IfWinActive ahk_exe Typora.exe

; Alt+1
!1::addOrReplaceFontColor("red")
; Alt+2 绿色
!2::addOrReplaceFontColor("#10c300")
; Alt+3 浅蓝
!3::addOrReplaceFontColor("cornflowerblue")
; Alt+4
!4::addOrReplaceFontColor("orange")
; Alt+W 去除颜色
!w::removeFontColor()
; Alt+Q 插入换行标签<br>
!q::
SendInput {Text}<br>
return

#IfWinActive

;--------------------------------------
; 添加或替换颜色标签
;--------------------------------------
addOrReplaceFontColor(color) {
clipboard := "" ; 清空剪贴板
; 选中整行
Send {Home}
Send +{End}
Send ^c
ClipWait, 0.3

if (clipboard != "") {
text := clipboard
; 检查是否已有 **<span style='color:...'>...**
if RegExMatch(text, "^\*\*<span\s+style=['""]color:\s*([^'"";]+);?['""]>(.*)</span>\*\*$", m) {
innerText := m2
; 去掉旧的 markdown ** 和 span 标签
innerText := RegExReplace(innerText, "\*\*", "")
; 直接替换颜色,并保持 markdown 加粗
text := "**<span style='color:" . color . "'>" . innerText . "</span>**"
} else {
; 没有标签则添加 ** + span
; 去掉旧的 **
text := RegExReplace(text, "\*\*", "")
; 去掉旧的 font 或 span
text := RegExReplace(text, "<font[^>]*>", "")
text := RegExReplace(text, "</font>", "")
text := RegExReplace(text, "<span[^>]*>", "")
text := RegExReplace(text, "</span>", "")
text := "**<span style='color:" . color . "'>" . text . "</span>**"
}
clipboard := text
Send ^v
Send {End}
} else {
; 空行时插入标签,加粗占位
newText := "**<span style='color:" . color . "'></span>**"
SendInput {Text}%newText%
; 光标定位到 span 内部
Send {Left 7} ; 把光标移到 </span>
}
}



removeFontColor() {
clipboard := ""
; 选中整行
Send {Home}
Send +{End}
Send ^c
ClipWait, 0.3

if (clipboard != "") {
cleaned := clipboard
; 去掉 <font> 和 <span> 标签
cleaned := RegExReplace(cleaned, "<font[^>]*>", "")
cleaned := RegExReplace(cleaned, "</font>", "")
cleaned := RegExReplace(cleaned, "<span[^>]*>", "")
cleaned := RegExReplace(cleaned, "</span>", "")
; 去掉 Markdown 粗体 **
cleaned := RegExReplace(cleaned, "\*\*", "")
clipboard := cleaned
Send ^v
Send {End}
}
}

4.2、执行方式

方式1:

双击运行 MyHotkeyScript.ahk文件,然后去Typora 尝试一下快捷键

选择要设置颜色的文字,按Alt+1添加红色,按Ctrl+w取消样式!

方式2:

右键 MyHotkeyScript.ahk 脚本文件,点击Compile Script编译脚本成exe程序,就可以不用下载Autohotkey在其他电脑上运行了;


五、设置开机自启动脚本

在系统启动文件夹C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup内粘贴 生成的autohotkey.exe文件

image-20230417005044206