Skip to content

Instantly share code, notes, and snippets.

@tmplinshi
Last active August 29, 2015 14:12
Show Gist options
  • Save tmplinshi/47e1f6c96c44317f0a23 to your computer and use it in GitHub Desktop.
Save tmplinshi/47e1f6c96c44317f0a23 to your computer and use it in GitHub Desktop.
#IfWinActive, ahk_class PX_WINDOW_CLASS ; Sublime Text 窗口
F5::st_ahk.run()
^F5::st_ahk.compile()
; F2::WinMenuSelectItem,,, 6&, 10&, 2&
#If
; 功能: 运行/编译在 Sublime Text 中打开的 ahk 文件。
;
; * 编译的时候,会自动将需要的 ahk 文件复制到“脚本目录\Lib\”。
; (注:不会覆盖旧文件)
;
; * 可在 ahk 文件(开头)加入 Ini 配置:
;
; /*
; [config]
;
; ; 用 Unicode 或者 ANSI 来运行/编译,不设置则用默认的。
; ahk_version=Unicode
;
; ; 编译用的图标文件、bin文件
; icon=
; bin=
; */
;
Class st_ahk
{
static Init := st_ahk.Init()
Run() {
this.GetFilePath(fPath, fDir)
IniRead, ahk_version, % fPath, config, ahk_version, % A_Space
exe_ahk := (ahk_version ~= "i)ANSI|Unicode") ? this.exe[ahk_version] : A_AhkPath
Run, % exe_ahk A_Space """" fPath """", % fDir
}
Compile() {
this.GetFilePath(fPath, fDir)
IniRead, ahk_version, % fPath, config, ahk_version, % A_Space
IniRead, bin , % fPath, config, bin , % A_Space
IniRead, icon , % fPath, config, icon , % A_Space
If bin
args_bin := " /bin " """" bin """"
Else If (ahk_version ~= "i)ANSI|Unicode")
args_bin := " /bin " """" A_AhkPath "\..\Compiler\" ahk_version " 32-bit.bin" """"
If icon
args_icon := " /icon " """" icon """"
ToolTip, 编译中...
RunWait, % this.exe["ahk2exe"] " /in """ fPath """" args_icon args_bin, % fDir
this.CopyLib(fPath)
ToolTip
MsgBox, 36, 运行已编译文件, 编译完成。是否运行?
IfMsgBox Yes
Run, % SubStr(fPath, 1, -3) "exe", % fDir
}
CopyLib(ScriptPath) {
SplitPath, ScriptPath,, ScriptDir
; ------------- 复制 #Include <> 中的文件
FileRead, ScriptContent, % ScriptPath
If RegExMatch(ScriptContent, "m`ai)^\s*#Include[\s,]*<.*?>") {
IfNotExist, % ScriptDir "\Lib"
FileCreateDir, % ScriptDir "\Lib"
Pos := 1
While ( Pos := RegExMatch(ScriptContent, "m`ai)^\s*#Include[,\s]*<\K[^>]+", Match, Pos + StrLen(Match)) )
FileCopy, % A_AhkPath "\..\Lib\" Match ".ahk", % ScriptDir "\Lib\"
}
; ------------- 复制没有使用 #Include 的文件
tempFile := A_Temp "\.ahkLib.temp"
RunWait, % A_AhkPath " /iLib " """" tempFile """ " """" ScriptPath """"
FileRead, content, % tempFile
If RegExMatch(content, "im`a)\.ahk$") {
IfNotExist, % ScriptDir "\Lib"
FileCreateDir, % ScriptDir "\Lib"
Loop, Read, % tempFile
{
ahkFile := RegExReplace(A_LoopReadLine, "^.*? ")
If ( SubStr(ahkFile, 0) != "\" )
FileCopy, % ahkFile, % ScriptDir "\Lib\"
}
}
}
GetFilePath(ByRef fPath, ByRef fDir) {
WinGetTitle, title, A
fPath := RegExReplace(title, " - Sublime Text \d?( ?\(UNREGISTERED\))?$")
fDir := RegExReplace(title, "\\[^\\]*$")
}
Init() {
SplitPath, A_AhkPath,, ahkDir
this.exe := { ANSI : ahkDir "\AutoHotkeyA32.exe"
, Unicode: ahkDir "\AutoHotkeyU32.exe"
, ahk2exe: ahkDir "\Compiler\Ahk2Exe.exe" }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment