AutoHotkey: AppendLnk option for packing (plus: Automatically assign name for archive): Difference between revisions

From TotalcmdWiki
Jump to navigation Jump to search
((v1.02 - restore clipboard contents after use/on exit))
(v1.03 - speeded up input (directly to TC's controls); fixed: selected dirs weren't recognized)
Line 8: Line 8:
:- Gets invoked with Alt-F5, replacing TC's standard name suggestion.
:- Gets invoked with Alt-F5, replacing TC's standard name suggestion.
:- The originally suggested name can be restored by pressing Alt-Backspace once, if needed.
:- The originally suggested name can be restored by pressing Alt-Backspace once, if needed.
:Successfully tested with TC 7.02a, 6.57, 6.03a.


<pre>
<pre>
Line 18: Line 20:
Related links:
Related links:
*[http://ghisler.ch/board/viewtopic.php?p=120095#120095 TC Forum discussion]
*[http://ghisler.ch/board/viewtopic.php?p=120095#120095 TC Forum discussion]
*[[AutoHotkey:_Automatically_assign_name_for_archive|TCWiki: AHK: Automatically_assign_name_for_archive]]
*[[AutoHotkey:_Automatically_assign_name_for_archive|TCWiki: AHK: Automatically_assign_name_for_archive]] (related project)




Line 24: Line 26:
*/ ; --> <code><pre>
*/ ; --> <code><pre>
; ////////////////////////////////////////////////////////////////////////////
; ////////////////////////////////////////////////////////////////////////////
; // TC_AppendLnkPack.ahk v1.02 -  (W) StatusQuo 2008     (for TC 7.0 and up)
; // TC_AppendLnkPack.ahk v1.03 -  (W) StatusQuo 2008
; //
; //
; // Context sensitive extension of TC's suggested name in packing dialog (Alt-F5),
; // Context sensitive extension of TC's suggested name in packing dialog (Alt-F5),
Line 45: Line 47:
#IfWinActive ahk_class TTOTAL_CMD
#IfWinActive ahk_class TTOTAL_CMD
!F5::
!F5::
; BlockInput, On ; deactivated, because of little benefit and possible probs in Win98/ME
sClipSave = %Clipboard%
sClipSave = %Clipboard%
IfWinNotExist,  ahk_class TTOTAL_CMD
IfWinNotExist,  ahk_class TTOTAL_CMD
Goto, lExit
Goto, lExit
IfWinNotActive, ahk_class TTOTAL_CMD, , WinActivate, ahk_class TTOTAL_CMD,
IfWinNotActive, ahk_class TTOTAL_CMD,, WinActivate, ahk_class TTOTAL_CMD
WinWaitActive,  ahk_class TTOTAL_CMD
WinWaitActive,  ahk_class TTOTAL_CMD
; save active control name (side of active panel);
; save active control name (side of active panel);
Line 54: Line 57:
ControlGetFocus, sLastFocus, ahk_class TTOTAL_CMD
ControlGetFocus, sLastFocus, ahk_class TTOTAL_CMD
; check, if TC's main window is active
; check, if TC's main window is active
IfNotInString, sLastFocus, TMyListBox
IfNotInString, sLastFocus, TMyListBox, Goto, lExit
Goto, lExit
; 2017 - cm_CopyNamesToClip - copy selected file names to clipboard
; 2017 - cm_CopyNamesToClip - copy selected file names to clipboard
Line 63: Line 65:
if ErrorLevel
if ErrorLevel
{ Clipboard = %sClipSave%
{ Clipboard = %sClipSave%
MsgBox, 49, TimeOut - %A_ScriptName%, - TimeOut - `n`nCould not get selected file names from TC`n(cm_CopyToClipboard)., 7
BlockInput, Off
MsgBox, 49, TimeOut - %A_ScriptName%, - TimeOut - `n`nCould not get selected file names from TC`n(cm_CopyToClipboard).`n`nOK to use TC's name suggestion`,`nCancel to abort packing now., 5
IfMsgBox, Cancel,  Goto, lExit
IfMsgBox, Cancel,  Goto, lExit
IfMsgBox, Timeout, Goto, lExit
IfMsgBox, Timeout, Goto, lExit
Line 70: Line 73:
}
}
; parse selected file names
;- parse selected file names
iLines = 0
iLines = 0
boUseName = 1
boUseName = 1
Line 79: Line 82:
; cut path and extension
; cut path and extension
SplitPath, A_LoopField, FullFileName, , , FileNameNoExt
SplitPath, A_LoopField, FullFileName, , , FileNameNoExt
;- if no file name given, use dir name (this is the case with selected dirs)
if (FullFileName = "")
{ sTmp  = %A_LoopField%
if not    (SubStr(sTmp, StrLen(sTmp), 1            ) != "\") ; backslash after dir name? => kill!
sTmp := SubStr(sTmp, 1,            StrLen(sTmp)-1)
StringGetPos, iTmp, sTmp, \, R ; more backslashes in front of dir name? => kill!
if (iTmp >= 0) ; first position is nr. 0
sTmp := SubStr(sTmp, iTmp+2)
FullFileName  := sTmp
StringGetPos, iTmp, sTmp, ., R ; contained extension? => kill!
if (iTmp >= 0) ; first position is nr. 0
sTmp := SubStr(sTmp, 1, iTmp)
FileNameNoExt := sTmp
}


; use first name for comparison
;- use first name for comparison
if (sFirstName = "")  
if (sFirstName = "")
{ sFirstName    = %FileNameNoExt%
{ sFirstName    = %FileNameNoExt%
sFirstFullName = %FullFileName%
sFirstFullName = %FullFileName%
} else {
} else {
; check if there is a different file name
; check if there is a different file name
Line 93: Line 112:
}
}
; multiple files selected:
;- multiple files selected:
if (iLines > 1)
if (iLines > 1)
{ ControlSend,  TPanel1, !{F5}
{ ControlSend,  TPanel1, !{F5}
; different file names selected: => use dirname.zip
;- different file names selected: => use dirname.zip
; (change nothing at all, use TC's suggestion)
; (change nothing at all, use TC's suggestion)
IfEqual, boUseName, 0, Goto, lExit
IfEqual, boUseName, 0, Goto, lExit
; all files have same name:    => use filename.zip
;- no file name found for pasting, so exit
IfEqual, sFirstName,  , Goto, lError
;- all files have same name:    => use filename.zip
WinWait,      ahk_class TDLGZIP
WinWait,      ahk_class TDLGZIP
ControlSend,  TEdit1, !{F5}%sFirstName%{HOME}+{END}
; ControlSend,  TEdit1, !{F5}%sFirstName%{HOME}+{END}
ControlGetText, sFullPath, TEdit1
sFullPath := ExchangeFN(sFullPath, sFirstName)
ControlSetText, TEdit1, %sFullPath%
ControlSend,  TEdit1, {HOME}+{END}
Goto, lExit
Goto, lExit
}
}


; single file name selected:  => use filename.ext.zip
;- single file name selected:  => use filename.ext.zip
ControlSend,  TPanel1, !{F5}
ControlSend,  TPanel1, !{F5}
WinWait,      ahk_class TDLGZIP
WinWait,      ahk_class TDLGZIP
ControlSend,  TEdit1, !{F5}%sFirstFullName%{HOME}+{END}
; ControlSend,  TEdit1, !{F5}%sFirstFullName%{HOME}+{END}
ControlGetText, sFullPath, TEdit1
;- no file name found for pasting, so exit
IfEqual, sFirstFullName,  , Goto, lError
sFullPath := ExchangeFN(sFullPath, sFirstFullName)
ControlSetText, TEdit1, %sFullPath%
ControlSend,  TEdit1, {HOME}+{END}
lExit:
lExit:
BlockInput, Off
Clipboard = %sClipSave%
Clipboard = %sClipSave%
Return
Return
lError:
BlockInput, Off
MsgBox, Error getting file name(s)`n`n- sFullPath = %sFullPath%`n- sFirstFullName = %sFirstFullName%`n- sFirstName = %sFirstName%
Goto, lExit
#IfWinActive
#IfWinActive
ExchangeFN(sEfnFullPath, sNewFN)
{
StringGetPos,    i1, sEfnFullPath, \, R
StringGetPos,    i2, sEfnFullPath, ., R
StringLeft, sEfnOut, sEfnFullPath, i1
Stringmid,  sEfnExt, sEfnFullPath, (i2 + 1)
if StrLen(sEfnOut) > 0
sEfnOut = %sEfnOut%\
sEfnOut    = %sEfnOut%%sNewFN%%sEfnExt%
Return %sEfnOut%
}
;</pre></code>
;</pre></code>

Revision as of 00:08, 27 January 2008

Context sensitive extension of TC's suggested name in packing dialog (Alt-F5), emulating the missing AppendLnkPack option in TC up to 7.02a.

- Stays resident in memory, can be quit via its icon in the system tray.
- Gets invoked with Alt-F5, replacing TC's standard name suggestion.
- The originally suggested name can be restored by pressing Alt-Backspace once, if needed.
Successfully tested with TC 7.02a, 6.57, 6.03a.
; // Resulting archive name:
; //   - if only 1 file selected -------------------- :  zip:filename.ext.zip (*.*.zip)
; //   - if multiple files /      same name  selected :  zip:filename.zip     (  *.zip)
; //   - if multiple files / different names selected :  zip:dirname.zip (as suggested by TC)

Related links:


	; ////////////////////////////////////////////////////////////////////////////
	; // TC_AppendLnkPack.ahk v1.03  -  (W) StatusQuo 2008
	; //
	; // Context sensitive extension of TC's suggested name in packing dialog (Alt-F5),
	; // emulating the missing AppendLnkPack option,
	; // => http://ghisler.ch/board/viewtopic.php?p=120095#120095
	; //
	; // Option 2 ("multiple files, same name") inspired by
	; // http://www.ghisler.ch/wiki/index.php/AutoHotkey:_Automatically_assign_name_for_archive
	; //
	; ////////////////////////////////////////////////////////////////////////////
	; //
	; // Resulting archive name:
	; //   - only 1 file selected:            zip:filename.ext.zip
	; //   - multiple files, same name:       zip:filename.zip
	; //   - multiple files, different names: zip:dirname.zip (as suggested by TC)
	; //
	; ////////////////////////////////////////////////////////////////////////////

#SingleInstance force
#IfWinActive ahk_class TTOTAL_CMD
!F5::
	; BlockInput, On	; deactivated, because of little benefit and possible probs in Win98/ME
	sClipSave = %Clipboard%
	IfWinNotExist,  ahk_class TTOTAL_CMD
		Goto, lExit
	IfWinNotActive, ahk_class TTOTAL_CMD,, WinActivate, ahk_class TTOTAL_CMD
	WinWaitActive,  ahk_class TTOTAL_CMD
		; save active control name (side of active panel);
		; while TC is inactive in background, nothing has the focus!
	ControlGetFocus, sLastFocus, ahk_class TTOTAL_CMD
		; check, if TC's main window is active
	IfNotInString, sLastFocus, TMyListBox, Goto, lExit
	
		; 2017 - cm_CopyNamesToClip - copy selected file names to clipboard
	Clipboard=
	SendMessage, 1075, 2017
	ClipWait, 1
	if ErrorLevel
	{	Clipboard = %sClipSave%
		BlockInput, Off
		MsgBox, 49, TimeOut - %A_ScriptName%, - TimeOut - `n`nCould not get selected file names from TC`n(cm_CopyToClipboard).`n`nOK to use TC's name suggestion`,`nCancel to abort packing now., 5
		IfMsgBox, Cancel,  Goto, lExit
		IfMsgBox, Timeout, Goto, lExit
		ControlSend,  TPanel1, !{F5}
		Goto, lExit
	}
	
		;- parse selected file names
	 iLines = 0
	boUseName = 1
	 sFirstName =
	 sFirstFullName =
	Loop, Parse, Clipboard, `n, `r
	{	iLines += 1
			; cut path and extension
		SplitPath, A_LoopField, FullFileName, , , FileNameNoExt
		
			;- if no file name given, use dir name (this is the case with selected dirs)
		if (FullFileName = "")
		{	sTmp  = %A_LoopField%
			if not    (SubStr(sTmp, StrLen(sTmp), 1             ) != "\")	; backslash after dir name? => kill!
				sTmp := SubStr(sTmp, 1,            StrLen(sTmp)-1)
			StringGetPos, iTmp, sTmp, \, R	; more backslashes in front of dir name? => kill!
			if (iTmp >= 0)	; first position is nr. 0
				sTmp := SubStr(sTmp, iTmp+2)
			FullFileName  := sTmp

			StringGetPos, iTmp, sTmp, ., R	; contained extension? => kill!
			if (iTmp >= 0)	; first position is nr. 0
				sTmp := SubStr(sTmp, 1, iTmp)
			FileNameNoExt := sTmp
		}

			;- use first name for comparison
		if (sFirstName = "")
		{	sFirstName     = %FileNameNoExt%
			sFirstFullName = %FullFileName%
		} else {
				; check if there is a different file name
			if (FileNameNoExt <> sFirstName)
			{	boUseName = 0
				break
			}
		}
	}
	
		;- multiple files selected:
	if (iLines > 1)
	{	ControlSend,  TPanel1, !{F5}
			;- different file names selected: => use dirname.zip
			; (change nothing at all, use TC's suggestion)
		IfEqual, boUseName,  0, Goto, lExit
			;- no file name found for pasting, so exit
		IfEqual, sFirstName,  , Goto, lError
			;- all files have same name:     => use filename.zip
		WinWait,       ahk_class TDLGZIP
		; ControlSend,  TEdit1, !{F5}%sFirstName%{HOME}+{END}
		ControlGetText, sFullPath, TEdit1
		sFullPath := ExchangeFN(sFullPath, sFirstName)
		ControlSetText, TEdit1, %sFullPath%
		ControlSend,  TEdit1, {HOME}+{END}
		Goto, lExit
	}

		;- single file name selected:   => use filename.ext.zip
	ControlSend,  TPanel1, !{F5}
	WinWait,       ahk_class TDLGZIP
	; ControlSend,  TEdit1, !{F5}%sFirstFullName%{HOME}+{END}
	ControlGetText, sFullPath, TEdit1
		;- no file name found for pasting, so exit
	IfEqual, sFirstFullName,  , Goto, lError
	sFullPath := ExchangeFN(sFullPath, sFirstFullName)
	ControlSetText, TEdit1, %sFullPath%
	ControlSend,  TEdit1, {HOME}+{END}
lExit:
	BlockInput, Off
	Clipboard = %sClipSave%
	Return

lError:
	BlockInput, Off
	MsgBox, Error getting file name(s)`n`n- sFullPath = %sFullPath%`n- sFirstFullName = %sFirstFullName%`n- sFirstName = %sFirstName%
	Goto, lExit

#IfWinActive


ExchangeFN(sEfnFullPath, sNewFN)
{
	StringGetPos,    i1, sEfnFullPath, \, R
	StringGetPos,    i2, sEfnFullPath, ., R
	StringLeft, sEfnOut, sEfnFullPath, i1
	Stringmid,  sEfnExt, sEfnFullPath, (i2 + 1)
	if StrLen(sEfnOut) > 0
		sEfnOut = %sEfnOut%\
	sEfnOut    = %sEfnOut%%sNewFN%%sEfnExt%
	Return %sEfnOut%
}
;