Param.ahk for fenrir

前々から書こうと思ってはやめ思ってはやめを繰り返していたがなんとなく書く事にした
fenrirで引数を渡すためのもの


fenrir上でパスを選択すれば""で囲んでCUIに渡す


相対パスは引数を渡すターゲットの親フォルダを基準とする


引数ではパスを""で囲んでいればワイルドカードが使用できる
複数ヒットすれば半角スペースで区切り列挙


"ワイルドカードを含むPath;" で、複数ヒットした場合に列挙せずに、ヒットしたパスをひとつずつ代入して処理
".\hoge\*.txt;" ".\hoge\*.ini;"
のように2つ以上の場合は、これらを一つのパスとみなして、ヒットしたパスをひとつずつ代入して処理
2つ以上書く場合は半角スペースで区切り列挙しないとErrorを吐く
"wildcard;" でヒットした一つのパスを表す


/ahkmax | /ahkmin | /ahkhide でAutohotkeyのRunオプションMax | Min | Hide


区切り文字を指定してコマンドを区切れる デフォルトは |


IME_GetConverting()はIME.ahkのものを使用





#SingleInstance Force
#NoEnv
SetBatchLines, -1
ListLines, Off

;Option
x = 695
y = 515
Weight = 524
Height = 20
ParseString = |
;OptionExit


Count = %0%
Loop % Count ? Count : 1
	ParamCui( %A_Index%)
ExitApp

#If WinActive("ahk_class AutoHotkeyGUI") && !IME_GetConverting()
	Enter:: RunwithParam()
	Tab:: WinActivate, ahk_class fenrirMainWindow
	Del:: EditDelString("ahk_class AutoHotkeyGUI", "Edit1")


#If WinActive("ahk_class fenrirMainWindow") && !IME_GetConverting()
	Enter:: EditPasteSelectedPath("ahk_class AutoHotkeyGUI", "Edit1")
	Tab:: WinActivate, ahk_class AutoHotkeyGUI
	Del:: EditDelString("ahk_class AutoHotkeyGUI", "Edit1")


#If WinExist("ahk_class fenrirListWindow") && !IME_GetConverting()	;fenrirのフォルダ、親フォルダ展開キーを指定
	Left::
	Right::
		Send, {%A_ThisHotkey%}
		ControlGet, List, List,, SysListView321, ahk_class fenrirListWindow
		Return



ParamCui( Path) {
	global x, y, Weight, Height

	Gui, Margin, -1 0, 0
	Gui, +AlwaysOnTop +ToolWindow
	Gui, Add, Edit, W%Weight% H%Height%, % Path ? """" Path """" A_Space :
	Gui, Show, X%x% Y%y% W%Weight% H%Height%
	Pause, On
	Gui, Destroy
	Return

	GuiClose:
	GuiEscape:
		ExitApp
}


