PPx + Autohotkey

PPcでファイル操作をするとコメントにその操作を書いてくれるAutohotkeyスクリプト
といっても常駐ホットキーなんだけど
現在はPPcのコマンドで R, K, \K, U, M, \M, C, \C, \D, ファイルオペレーションのDeleteのみ
Deleteはファイルが削除されればコメントを消す
DllCallなんぞわかりません
三項演算子を最近少し覚え、嬉しくなったので無理やりにでもところどころに使用中
IME_GetConverting()はIME.ahkのものを使用


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


UnpackFirstWaitTime = 10
	;Unpackで最初の展開ダイアログが表示されるまで待つ時間
DialogWaitTime = 2
	;U, C, M, \Dコマンドでダイアログが表示されるまで待つ時間
EnvMult, OperationWaitTime, 1000
GroupAdd, PPc, ahk_class PaperPlaneCUIW
GroupAdd, PPc, ahk_class PaperPlaneCombo
Return


#If WinActive("File Operation Options ahk_class PPfileW") && !IME_GetConverting()
Enter:: FileOperation("")
#If

#If WinActive("名前変更 ahk_class #32770") && Control("Edit1|Edit2|Button1|Button2|Button3") && !IME_GetConverting()
Enter:: FileOperation("Rename")
#If

#if WinActive("ディレクトリ作成 ahk_class #32770") && Control("Edit1|Button1|Button2|Button3") && !IME_GetConverting()
Enter:: FileOperation("Makedir")
#If

#if WinActive("ファイル作成 ahk_class #32770") && Control("Edit1|Button1|Button2|Button3") && !IME_GetConverting()
Enter:: FileOperation("Makefile")
#If

#if WinActive("Unpack ahk_class #32770") && Control("Edit1|Button1|Button2|Button3") && !IME_GetConverting()
Enter:: FileOperation("Unpack")
#If

#If WinActive("Execute ahk_class #32770") && Control("Edit1|Button1|Button2|Button3") && !IME_GetConverting()
Enter:: FileOperation("Execute")
#If

#If WinActive("Move ahk_class #32770") && Control("Edit1|Button1|Button2|Button3") && !IME_GetConverting()
Enter:: FileOperation("Move")
#If

#If WinActive("Copy ahk_class #32770") && Control("Edit1|Button1|Button2|Button3") && !IME_GetConverting()
Enter:: FileOperation("Copy")
#If

