How to directly preview a file programlly?

English support forum

Moderators: white, Hacker, petermad, Stefan2

Post Reply
valuex
Junior Member
Junior Member
Posts: 25
Joined: 2014-12-25, 13:53 UTC

How to directly preview a file programlly?

Post by *valuex »

From link1, it is possible to place cursor on a desired file, although not very robust right now.
The reason I would like to use the script actually is to preview the desired file using TC quick view.
So I am wondering is there any command that can be used to direclty preview a file, while no need to place the cursor on the file first.


link1:
viewtopic.php?t=78299
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: How to directly preview a file programlly?

Post by *beb »

Assuming that you have a filename of a file in question (that you want to be displayed in QuickView) in the clipboard, the following command will do the trick:
usercmd.ini:

Code: Select all

[em_test_tc_command2]
cmd=cm_LoadSelectionFromClip,cm_GoToFirstEntry,cm_GoToNextSelected,cm_ClearAll,cm_SrcQuickview
How it works (example):
- copy a file name of one of the files in the active pane in the clipboard,
- place the cursor somewhere else (on other file),
- activate em_test_tc_command2 (e.g. from button bar),
- then:
--- cm_LoadSelectionFromClip (the first command in the commands chain) will select the file, which name is in the clipboard,
--- cm_GoToFirstEntry,cm_GoToNextSelected (the second, and third commands in the commands chain) will place the cursor on the said file,
--- cm_ClearAll (the fourth command in the commands chain) will remove the selection,
--- cm_SrcQuickview (the last command in the commands chain) will show the contents of the said file in the Quickview mode.

