media player classic - jump to point in video/audio programmatically - winapi

In Media Player Classic I found a way to jump to a point in a video/audio programmatically, avoiding the Go To... box.
The jump distances are available at Options → Tweaks,
and HKEY_CURRENT_USER\Software\MPC-HC\MPC-HC\Settings
(JumpDistL/JumpDistM/JumpDistS).
What I do is find the jump distances in the address space of Media Player Classic, and set the value of the large jump distance such
that if you applied it to the elapsed time you would get the desired time.
I then send a WM_COMMAND message with parameter 903/904 (all via AutoHotkey. I get the elapsed time by retrieving/parsing the contents of the Edit control.)
Because the jump is relative to the current point, it is imprecise,
and arrives within a second of the right time, but doesn't arrive
at exactly the same point each time.
Is there a more direct way of accomplishing this and if not,
would any Media Player Classic users/programmers
consider discussing on the forum, introducing new WM_COMMAND messages
that allow jump to point (in milliseconds),
or that retrieve the numerical values listed here
(state, position, duration, volumelevel, muted, playbackrate, reloadtime).
(The method found here is too slow to get the time accurately, and requires special options be set).

Thanks to the message from wOxxOm, below the question,
I have been able to create this AutoHotkey script,
which solves my original problem:
to set the elapsed time in Media Player Classic programmatically,
directly, without using the Go To... box.
It also solves the problem of retrieving
information about the video.
The hotkeys are:
- Ctrl+Q to start the MPC API,
- Ctrl+W to retrieve information,
- the number keys to jump partway through the video.
;==================================================
^q:: ;start MPC API
hWnd := A_ScriptHwnd+0
OnMessage(WM_COPYDATA:=74, "On_WM_COPYDATA")
;64-bit
Run, "C:\Program Files (x86)\K-Lite Codec Pack\MPC-HC64\mpc-hc64.exe" /slave %hWnd%
;32-bit
;Run, "C:\Program Files (x86)\K-Lite Codec Pack\MPC-HC\mpc-hc.exe" /slave %hWnd%
Return
;==================================================
^w:: ;display information
Send(vMPCApiHWnd, 0xA0003004, "") ;CMD_GETCURRENTPOSITION := 0xA0003004
vElapsed := 19990101
vDuration := 19990101
vElapsed += vMPCApiCurrent, S
vDuration += vMPCApiDuration, S
if (vMPCApiCurrent >= 3600) OR (vMPCApiDuration >= 3600)
vFormat := "HH:mm:ss"
else
vFormat := "mm:ss"
FormatTime, vElapsed, %vElapsed%, %vFormat%
FormatTime, vDuration, %vDuration%, %vFormat%
SplitPath, vMPCApiPath, vName, vDir, vExt, vNameNoExt, vDrive
vText = ;continuation section
(
title: %vMPCApiTitle%
author: %vMPCApiAuthor%
description: %vMPCApiDesc%
name: %vName%
path: %vMPCApiPath%
elapsed: %vElapsed% (%vMPCApiCurrent%)
duration: %vDuration% (%vMPCApiDuration%)
)
MsgBox %vText%
Return
;==================================================
#IfWinActive, ahk_class MediaPlayerClassicW
0:: ;skip to point
1::
2::
3::
4::
5::
6::
7::
8::
9::
vNum := SubStr(A_ThisHotkey, 1-1)
vElapsed2 := Round(vMPCApiDuration*(vNum/10))
Send(vMPCApiHWnd, 0xA0002000, "" vElapsed2) ;CMD_SETPOSITION := 0xA0002000
Return
#IfWinActive
;==================================================
On_WM_COPYDATA(wParam, lParam, msg, hwnd)
{
global vMPCApiHWnd
global vMPCApiTitle
global vMPCApiAuthor
global vMPCApiDesc
global vMPCApiPath
global vMPCApiDuration
global vMPCApiCurrent
dwData := NumGet(lParam+0, 0)
cbData := NumGet(lParam+A_PtrSize)
lpData := NumGet(lParam + 2*A_PtrSize)
lpData := StrGet(lpData)
if (dwData = 0x50000000) ;CMD_CONNECT := 0x50000000
{
vMPCApiHWnd := lpData
WinGetClass, vWinClass, ahk_id %vMPCApiHWnd%
if (vWinClass = "MediaPlayerClassicW")
MsgBox, , , MPC API on, 3
}
if (dwData = 0x50000003) ;CMD_NOWPLAYING := 0x50000003
{
StringSplit, lpData, lpData, |
vMPCApiTitle := lpData1
vMPCApiAuthor := lpData2
vMPCApiDesc := lpData3
vMPCApiPath := lpData4
vMPCApiDuration := lpData5
}
if (dwData = 0x50000007) ;CMD_CURRENTPOSITION := 0x50000007
vMPCApiCurrent := lpData
Return true
}
;==================================================
Send(Hwnd, dwData, lpData)
{
static WM_COPYDATA := 0x4a
VarSetCapacity(COPYDATASTRUCT, 3*A_PtrSize, 0)
cbData := (StrLen(lpData) + 1) * (A_IsUnicode ? 2 : 1)
NumPut(dwData, COPYDATASTRUCT, 0)
NumPut(cbData, COPYDATASTRUCT, A_PtrSize)
NumPut(&lpData, COPYDATASTRUCT, 2*A_PtrSize)
SendMessage, % WM_COPYDATA, % A_ScriptHwnd , &COPYDATASTRUCT,, % "ahk_id " Hwnd
return ErrorLevel == "FAIL" ? false : true
}
;==================================================
;USEFUL LINKS
;Sending Strings Via SendMessage - Ask for Help - AutoHotkey Community
;https://autohotkey.com/board/topic/98334-sending-strings-via-sendmessage/
;Media Player Classic - Homecinema MPC remote API (via WM_COPYDATA) - AutoIt Example Scripts - AutoIt Forums
;https://www.autoitscript.com/forum/topic/85354-media-player-classic-homecinema-mpc-remote-api-via-wm_copydata/
;mpcapi.h
;https://raw.githubusercontent.com/jeeb/mpc-be/master/src/apps/mplayerc/mpcapi.h
;winapi - media player classic - jump to point in video/audio programmatically - Stack Overflow
;http://stackoverflow.com/questions/41310778/media-player-classic-jump-to-point-in-video-audio-programmatically
;==================================================
USEFUL LINKS:
Sending Strings Via SendMessage - Ask for Help - AutoHotkey Community
https://autohotkey.com/board/topic/98334-sending-strings-via-sendmessage/
Media Player Classic - Homecinema MPC remote API (via WM_COPYDATA) - AutoIt Example Scripts - AutoIt Forums
https://www.autoitscript.com/forum/topic/85354-media-player-classic-homecinema-mpc-remote-api-via-wm_copydata/
mpcapi.h
https://raw.githubusercontent.com/jeeb/mpc-be/master/src/apps/mplayerc/mpcapi.h