#If WinActive("エントリ削除") && Control("Static1|Static2|Button1|Button2") && !IME_GetConverting()
Enter:: FileOperation("Delete")
#If



							Control(ControlList){
	WinGet, CtrlList, ControlList
	StringReplace, CtrlList, CtrlList, `n, |, 1
	Return InStr(CtrlList, ControlList)
}



							GetCurrentDir(Option){
							;%1を渡すとPPcカレントディレクトリ、%2を渡すと反対窓PPcのカレントディレクトリを返す
	WinGetTitle, Title, ahk_group PPc
	If Option = `%2
	{	WinGetTitle, OppTitle, ahk_group PPc,, %Title%
		If !OppTitle
		{	ControlGetText, OppText, PaperPlaneCUIW1, ahk_group PPc
			IfEqual, Title, %OppText%, ControlGetText, OppText, PaperPlaneCUIW2, ahk_group PPc
		}
	} Return % Option = `%1? RegExReplace( Title, "PPC\[\D\]")
	: OppText ? RegExReplace( OppText, "PPC\[\D\]") : RegExReplace( OppTitle, "PPC\[\D\]")
}



							ConvertPath(InputPath, CurrentDir){
							;引数が絶対パスならそのまま、文字列なら相対パス等に変えて
							;そのパスから存在するフォルダまで遡りそのパスを返す
	InputPath := RegExReplace( InputPath, "\\$")
	OutputPath := FileExist( InputPath) ? InputPath : InputPath = "#0:" ? A_Desktop
	: InputPath = "-#5" ? A_MyDocuments : InStr( InputPath, `%TEMP`%) ? RegExReplace( InputPath, "i )" `%TEMP`%, A_TEMP)
	: FileExist( CurrentDir "\" InputPath) ? CurrentDir "\" InputPath : ""
	While !OutputPath
	{	SplitPath, InputPath,, InputPath
		OutputPath := !InputPath ? CurrentDir : InStr( InputPath, ":") && FileExist( InputPath) ? InputPath
		: FileExist( CurrentDir "\" InputPath) ? CurrentDir "\" InputPath : ""
	} Return OutputPath
}




							FileOperation(Label){
	global OperationWaitTime, CloseWaitTime, UnpackFirstWaitTime, DialogWaitTime

	WinGet, ID,, A
	WinGetText, Text, ahk_id %ID%
	Loop, Parse, text, `r`n
	{	OperationArray0++
		OperationArray%A_Index% := A_LoopField
	} CurrentDir := GetCurrentdir("%1")
	Goto, % Label ? Label : OperationArray1 = "Act&ion: Move" ? "Move"
	: OperationArray1 = "Act&ion: Copy" ? "Copy" : "Delete"
	Return




	Rename:
	Send, {Enter}
	WinWaitClose, ahk_id %ID%,, %CloseWaitTime%
	If ErrorLevel || OperationArray0 = 9
		Return
	FileRead, OldComment, %CurrentDir%\00_INDEX.txt
	Comment := FileExist( CurrentDir "\00_INDEX.txt") ? "" : """" "00_INDEX.txt" """" A_Tab "CommentFile`r`n"
	Comment .= RegExReplace( OldComment, """" RegExReplace( OperationArray1, "([.*?+\[\]{}|()^$\\])", "\$1") """" "\t(.*)`r`n")
	. """" OperationArray3 """" A_Tab "*Rename`r`n"
	FileDelete, %CurrentDir%\00_INDEX.txt
	FileAppend, %Comment%, %CurrentDir%\00_INDEX.txt
	Return



	Makedir:
	Send, {Enter}
	WinWaitClose, ahk_id %ID%,, %CloseWaitTime%
	If ErrorLevel || OperationArray0 = 7
		Return
	Comment := FileExist( CurrentDir "\00_INDEX.txt") ? "" : """" "00_INDEX.txt" """" A_Tab "CommentFile`r`n"
	Comment .= """" . OperationArray1 . """" . A_Tab . "*Makedir`r`n"
	FileAppend, %Comment%, %CurrentDir%\00_INDEX.txt
	Return



	MakeFile:
	Send, {Enter}
	WinWaitClose, ahk_id %ID%,, %CloseWaitTime%
	If ErrorLevel || OperationArray0 = 7
		Return
	Comment := FileExist( CurrentDir "\00_INDEX.txt") ? "" : """" "00_INDEX.txt" """" A_Tab "CommentFile`r`n"
	Comment .= """" . OperationArray1 . """" . A_Tab . "*MakeFile`r`n"
	FileAppend, %Comment%, %CurrentDir%\00_INDEX.txt
	Return



	Unpack:
	DestDir := ConvertPath( OperationArray1, CurrentDir)
	Loop, %DestDir%\*, 1
		FileList .= """" A_LoopFileName """"
	Gosub, ExitWait
	Comment := FileExist( DestDir "\00_INDEX.txt") ? "" : """" "00_INDEX.txt" """" A_Tab "CommentFile`r`n"
	Loop, %DestDir%\*, 1
		IfNotInString, FileList, "%A_LoopFileName%"
			Comment .= """" . A_LoopFileName . """" . A_Tab . "*Unpack`r`n"
	FileAppend, %Comment%, %DestDir%\00_INDEX.txt
	Return



	Execute:
	DestDir := ConvertPath( OperationArray1, CurrentDir)
	If DestDir = %CurrentDir%
	{	SplitPath, CurrentDir,, DestDir
		ControlSetText, Edit1, %DestDir%, A
	} Loop, %DestDir%\*, 1
		FileList .= """" A_LoopFileName """"
	Gosub, ExitWait
	Comment := FileExist( DestDir "\00_INDEX.txt") ? "" : """" "00_INDEX.txt" """" A_Tab "CommentFile`r`n"
	Loop, %DestDir%\*, 1
		IfNotInString, FileList, "%A_LoopFileName%"
			Comment .= """" . A_LoopFileName . """" . A_Tab . "*Execute`r`n"
	FileAppend, %Comment%, %DestDir%\00_INDEX.txt
	Return



	Move:
	DestDir := Label ? ConvertPath( OperationArray1, CurrentDir) : ConvertPath( OperationArray33, CurrentDir)
	IfEqual, DestDir, %CurrentDir%, Return
	Loop, %CurrentDir%\*, 1
		FileList .= A_LoopFileName "`r`n"
	Gosub, % Label ? "ExitWait" : "OperationExitWait"
	FileRead, SourceComment, %CurrentDir%\00_INDEX.txt
	Comment := FileExist( DestDir "\00_INDEX.txt") ? "" : """" "00_INDEX.txt" """" A_Tab "CommentFile`r`n"
	Loop, Parse, FileList, `r`n
		If A_LoopField && FileExist( DestDir . "\" . A_LoopField) && !FileExist( CurrentDir "\" A_LoopField)
			{	Comment .= """" A_LoopField """" A_Tab "*Move`r`n"
				SourceComment := RegExReplace( SourceComment, """" RegExReplace( A_LoopField, "([.*?+\[\]{}|()^$\\])", "\$1") """" "\t(.*)`r`n")
			}
	FileDelete, %CurrentDir%\00_INDEX.txt
	If SourceComment && SourceComment != """" . "00_INDEX.txt" . """" . A_Tab . "CommentFile`r`n"
		FileAppend, %SourceComment%, %CurrentDir%\00_INDEX.txt
	FileAppend, %Comment%, %DestDir%\00_INDEX.txt
	Return
	
	
	
	Copy:
	DestDir := Label ? ConvertPath( OperationArray1, CurrentDir) : ConvertPath( OperationArray33, CurrentDir)
	IfEqual, DestDir, %CurrentDir%, Return
	Loop, %DestDir%\*, 1
		FileList .= """" A_LoopFileName """" "`r`n"
	Gosub, % Label ? "ExitWait" : "OperationExitWait"
	Comment := FileExist( DestDir "\00_INDEX.txt") ? "" : """" "00_INDEX.txt" """" A_Tab "CommentFile`r`n"
	Loop, %DestDir%\*, 1
		IfNotInString, FileList, "%A_LoopFileName%"
		{	FileGetSize, Copysize, %DestDir%\%A_LoopFileName%
			FileGetSize, SourceSize, %CurrentDir%\%A_LoopFileName%
			If Copysize = %Sourcesize%
				Comment .= """" A_LoopFileName """" A_Tab "*Copy`r`n"
		}
	FileAppend, %Comment%, %DestDir%\00_INDEX.txt
	Return
	
	
	
	Delete:
	Gosub, % Label ? "ExitWait" : "OperationExitWait"
	Loop, Read, %CurrentDir%\00_INDEX.txt
		Comment .= FileExist( CurrentDir "\" RegExReplace( A_LoopReadLine,  """" "(.*)" """" "\t(.*)", "$1")) ? A_LoopReadLine "`r`n" : ""
	FileDelete, %CurrentDir%\00_INDEX.txt
	If Comment && Comment != """" "00_INDEX.txt" """" A_Tab "CommentFile`r`n"
		FileAppend, %Comment%, %CurrentDir%\00_INDEX.txt
	Return



	OperationExitWait:
	Send, {Enter}
	WinGetTitle, Title, ahk_id %ID%
	IfEqual, Title, File Operation Options, Exit
	WinWaitClose, ahk_id %ID%
	Return



	ExitWait:
	Send, {Enter}
	First := True
	Loop {
		WinWait, ahk_class #32770,, % Label = "Unpack" && First ? UnpackFirstWaitTime : DialogWaitTime, %ExTitle%
		IfEqual, ErrorLevel, 1, Return
		If % Label  = "Unpack" || Label  = "Execute"
		? Control("Button1|Button2|SysListView321|SysHeader321|Static1|Static2|Static3|Static4|msctls_progress321|Static5")
		|| Control("Static1|Static2|Static3|Static4|Static5|Static6|Static7|msctls_progress321|Button1")
		: Control("SysAnimate321|Static1|Static2|msctls_progress321|Static3|Button1")
			{	WinWaitClose
				IfNotEqual, Label, Unpack, Return
				First := False
			} Else {
				WinGetTitle, Title
				ExTitle .= Title A_Space
			}
	} Return
	
}