Notes:
The first, second, and third commands in the chain have no other purpose than to move the cursor on the file in question (there is no other known way to do that; then the fourth command clears the selection, thus clearing the traces of the command chain except the cursor positioning; in fact, the end user doesn't see that those commands are there, the user is just getting the result they needed).
In its turn, such a construction is required since the cm_SrcQuickview works only with a file under the cursor and doesn't accept arguments (such as filename; otherwise the entire command could be much simpler).

Example button:

Code: Select all

TOTALCMD#BAR#DATA
em_test_tc_command2

WCMICON2.DLL


0
10028
Or you can use just a button (without a user command in usercmd.ini):

Code: Select all

TOTALCMD#BAR#DATA
cm_LoadSelectionFromClip,cm_GoToFirstEntry,cm_GoToNextSelected,cm_ClearAll,cm_SrcQuickview

WCMICON2.DLL


0
-1
Edit:
With a bit different approach to place the cursor on the file in question using em_cd proxy command (thanks to Fla$her):

Code: Select all

[em_cd_proxy]
cmd=cd
param=%S1\:

Code: Select all

[em_final_command]
cmd=cm_LoadSelectionFromClip,em_cd_proxy,cm_ClearAll,cm_SrcQuickview

Code: Select all

TOTALCMD#BAR#DATA
em_final_command

WCMICON2.DLL


0
-1
Note: as you can see, the command chain is native, no other tools/scripts are involved.
Last edited by beb on 2024-02-02, 07:27 UTC, edited 1 time in total.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: How to directly preview a file programlly?

Post by *Fla$her »

valuex wrote: 2024-02-01, 14:40 UTC is there any command that can be used to direclty preview a file, while no need to place the cursor on the file first.
No. For quick viewing, placing the cursor on the file is required. See the suggestions:
https://ghisler.ch/board/viewtopic.php?p=399570
https://ghisler.ch/board/viewtopic.php?p=414311
https://ghisler.ch/board/viewtopic.php?p=433591

2beb
Why such difficulties if there is CD command?

usercmd.ini:

Code: Select all

[em_cd]
cmd=cd
param=%Z%A
Command: em_cd "C:\Path To\File.ext\:",cm_SrcQuickview
Overquoting is evil! 👎
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: How to directly preview a file programlly?

Post by *beb »

2Fla$her
The cd command just wasn't working when I tested it in that scenario. I even suspected a bug, then installed the previous TC version, and it still didn't work for me, don't know why.
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: How to directly preview a file programlly?

Post by *Fla$her »

2beb
in that scenario
In which scenario exactly? Maybe you did something wrong?
Overquoting is evil! 👎
User avatar
beb
Senior Member
Senior Member
Posts: 435
Joined: 2009-09-20, 08:03 UTC
Location: Odesa, Ukraine

Re: How to directly preview a file programlly?

Post by *beb »

2Fla$her
Because I tried it directly and it didn't work that way.
With the proxy em_cd it does work, though:

Code: Select all

[em_cd_proxy]
cmd=cd
param=%S1\:

Code: Select all

[em_final_command]
cmd=cm_LoadSelectionFromClip,em_cd_proxy,cm_ClearAll,cm_SrcQuickview
#278521 User License
Total Commander [always the latest version, including betas] x86/x64 on Win10 x64/Android 10
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: How to directly preview a file programlly?

Post by *Fla$her »

beb wrote: 2024-02-02, 07:21 UTC Because I tried it directly
That's what I assumed.
beb wrote: 2024-02-02, 07:21 UTC With the proxy em_cd it does work, though:
Then I'm even more perplexed. What is %S1, cm_LoadSelectionFromClip and cm_ClearAll for? :shock:
Overquoting is evil! 👎
valuex
Junior Member
Junior Member
Posts: 25
Joined: 2014-12-25, 13:53 UTC

Re: How to directly preview a file programlly?

Post by *valuex »

Thanks for your reply.
CD command is really a little bit tricky. :D
This is the autohotkey V2 script that can focus on the desired file. (Open preview panel in TC first)

Code: Select all

F1::
{
    ThisFile:="D:\xxx\xxx.mhtml"

    newPath:=ThisFile . "\:`r" . "\0"
    SendMessage(0x433,4001,0,,"ahk_class TTOTAL_CMD")   ; FocusLeft
    SendMessage(0x433,3001,0,,"ahk_class TTOTAL_CMD")   ; NewTab
    TC_SetPath(newPath)                                 ; CD to place cursor on file
}
TC_SetPath(userCommand) 
{
    ; https://www.autohotkey.com/boards/viewtopic.php?p=538463&sid=4471e03917209854441ac07ebdc70901#p538463
    static dwData := 17475  ;;Ord("C") +256*Ord("D")
    static WM_COPYDATA := 0x4A
    cbData := Buffer(StrPut(userCommand, 'CP0'))
    StrPut(userCommand, cbData, 'CP0')
    COPYDATASTRUCT := Buffer(A_PtrSize * 3)
    NumPut('Ptr', dwData, 'Ptr', cbData.size, 'Ptr', cbData.ptr, COPYDATASTRUCT)
    MsgResult:=SendMessage( WM_COPYDATA,, COPYDATASTRUCT,, 'ahk_class TTOTAL_CMD')
    return MsgResult
}

However, the VBA version (64bit) does not work. Any help will be appricated.

Code: Select all

Private Declare PtrSafe Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As LongPtr
Private Declare PtrSafe Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As LongPtr, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As LongPtr

Public Type CopyDataStruct
    dwData As LongPtr
    cbData As LongPtr
    lpData As LongPtr
End Type
Private Const WM_COPYDATA = &H4A
Sub test()
    Dim newPath As String
    ThisFile = "D:\xxx\xxx.mhtml"
    newPath = ThisFile & "\:\n" & "\0"
    hwndTC = FindWindow("TTOTAL_CMD", vbNullString)
    result = SendMessage(hwndTC, 1075, 4001, 0)  'FocusLeft
     result = SendMessage(hwndTC, 1075, 3001, 0)  'NewTab
    
    Call TC_SetPath(hwndTC, newPath)  'CD to place cursor on file
End Sub

Sub TC_SetPath(hwndTarget, UserCMD As String)
    Dim cds As CopyDataStruct
    
    cds.dwData = Asc("C") + 256 * Asc("D")
    cds.cbData = Len(UserCMD) + 1
    cds.lpData = StrPtr(UserCMD)
    
    ' Send the WM_COPYDATA message
    result = SendMessage(hwndTarget, WM_COPYDATA, 0, cds)
End Sub

valuex
Junior Member
Junior Member
Posts: 25
Joined: 2014-12-25, 13:53 UTC

Re: How to directly preview a file programlly?

Post by *valuex »

Fla$her wrote: 2024-02-01, 23:14 UTC Command: em_cd "C:\Path To\File.ext\:",cm_SrcQuickview
It is robust to place the cursor on the desired file.
Using this method, the origin application who send the message will lose focus.
I got to switch back the focus, which is not good for continous preview.

So if possible, please kindly consider to allow sendmessage to preview file without having to activate TC.
Thanks.
joe910
Junior Member
Junior Member
Posts: 37
Joined: 2009-02-23, 03:05 UTC
Location: Peoria, IL USA

Re: How to directly preview a file programlly?

Post by *joe910 »

Will using cm_SeparateQuickView make any difference?
Fla$her
Power Member
Power Member
Posts: 2318
Joined: 2020-01-18, 04:03 UTC

Re: How to directly preview a file programlly?

Post by *Fla$her »

valuex wrote: 2024-02-04, 03:22 UTC So if possible, please kindly consider to allow sendmessage to preview file without having to activate TC.
I've given you three topics where you can join with support.
valuex wrote: 2024-02-04, 03:22 UTC Will using cm_SeparateQuickView make any difference?
No. This is a minimized analogue of F3, with the only difference being that it can be closed by the same command from the main TC window.
Overquoting is evil! 👎
Post Reply