Related

Change mouse cursor color when left button is held down - Windows

I am trying to change cursor color when the mouse left button is in the 'hold down state'. This is supposed to work in Windows 10, so something at the OS level, not in any specific program. I would like to do this to know when "ClickLock" is enabled.
Is there anyway to achieve this?
I have tried with Autohotkey but nothing happens
; Cursor types
IDC_APPSTARTING := 32650
~LButton::
while GetKeyState("LButton", "P")
{
; this is the code to the Dll call, but I am not sure how to integrate it
hCursor:=DllCall("LoadCursor", "UInt", NULL,"Int", IDC_APPSTARTING, "UInt")
DllCall("SetCursor","UInt",hCursor)
}
return
For more info, please refer to: https://autohotkey.com/board/topic/32608-changing-the-system-cursor/
IDC_APPSTARTING := 32650
~LButton::
changeCursor(IDC_APPSTARTING)
Return
~LButton Up::
changeCursor()
Return
changeCursor(cursor := 0) {
if (cursor) {
CursorHandle := DllCall("LoadCursor", Uint, 0, Int, cursor)
Cursors = 32512,32513,32514,32515,32516,32640,32641,32642,32643,32644,32645,32646,32648,32649,32650,32651
Loop, Parse, Cursors, `,
DllCall("SetSystemCursor", Uint, CursorHandle, Int, A_Loopfield )
} else {
DllCall("SystemParametersInfo", UInt, 0x57, UInt, 0, UInt, 0, UInt, 0 )
}
}
Have you tried anything yet? I would say probably start googling. I came across a few different resources on the first try that pointed me in the direction of the registry keys, and even a nifty powershell script to set them on demand. Do a little research, bro. Happy coding.

Move between Chrome windows using Autohotkey shortcuts

I spent last few hours trying to figure out how to use Autohotkey shortcut to switch between specific Chrome windows.
I have two monitors. Left side monitor (nr 1) is split vertically between Chrome localhost window and Vim editor. Right side monitor (nr 2) has Chrome fullscreen with my gmail, search tabs etc.
I would like to switch between the windows with ahk shortcuts e.g. Alt+1 (localhost monitor nr 1), Alt+2 (Chrome window monitor nr 2).
It's easy to do if windows have different title. I tried with titles, text, ahk_id, ahk_class, sysget (to change focus monitor), mouse clicks(covered by other windows) etc. Nothing seems to work consistently and couldn't google any sensible answer.
Any ideas?
This code should work for you. One hotkey to activate the leftmost chrome window. The other to active the rightmost
CoordMode, Pixel, Screen
!1::
ChromeList := GetWinListByClass("Chrome_WidgetWin_1")
LeftmostPos := 9999
LeftmostId := ""
Loop, % ChromeList.MaxIndex()
{
currentId := ChromeList[A_Index][1]
currentX := ChromeList[A_Index][2]
if (currentX < LeftmostPos)
{
LeftmostPos := currentX
LeftmostId := currentId
}
}
WinActivate, % "ahk_id" LeftmostId
Return
!2::
ChromeList := GetWinListByClass("Chrome_WidgetWin_1")
RightmostPos := -9999
RightmostId := ""
Loop, % ChromeList.MaxIndex()
{
currentId := ChromeList[A_Index][1]
currentX := ChromeList[A_Index][2]
if (currentX > RightmostPos)
{
RightmostPos := currentX
RightmostId := currentId
}
}
WinActivate, % "ahk_id" RightmostId
Return
GetWinListByClass(filterClass)
{
WinGet, all, list
ChromeList := {}
winCount := 1
Loop, %all%
{
WinGetClass, WClass, % "ahk_id " all%A_Index%
if (WClass = filterClass)
{
winId := all%A_Index%
WinGetPos, X, Y, W, H, % "ahk_id " winId
ChromeList[winCount] := [winId, X]
winCount++
}
}
return ChromeList
}

Super simple download with progress bar

Setting up an asynchronous download in AutoHotkey is a pain, but this is is necessary if you want to run some code during the download, like for example updating progress bar.
So the question is:
Is there a short and simple way of downloading a file with a progress bar, without including huge 1000+ lines libraries?
I came up with that code quite some time ago and you can still find it on the AHK forums, however, why not share it with the Stackoverflow community:
DownloadFile(UrlToFile, SaveFileAs, Overwrite := True, UseProgressBar := True, ExpectedFileSize := 0) {
;Check if the file already exists and if we must not overwrite it
If (!Overwrite && FileExist(SaveFileAs))
Return
;Check if the user wants a progressbar
If (UseProgressBar) {
;Initialize the WinHttpRequest Object
WebRequest := ComObjCreate("WinHttp.WinHttpRequest.5.1")
;Download the headers
WebRequest.Open("HEAD", UrlToFile)
WebRequest.Send()
try {
;Store the header which holds the file size in a variable:
FinalSize := WebRequest.GetResponseHeader("Content-Length")
} catch e {
; Cannot get "Content-Length" header
FinalSize := ExpectedFileSize
}
;Create the progressbar and the timer
Progress, , , Downloading..., %UrlToFile%
LastSizeTick := 0
LastSize := 0
; Enable progress bar updating if the system knows file size
SetTimer, __UpdateProgressBar, 1500
}
;Download the file
UrlDownloadToFile, %UrlToFile%, %SaveFileAs%
;Remove the timer and the progressbar because the download has finished
If (UseProgressBar) {
Progress, Off
SetTimer, __UpdateProgressBar, Off
}
Return
;The label that updates the progressbar
__UpdateProgressBar:
;Get the current filesize and tick
CurrentSize := FileOpen(SaveFileAs, "r").Length ;FileGetSize wouldn't return reliable results
CurrentSizeTick := A_TickCount
;Calculate the downloadspeed
SpeedOrig := Round((CurrentSize/1024-LastSize/1024)/((CurrentSizeTick-LastSizeTick)/1000))
SpeedUnit := "KB/s"
Speed := SpeedOrig
if (Speed > 1024) {
; Convert to megabytes
SpeedUnit := "MB/s"
Speed := Round(Speed/1024, 2)
}
SpeedText := Speed . " " . SpeedUnit
;Save the current filesize and tick for the next time
LastSizeTick := CurrentSizeTick
LastSize := FileOpen(SaveFileAs, "r").Length
if FinalSize = 0
{
PercentDone := 50
} else {
;Calculate percent done
PercentDone := Round(CurrentSize/FinalSize*100)
SpeedText := SpeedText . ", " . Round((FinalSize - CurrentSize) / SpeedOrig / 1024) . "s left"
}
;Update the ProgressBar
Progress, %PercentDone%, %PercentDone%`% (%SpeedText%), Downloading..., Downloading %SaveFileAs% (%PercentDone%`%)
Return
}
Here are some examples:
Example 1 - Download a firefox setup with a progressbar and overwrite it if it already exists on the disk:
DownloadFile("http://download-installer.cdn.mozilla.net/pub/firefox/releases/26.0/win32/en-US/Firefox%20Setup%2026.0.exe", "firefox_setup.exe")
Example 2 - Download Autohotkey with a progressbar and don't overwrite it if it already exists:
Url = http://ahkscript.org/download/ahk-install.exe
DownloadAs = AutoHotkey_L Installer.exe
Overwrite := False
UseProgressBar := True
DownloadFile(Url, DownloadAs, Overwrite, UseProgressBar)
Example 3 - Download a CCleaner setup and open a Save As dialog, asking the user where to save the file, overwriting it if already exists:
FileSelectFile, SaveAs, S, ccsetup410.exe
DownloadFile("http://download.piriform.com/ccsetup410.exe", SaveAs, True, True)

Navigate Shell command not working when the path includes an hash

I'm having problem using the Navigate Shell command when the path include an # sign.
; this will create 2 folders at the root of your C: drive
myPath1 := "C:\delete_me\"
myPath2 := "C:\delete#me\"
if !FileExist(myPath1)
FileCreateDir, %myPath1%
if !FileExist(myPath2)
FileCreateDir, %myPath2%
; make an Explorer active and press Alt-1 and Alt-2
return
!1::
strWinId := WinExist("A")
TrayTip, %myPath1%, %strWinId%
For pExp in ComObjCreate("Shell.Application").Windows
if (pExp.hwnd = strWinId)
try pExp.Navigate(myPath1)
return
!2::
strWinId := WinExist("A")
TrayTip, %myPath2%, %strWinId%
For pExp in ComObjCreate("Shell.Application").Windows
if (pExp.hwnd = strWinId)
try pExp.Navigate(myPath2)
return
Alt-1 works well. But, with Alt-2, the Navigate command returns "file:///C:/delete#me/ » not found.".
If there is no "/" after the "#" (eg myPath := "C:\delete#me"), it works. But this cannot be a solution because the destination path can be deeper in a subfolder (eg. "C:\delete#me\xyz").
I tried to encode the "#", replacing it with "%23", without success. Found nothing on the web or MSDN about that. Any idea?
[keywords: haskmark, hashtag, number sign or pound]
I have what looks to be a working solution for this, which I've also posted here:
4 options to change the current folder in Windows Explorer - Page 3 - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=526&p=153676#p153676
;links:
;Explorer Windows Manipulations - Page 5 - Scripts and Functions - AutoHotkey Community
;https://autohotkey.com/board/topic/19039-explorer-windows-manipulations/page-5#entry297581
;Navigate2 Method (IWebBrowser2)
;https://msdn.microsoft.com/en-us/library/aa752134(v=vs.85).aspx
;4 options to change the current folder in Windows Explorer - AutoHotkey Community
;https://autohotkey.com/boards/viewtopic.php?f=5&t=526
;windows - Navigate Shell command not working when the path includes an hash - Stack Overflow
;https://stackoverflow.com/questions/22868546/navigate-shell-command-not-working-when-the-path-includes-an-hash
;an AutoHotkey v1.1 script
;note: will create folder: %A_Desktop%\abc#def\abc#def
;q:: ;explorer - navigate to folder (tested on Windows 7)
WinGet, hWnd, ID, A
WinGetClass, vWinClass, % "ahk_id " hWnd
if !(vWinClass = "CabinetWClass") && !(vWinClass = "ExploreWClass")
return
vDir = %A_Desktop%\abc#def\abc#def
;vDir = %A_Desktop%\abc def\abc def
if !FileExist(vDir)
FileCreateDir, % vDir
DllCall("shell32\SHParseDisplayName", WStr,vDir, Ptr,0, PtrP,vPIDL, UInt,0, Ptr,0)
for oWin in ComObjCreate("Shell.Application").Windows
if (oWin.HWND = hWnd)
{
if !InStr(vDir, "#")
oWin.Navigate(vDir)
else
{
VarSetCapacity(SAFEARRAY, A_PtrSize=8?32:24, 0)
NumPut(1, SAFEARRAY, 0, "UShort")
NumPut(1, SAFEARRAY, 4, "UShort")
NumPut(vPIDL, SAFEARRAY, A_PtrSize=8?16:12, "Ptr")
NumPut(DllCall("shell32\ILGetSize", Ptr,vPIDL, UInt), SAFEARRAY, A_PtrSize=8?24:16, "Int")
oWin.Navigate2(ComObject(0x2011,&SAFEARRAY))
DllCall("shell32\ILFree", Ptr,vPIDL)
}
break
}
return
If you want to open a new window, there's no need for COM or unreliable workarounds: just run the folder.
Run C:\delete#me
If you want to open the path in an existing window which is already active, the simplest and most effective workaround is this:
SendInput {F4}{Esc}{Raw}C:\delete#me`n
So in the context of your script, you could use the following function to work around the # when it is present:
Navigate(pExp, myPath2)
;...
Navigate(Exp, Path)
{
if RegExMatch(Path, "#.*\\")
SendInput {F4}{Esc}{Raw}%Path%`n
else
Exp.Navigate(Path)
}
Unfortunately, there does not seem to be a solution to this. Shell.Application Navigate command fails if the path includes a hash (# as in C:\C#Projects).
Using AutoHotkey, the workaround would be to rely on the "second best" approach as identified by the tests in this thread: http://ahkscript.org/boards/viewtopic.php?f=5&t=526.
run, Explorer.exe
Sleep, 500
strFolder := A_ScriptDir
Send, {F4}{Esc}
Sleep, 500
ControlSetText, Edit1, C:\delete#me, A
ControlSend, Edit1, {Enter}, A
When I saw that Navigate couldn't handle hash, I was shocked,
but sure enough I replicated the error.
I thought I'd try the short form path just in case. It works!
if vDir contains #
Loop, %vDir%, 2, 0 ;(0/1/2=files/both/folders, 0/1=recurse no/yes)
vDir := A_LoopFileShortPath
The following approach doesn't require a visible address bar, or SendInput,
also the previous navigation history is maintained.
In the worst-case scenario of a hash in the short-form path of the dir above the target dir,
a go-between folder is used which is navigated to.
A link is created there, invoked, and deleted.
Below, the workaround code is indented, to separate it from the standard code.
A hotkey of ctrl+q, when an Explorer window is active, launches the script.
-
^q:: ;explorer - navigate to directory (use go-between dir if short-form path of dir above target contains #)
WinGet, hWnd, ID, A
WinGetClass, vWinClass, ahk_id %hWnd%
if vWinClass not in CabinetWClass,ExploreWClass
Return
vDir2 = %A_Desktop%\Go-Between ;go-between dir
vDir3 = C:\delete#me ;target dir
if (SubStr(vDir3, 1-1) = "\")
vDir3 := SubStr(vDir3, 1, -1)
if !InStr(FileExist(vDir3), "D")
Return
vPathLnk := ""
if vDir3 contains #
Loop, %vDir3%, 2, 0 ;(0/1/2=files/both/folders, 0/1=recurse no/yes)
vDir3 := A_LoopFileShortPath
;vDir4 is the short-form path of the dir above the target
;paths of problem target dirs are of the form: *#*\*
;where there is at least one hash with a backslash to its right
SplitPath, vDir3, , vDir4
if vDir4 contains #
{
if !InStr(FileExist(vDir2), "D")
FileCreateDir, %vDir2%
if !InStr(FileExist(vDir2), "D")
{
MsgBox error`, go-between dir not found:`r`n%vDir2%
Return
}
vNameLnk = Go-Between.lnk
vPathLnk = %vDir2%\%vNameLnk%
FileCreateShortcut, %vDir3%, %vPathLnk%
}
for oWin in ComObjCreate("Shell.Application").Windows
if (hWnd = oWin.Hwnd)
{
vDir1 := oWin.Document.Folder.Self.Path
if (vDir1 = vDir3)
break
if vDir3 contains #
{
if !(vDir1 = vDir2)
oWin.Navigate(vDir2)
while !(oWin.ReadyState = 4)
Sleep 10
oItem := oWin.Document.Folder.Items.Item(vNameLnk)
oItem.InvokeVerbEx("open")
break
}
oWin.Navigate(vDir3)
break
}
oWin := ""
if !(vPathLnk = "")
FileRecycle, %vPathLnk% ;send to recycle bin
;if !(vPathLnk = "")
;FileDelete, %vPathLnk% ;delete
Return

AutoHotkey able to capture firefox tab?

It is a bit beyond of my knowledge, so I copy&pasted the whole script.
But I was rejected with a message, which reads, (Firefox version is 28.0) Anybody please help me.
Error: Call to nonexistent function.
Specifically: Acc_Get( ... ...
SetTitleMatchMode 2
WinGet, windows, List, Mozilla Firefox
Loop %windows% {
hwnd := windows%A_Index%
;// Acc_Get(Cmd, ChildPath="", ChildID=0, WinTitle="", WinText="", ExcludeTitle="", ExcludeText="")
page_tab_list := Acc_Get("object", "application.grouping2.property_page.tool_bar3.page_tab_list", "", "ahk_id" hwnd)
For Each, tab in Acc_Children(page_tab_list)
if tab.accName(0) = "https://www.apple.com/" {
tab.accDoDefaultAction(0) ;// remove line to NOT activate tab
WinActivate ahk_id %hwnd%
break 2
}
}
This script simply walks through all tabs until it finds the correct one, via the page title in the window title. Sleep timer can be adjusted.
SetTitleMatchMode 2
needle := "Stack Overflow"
WinActivate, Firefox
Loop {
WinGetTitle, title
IfWinNotActive, Firefox
break
if (InStr(title,needle))
Break
Else
send ^{PgUp}
sleep 50
}
You didn't take all the dependencies from the source. You are missing the required Acc Library.
; Acc.ahk https://github.com/sancarn/ACC.AHK/blob/master/AccV2.ahk
; ACC Tutorial: https://www.autohotkey.com/boards/viewtopic.php?f=7&t=40590

Resources