st_ahk - 运行/编译在 Sublime Text 中打开的 ahk 文件

许多实用脚本和封装函数, 可以让您编写脚本更加便捷高效

Moderators: tmplinshi, arcticir

tmplinshi
Posts: 1604
Joined: 01 Oct 2013, 14:57

st_ahk - 运行/编译在 Sublime Text 中打开的 ahk 文件

07 Jan 2015, 02:06

Code: Select all

#IfWinActive, ahk_class PX_WINDOW_CLASS ; Sublime Text 窗口
	F5::st_ahk.run() ; 按 F5 运行
	^F5::st_ahk.compile() ; 按 Ctrl+F5 编译
#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" }
	}
}
https://gist.github.com/tmplinshi/47e1f6c96c44317f0a23

Return to “脚本函数”

Who is online

Users browsing this forum: No registered users and 7 guests