RunwithParam() {
	global ParseString

	WinGetText, Text, A
	Text := RTrim( Text, "`r`n")
	Loop, Parse, Text, % ParseString
	{	Param := Trim( A_LoopField)
		Target := RegExMatch( Param, "(?<=^\x22)[^\x22]*?(?=\x22)|(?<=^)[^\s\x22]*?(?=\s|$)", $) ? $ : Param
		SetWorkingDir, % RegExReplace( Target, "\\[^\\]*$")
		If !Target || RegExMatch( Target, "[*?]") && FileExist( RegExReplace( $, "\\[^\\]*$"))
			ErrorCode := """illegal Characters in Target"""
		While RegExMatch( Param, "(?<=/ahk)max(?=\s|$)|(?<=/ahk)min(?=\s|$)|(?<=/ahk)hide(?=\s|$)", $) {
			IfGreaterOrEqual, A_Index, 2, {
				ErrorMessage := RegExReplace( ErrorMessage ? ErrorMessage : Text, "/ahk" $, "<$0>")
				ErrorCode := ErrorCode ? ErrorCode A_Space "&&" A_Space """/ahkCommand""" : """/ahkCommand"""
			} Option := $
			Param := RegExReplace( Param, "/ahk" $ "\s*?")
		} While RegExMatch( Param, "(?<=\x22)[^\x22]*?[*?][^\x22]*?(?=;\x22)", $, Pos1 ? Pos1 : 1) {
			If !FileExist( RegExReplace( $, "\\[^\\]*$")) {
				Pos1 := InStr( Param, """" $ ";" """") + StrLen( """" $ ";" """")
				Continue
			} If ErrorCheck {
				If RegExMatch( Param, "\x22wildcard;\x22\s\x22" RegExReplace( $, "([.*?+\[\]{}|()^$\\])", "\$1") ";\x22")
					Loop, % $, 1, 0
						FilePaths .= """" A_LoopFileFullPath """" ","
				Else {
					ErrorMessage := RegExReplace( ErrorMessage ? ErrorMessage : Text, "\x22" RegExReplace( $, "([.*?+\[\]{}|()^$\\])", "\$1") ";\x22", "<$0>")
					ErrorCode := ErrorCode ? ErrorCode A_Space "&&" A_Space """wildcard;Command""" : """wildcard;Command"""
				} StringReplace, Param, Param, % A_Space """" $ ";" """"
			} Else {
				Loop, % $, 1, 0
					FilePaths .= """" A_LoopFileFullPath """" ","
				Pos1 := InStr( Param, """" $ ";" """") + StrLen("""widlcard;""")
				StringReplace, Param, Param, % """" $ ";" """", "wildcard;"
				ErrorCheck := True
			}
		} If ErrorCode {
			MsgBox, 0x40000, ParamError, % ErrorCode "`r`n" ErrorMessage
			Return
		} Else {
			While RegExMatch( Param, "(?<=\x22)[^\x22]*?[*?][^\x22]*?(?=\x22)", $, Pos2 ? Pos2 : 1) {
				If !FileExist( RegExReplace( $, "\\[^\\]*$")) {
					Pos2 := InStr( Param, """" $ """") + StrLen( """" $ """")
					Continue
				} Loop, % $, 1, 0
					TempFilePaths .= """" A_LoopFileFullPath """" A_Space
				Pos2 := InStr( Param, """" $ """") + StrLen( TempFilePath)
				StringReplace, Param, Param, % """" $ """", % RTrim( TempFilePaths)
				TempFilePaths :=
			} If FilePaths {
				FilePaths := RTrim( FilePaths, ",")
				Loop, Parse, FilePaths, CSV
					Run, % RegExReplace( Param, "\x22wildcard;\x22", A_LoopField), % WorkingDir, % Option "UseErrorLevel"
				FilePaths :=
			} Else Run, % Param, % WorkingDir, % Option "UseErrorLevel"
			If ErrorLevel {
				MsgBox, 0x40000, TargetError, % """NotFound""" "`r`n" Target
				Return
			}
		}
	} Pause, Off
	Return
}
EditPasteSelectedPath( class, edit) {
	global list

	ControlGet, SelectedCol, List, Selected, SysListView321, ahk_class fenrirListWindow
	SelectedPath := Instr( SelectedCol, ":") ? SelectedCol : RegExReplace( List, "(.*?)`n.*", "$1") SelectedCol
	Control, EditPaste, % SelectedPath ? """" RTrim( SelectedPath, "\") """" A_Space : , % edit, % class
	Return
}

EditDelString( class, edit) {
	WinGetText, String, % class
	String := RegExReplace( String, "\x22[^\x22]*?\x22\s*$|\s*[^\s\x22]*?\s*$")
	ControlSetText, % edit,, % class
	Control, EditPaste, % String, % edit, % class
	Return
}

IME_GetConverting(WinTitle="A",ConvCls="",CandCls="") {

    ;IME毎の 入力窓/候補窓Class一覧 ("|" 区切りで適当に足してけばOK)
    ConvCls .= (ConvCls ? "|" : "")                 ;--- 入力窓 ---
            .  "ATOK\d+CompStr"                     ; ATOK系
            .  "|imejpstcnv\d+"                     ; MS-IME系
            .  "|WXGIMEConv"                        ; WXG
            .  "|SKKIME\d+\.*\d+UCompStr"           ; SKKIME Unicode
            .  "|MSCTFIME Composition"              ; Google日本語入力

    CandCls .= (CandCls ? "|" : "")                 ;--- 候補窓 ---
            .  "ATOK\d+Cand"                        ; ATOK系
            .  "|imejpstCandList\d+|imejpstcand\d+" ; MS-IME 2002(8.1)XP付属
            .  "|mscandui\d+\.candidate"            ; MS Office IME-2007
            .  "|WXGIMECand"                        ; WXG
            .  "|SKKIME\d+\.*\d+UCand"              ; SKKIME Unicode
   CandGCls := "GoogleJapaneseInputCandidateWindow" ;Google日本語入力

    VarSetCapacity(stGTI, 48, 0)
    NumPut(48, stGTI,  0, "UInt")   ;	DWORD   cbSize;
	hwndFocus := DllCall("GetGUIThreadInfo", Uint,0, Uint,&stGTI)
	             ? NumGet(stGTI,12,"UInt") : WinExist(WinTitle)

    WinGet, pid, PID,% "ahk_id " hwndFocus
    tmm:=A_TitleMatchMode
    SetTitleMatchMode, RegEx
    ret := WinExist("ahk_class " . CandCls . " ahk_pid " pid) ? 2
        :  WinExist("ahk_class " . CandGCls                 ) ? 2
        :  WinExist("ahk_class " . ConvCls . " ahk_pid " pid) ? 1
        :  0
    SetTitleMatchMode, %tmm%
    return ret
}