Bilgus Posted March 3, 2018 Share Posted March 3, 2018 (edited) Ok this should be the final version (I Think) barring bugs or additional features I'll probably post this to the example scripts in a few weeks once I get some real usage of it VlcMover.ico expandcollapse popup#Region ;**** Directives created by AutoIt3Wrapper_GUI **** ;;#AutoIt3Wrapper_Icon=VlcMover.ico #AutoIt3Wrapper_Outfile=VLC86.exe #AutoIt3Wrapper_Outfile_x64=VLC64.exe #AutoIt3Wrapper_AU3Check_Parameters=-q -d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #EndRegion ;**** Directives created by AutoIt3Wrapper_GUI **** ;GUIConstantsEx Global $GUI_EVENT_CLOSE = -0x3 Global $GUI_CHECKED = 0x1 Global $GUI_ENABLE = 0x40 Global $GUI_DISABLE = 0x80 ;WindowsConstants Global Const $WS_OVERLAPPEDWINDOW = 0x00CF0000 ; StringStripWS Constants Global Const $STR_STRIPALL = 8 ; Strip all spaces (over-rides all other flags) ; StringSplit Constants Global Const $STR_ENTIRESPLIT = 1 ; Entire delimiter marks the split Global Const $STR_NOCOUNT = 2 ; Disable the return count ;Authorization Structures Global Const $tagSECURITY_ATTRIBUTES = "dword Length;ptr Descriptor;bool InheritHandle" ; Global $gsINI_PATH = @AppDataDir & "\" & @ScriptName & "_PassThrough.ini" ;Needs to be in @AppDataDir to have write access If FileExists(@ScriptDir & "\" & @ScriptName & "_PassThrough.ini") Then $gsINI_PATH = @ScriptDir & "\" & @ScriptName & "_PassThrough.ini" EndIf ;Override with ini file in script directory, not writeable though Global Const $giSzMB = (1024 * 1024) Global Const $gsCFG = "Config" Global Const $giACTWAIT = 5 ;wait #seconds for window to become active Global Enum $geCfg_Close = 0, $geCfg_Max, $geCfg_Sz, $geCfg_Delay, $geCfg_DnDelay, $geCfg_Exec, _ $geCfg_ActCd, $geCfg_InACd, $geCfg_KeysAct, $geCfg_KeysInA, $geCfg_FileT, $geCfg_WName, _ $geCfg_WTxt Global Const $gaSettings = ["CloseOnInactive", "MaximizeOnActivate", "ExitOnFilesLessThanMB", "KeyDelayMS", _ "KeyDownDelayMS", "Exec_Name", "ActiveWindowCoords", "InactiveWindowCoords", _ "KeysOnActivate", "KeysOnInactive", "FileTypes", "WindowName", "WindowText_Fallback"] Global $g_bClose = (Read_Cfg($geCfg_Close, 0) = 0 ? False : True) Global $g_bMaxonAct = (Read_Cfg($geCfg_Max, 0) = 0 ? False : True) Global $g_iMinSz = Read_Cfg($geCfg_Sz, "0") ;Mb Global $g_iKeyDelay = Read_Cfg($geCfg_Delay, 100) Global $g_iKeyDnDelay = Read_Cfg($geCfg_DnDelay, 50) Global $g_sExec = Read_Cfg($geCfg_Exec, StringTrimRight(@ScriptName, 4) & "_Orig.exe") Global $g_sWinPosAct = Read_Cfg($geCfg_ActCd, "0") Global $g_sWinPosInact = Read_Cfg($geCfg_InACd, "0") ;Mb Global $g_sKeysSend_Act = Read_Cfg($geCfg_KeysAct, "f") ;(#Win)(+Shift)(arrow)(f)ull screen Global $g_sKeysSend_Inact = Read_Cfg($geCfg_KeysInA, "") Global $g_sFileTypes = Read_Cfg($geCfg_FileT, "mp4,avi,mkv,wmv") ;What files should be intercepted? Global $g_sMatchWin_Name = Read_Cfg($geCfg_WName, "VLC media player") Global $g_sMatchWin_FbTxt = Read_Cfg($geCfg_WTxt, "VLC (Direct3D output)") ;------------------------------------------------------------------------------------ Global $gsFileTypesMatch = BuildFileTypes($g_sFileTypes) Global $gsMatchVLC_Win = "[REGEXPTITLE:(?i)(" & $gsFileTypesMatch & ")(.*" & $g_sMatchWin_Name & ")]" Global $gsMatchVLC_Win_Fb = "[REGEXPTITLE:(?i)(.*" & $g_sMatchWin_Name & ")]" ;Fallback Matching Global $gaWinPosAct = GetRectFromString($g_sWinPosAct) Global $gaWinPosInact = GetRectFromString($g_sWinPosInact) Global $gPID = 0 Global $gPosInit Global Const $_HWND_BROADCAST = 0xFFFF Global $gbActivated = False Global $giActMsg = 0 Global $gsErr = "" Global $ghVlc ;Main-------------------------------------------------------------------------------------- If Not FileExists($gsINI_PATH) Then ;FIRST RUN Settings_Save() Settings() ;SETTINGS WINDOW EndIf Opt("SendKeyDelay", $g_iKeyDelay) Opt("SendKeyDownDelay", $g_iKeyDnDelay) If _IsCompiled() Then $giActMsg = _Create_Message("@ScriptFullPath" & "_msg") RunPassThrough() _Create_MessageHandler($giActMsg, "Activated_Ext") EndIf OnAutoItExitRegister("_Exit") Do $ghVlc = WaitActiveVLC_Win($gaWinPosAct, $g_sKeysSend_Act) Until (WaitNotExists_Win($ghVlc, $gaWinPosInact, $g_sKeysSend_Inact) Or $g_bClose) If $g_bClose And WinExists($ghVlc) Then WinClose($ghVlc) Exit ;^Main^------------------------------------------------------------------------------------ Func _Create_Message($sMessage) Local $iMsg = WinAPI_RegisterWindowMessage($sMessage) If $iMsg = 0 Then $gsErr &= "IPC Message Creation Failure " & $sMessage & @CRLF Return $iMsg EndFunc ;==>_Create_Message Func _Create_MessageHandler($iMsg, $sFunction) If $iMsg <> 0 Then Opt("GUIOnEventMode", 1) GUICreate(@ScriptName & "_IPC", 0, 0, 0, 0, 0, 0) ; create a top level window GUIRegisterMsg($iMsg, $sFunction) EndIf EndFunc ;==>_Create_MessageHandler Func _Exit() ;sets hotkeys and saves settings HotKeySet("{F4}", Settings) HotKeySet("{F1}", _Help) HotKeySet("{ESC}", _Exit_Now) ToolTip("Pass Through F1 - Help, F4 - Settings, ESC - Quit without saving", 0, 0) Sleep(5000) ToolTip("", 0, 0) HotKeySet("{F4}") HotKeySet("{F1}") HotKeySet("{ESC}") Settings_Save() Exit EndFunc ;==>_Exit Func _Exit_Now() OnAutoItExitUnRegister("_Exit") Exit EndFunc ;==>_Exit_Now Func _Help() MsgBox(0, "Pass Through", "Settings stored in:" & @CRLF & $gsINI_PATH & _ @CRLF & @CRLF & _ "Invoked: " & @ScriptDir & "\" & $g_sExec & " " & $CmdLineRaw & _ @CRLF & "FileTypes: " & $gsFileTypesMatch & @CRLF & _ "Errors:" & $gsErr & @CRLF) EndFunc ;==>_Help Func _IsCompiled() ;wakillon Return True ;@Compiled EndFunc ;==>_IsCompiled Func _Singleton($sOccurrenceName, $iFlag = 0) ;Valik Local Const $ERROR_ALREADY_EXISTS = 183 Local Const $SECURITY_DESCRIPTOR_REVISION = 1 Local $tSecurityAttributes = 0 If BitAND($iFlag, 2) Then ; The size of SECURITY_DESCRIPTOR is 20 bytes. We just ; need a block of memory the right size, we aren't going to ; access any members directly so it's not important what ; the members are, just that the total size is correct. Local $tSecurityDescriptor = DllStructCreate("byte;byte;word;ptr[4]") ; Initialize the security descriptor. Local $aRet = DllCall("advapi32.dll", "bool", "InitializeSecurityDescriptor", _ "struct*", $tSecurityDescriptor, "dword", $SECURITY_DESCRIPTOR_REVISION) If @error Then Return SetError(@error, @extended, 0) If $aRet[0] Then ; Add the NULL DACL specifying access to everybody. $aRet = DllCall("advapi32.dll", "bool", "SetSecurityDescriptorDacl", _ "struct*", $tSecurityDescriptor, "bool", 1, "ptr", 0, "bool", 0) If @error Then Return SetError(@error, @extended, 0) If $aRet[0] Then ; Create a SECURITY_ATTRIBUTES structure. $tSecurityAttributes = DllStructCreate($tagSECURITY_ATTRIBUTES) ; Assign the members. DllStructSetData($tSecurityAttributes, 1, DllStructGetSize($tSecurityAttributes)) DllStructSetData($tSecurityAttributes, 2, DllStructGetPtr($tSecurityDescriptor)) DllStructSetData($tSecurityAttributes, 3, 0) EndIf EndIf EndIf Local $aHandle = DllCall("kernel32.dll", "handle", "CreateMutexW", "struct*", $tSecurityAttributes, "bool", 1, "wstr", $sOccurrenceName) If @error Then Return SetError(@error, @extended, 0) Local $aLastError = DllCall("kernel32.dll", "dword", "GetLastError") If @error Then Return SetError(@error, @extended, 0) If $aLastError[0] = $ERROR_ALREADY_EXISTS Then If BitAND($iFlag, 1) Then DllCall("kernel32.dll", "bool", "CloseHandle", "handle", $aHandle[0]) If @error Then Return SetError(@error, @extended, 0) Return SetError($aLastError[0], $aLastError[0], 0) Else Exit -1 EndIf EndIf Return $aHandle[0] EndFunc ;==>_Singleton ;------------------------------------------------------------------------------------------ Func Activated_Ext() $gbActivated = True EndFunc ;==>Activated_Ext Func ArrayToStr($aArray) Local $sRet = "" If Not IsArray($aArray) Then Return SetError(1, 0, 0) For $i = 0 To UBound($aArray) - 1 $sRet &= $aArray[$i] & "," Next $sRet = StringTrimRight($sRet, 1) ;Remove the trailing ',' Return $sRet EndFunc ;==>ArrayToStr Func BuildFileTypes($sExtensions) ;Build a regexp string of file extensions from a comma separated one ;"txt,exe,com" => ".*\.txt,.*\.exe,.*\.com" ('\' escapes the '.') Local $sRet = "" $sExtensions = StringStripWS($sExtensions, $STR_STRIPALL) If Not StringIsASCII($sExtensions) Then Return "" Local $aExt = StringSplit($sExtensions, ",", $STR_ENTIRESPLIT + $STR_NOCOUNT) For $i = 0 To UBound($aExt) - 1 $sRet &= ".*\." & $aExt[$i] & "|" Next $sRet = StringTrimRight($sRet, 1) ;Remove the trailing '|' Return $sRet EndFunc ;==>BuildFileTypes Func GetRectFromString($sCoords) ;takes rect passed in string 'x, y, w, h' converts to array ;Returns [x, y, w, h] if string is valid or 0 if invalid ;You should check if return is an array with IsArray() $sCoords = StringStripWS($sCoords, $STR_STRIPALL) If Not StringIsASCII($sCoords) Then Return "" If $sCoords = "0" Or StringLen($sCoords) < 7 Then Return "" ;'0,0,0,0 = 7' Local $aRect = StringSplit($sCoords, ",", $STR_ENTIRESPLIT + $STR_NOCOUNT) ;_ArrayDisplay($aRect, "Rect From String") Return $aRect EndFunc ;==>GetRectFromString Func Read_Cfg($iSetting, $sDefault) Return IniRead($gsINI_PATH, $gsCFG, $gaSettings[$iSetting], $sDefault) EndFunc ;==>Read_Cfg Func RunPassThrough() ;calls the original file passing through commandline arguments Local $sPath = @ScriptDir & "\" & $g_sExec Local $sSwitches = $CmdLineRaw ;MsgBox(0, "Pass Through", $sPath & @CRLF & $sSwitches) $gPID = Run(Chr(34) & $sPath & Chr(34) & " " & $sSwitches, "") ; Run If _Singleton(@ScriptName, 1) = 0 Then If $CmdLine[0] > 1 And FileExists($CmdLine[2]) Then If $g_iMinSz And FileGetSize($CmdLine[2]) < ($g_iMinSz * $giSzMB) Then ;;;If file is less than $g_iMinSz Megabytes treat it like an inactive file ElseIf $giActMsg <> 0 Then ; Update script by posting a message from this (new) instance If StringRegExp($CmdLine[2], $gsFileTypesMatch) Then WinAPI_PostMessage($_HWND_BROADCAST, $giActMsg, 0, 0) EndIf EndIf EndIf _Exit_Now() ;single instance of script ONLY EndIf Sleep(1000) ;Wait a moment for the file to load EndFunc ;==>RunPassThrough Func Settings() Opt("GUIOnEventMode", 0) Local $iY = 60 Local $nMsg Local Enum $eS_KeySend = 0, $eS_Coords, $eS_SetWHelper Local $aCtlText[3] = ["Send Keys", "Coordinates (x, y, w, h)", "Set With Helper"] Local $aActivCtls[3] ;Holds handles for the Active State Controls Local $aInactCtls[3] ;Holds handles for the Inactive State Controls Local $hForm1_1 = GUICreate("Pass Through Settings", 287, 225 + $iY, 192, 124) GUICtrlCreateLabel("Execute", 8, 2, 74, 17) Local $hI_EXEC = GUICtrlCreateInput($g_sExec, 100, 0, 180, 21) GUICtrlCreateLabel("Win Text FB", 8, 30, 74, 17) Local $hI_FBT = GUICtrlCreateInput($g_sMatchWin_FbTxt, 100, 27, 180, 21) GUICtrlCreateLabel("Activated Settings", 8, 8 + $iY, 130, 17) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") GUICtrlCreateLabel("File Extensions", 8, 30 + $iY, 74, 17) Local $hI_FEXT = GUICtrlCreateInput($g_sFileTypes, 130, 27 + $iY, 150, 21) GUICtrlCreateLabel($aCtlText[$eS_KeySend], 8, 53 + $iY, 55, 17) $aActivCtls[$eS_KeySend] = GUICtrlCreateInput($g_sKeysSend_Act, 130, 49 + $iY, 150, 21) GUICtrlCreateLabel($aCtlText[$eS_Coords], 8, 75 + $iY, 111, 17) $aActivCtls[$eS_SetWHelper] = GUICtrlCreateButton($aCtlText[$eS_SetWHelper], 130, 96 + $iY, 150, 17) $aActivCtls[$eS_Coords] = GUICtrlCreateInput(ArrayToStr($gaWinPosAct), 130, 72 + $iY, 150, 21) Local $hC_AMAXM = GUICtrlCreateCheckbox("Maximize", 8, 96 + $iY, 89, 17) GUICtrlSetState($hC_AMAXM, $g_bMaxonAct ? 1 : 4) GUICtrlCreateLabel("Inactivated Settings", 8, 127 + $iY, 146, 17) GUICtrlSetFont(-1, 10, 800, 0, "MS Sans Serif") GUICtrlCreateLabel($aCtlText[$eS_KeySend], 8, 155 + $iY, 55, 17) $aInactCtls[$eS_KeySend] = GUICtrlCreateInput($g_sKeysSend_Inact, 130, 151 + $iY, 150, 21) GUICtrlCreateLabel($aCtlText[$eS_Coords], 8, 178 + $iY, 111, 17) $aInactCtls[$eS_SetWHelper] = GUICtrlCreateButton($aCtlText[$eS_SetWHelper], 130, 199 + $iY, 150, 17) $aInactCtls[$eS_Coords] = GUICtrlCreateInput(ArrayToStr($gaWinPosInact), 130, 175 + $iY, 150, 21) Local $hB_Cancel = GUICtrlCreateButton("Cancel", 8, 200 + $iY, 49, 17) Local $hB_Save = GUICtrlCreateButton("Save", 71, 200 + $iY, 49, 17) GUISetState(@SW_SHOW, $hForm1_1) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $hB_Cancel ContinueCase Case $GUI_EVENT_CLOSE GUIDelete($hForm1_1) Return ; Case $aActivCtls[$eS_SetWHelper] ;Pops-up window to make setting coords easier GUICtrlSetState($aActivCtls[$eS_SetWHelper], $GUI_DISABLE) GUICtrlSetData($aActivCtls[$eS_Coords], Settings_PosHelper("Active Pos", _ GetRectFromString(GUICtrlRead($aActivCtls[$eS_Coords])))) GUICtrlSetState($aActivCtls[$eS_SetWHelper], $GUI_ENABLE) Case $aInactCtls[$eS_SetWHelper] ;Pops-up window to make setting coords easier GUICtrlSetState($aInactCtls[$eS_SetWHelper], $GUI_DISABLE) GUICtrlSetData($aInactCtls[$eS_Coords], Settings_PosHelper("Inactive Pos", _ GetRectFromString(GUICtrlRead($aInactCtls[$eS_Coords])))) GUICtrlSetState($aInactCtls[$eS_SetWHelper], $GUI_ENABLE) Case $hB_Save $g_sExec = GUICtrlRead($hI_EXEC) $g_sMatchWin_FbTxt = GUICtrlRead($hI_FBT) $g_sFileTypes = GUICtrlRead($hI_FEXT) $g_sKeysSend_Act = GUICtrlRead($aActivCtls[$eS_KeySend]) $gaWinPosAct = GetRectFromString(GUICtrlRead($aActivCtls[$eS_Coords])) $g_bMaxonAct = (BitAND(GUICtrlRead($hC_AMAXM), $GUI_CHECKED) = $GUI_CHECKED) ? 1 : 0 $g_sKeysSend_Inact = GUICtrlRead($aInactCtls[$eS_KeySend]) $gaWinPosInact = GetRectFromString(GUICtrlRead($aInactCtls[$eS_Coords])) Settings_Save() Return ; EndSwitch WEnd EndFunc ;==>Settings Func Settings_PosHelper($sTitle, $sRect) ;Pops-up window to make setting coords easier ;sRect is a string format x, y, w, h OR '0' ;Returns new sRect in same format Local $nMsg Local $hForm1_2 = GUICreate($sTitle, 0, 0, -1, -1, $WS_OVERLAPPEDWINDOW) WinMoveArray($hForm1_2, GetRectFromString($sRect)) Local $hB_Set = GUICtrlCreateButton("Set", 0, 0, 50, 17) GUISetState(@SW_SHOW, $hForm1_2) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $hB_Set $sRect = ArrayToStr(WinGetPos($hForm1_2)) ContinueCase Case $GUI_EVENT_CLOSE GUIDelete($hForm1_2) Return $sRect ; EndSwitch WEnd EndFunc ;==>Settings_PosHelper Func Settings_Save() If Not _IsCompiled() Then Return Write_Cfg($geCfg_Exec, $g_sExec) Write_Cfg($geCfg_Close, $g_bClose ? 1 : 0) Write_Cfg($geCfg_Max, $g_bMaxonAct ? 1 : 0) Write_Cfg($geCfg_Sz, $g_iMinSz) ;Mb Write_Cfg($geCfg_Delay, $g_iKeyDelay) Write_Cfg($geCfg_DnDelay, $g_iKeyDnDelay) Write_Cfg($geCfg_KeysAct, $g_sKeysSend_Act) Write_Cfg($geCfg_KeysInA, $g_sKeysSend_Inact) Write_Cfg($geCfg_FileT, $g_sFileTypes) Write_Cfg($geCfg_WName, $g_sMatchWin_Name) Write_Cfg($geCfg_WTxt, $g_sMatchWin_FbTxt) Write_Cfg($geCfg_InACd, ArrayToStr($gaWinPosInact)) Write_Cfg($geCfg_ActCd, ArrayToStr($gaWinPosAct)) EndFunc ;==>Settings_Save Func WinAPI_PostMessage($hWnd, $iMsg, $wParam, $lParam) Local $aResult = DllCall("user32.dll", "bool", "PostMessage", "hwnd", $hWnd, "uint", $iMsg, "wparam", $wParam, "lparam", $lParam) If @error Then Return SetError(@error, @extended, False) Return $aResult[0] EndFunc ;==>WinAPI_PostMessage Func WinAPI_RegisterWindowMessage($sMessage) Local $aResult = DllCall("user32.dll", "uint", "RegisterWindowMessageW", "wstr", $sMessage) If @error Then Return SetError(@error, @extended, 0) Return $aResult[0] EndFunc ;==>WinAPI_RegisterWindowMessage Func WaitActiveVLC_Win($aRect, $sKeys) ; Loops waiting for a trigger from vlc window either filename(s) ; in the title or window name and text ; if PID is defined and doesn't exist closes script ; Returns window handle Local $hWin = 0 Local $bIsAct Do WinActivate($gsMatchVLC_Win, "") $hWin = WinWaitActive($gsMatchVLC_Win, "", $giACTWAIT) ; Wait for active window for number of seconds $bIsAct = ($hWin = 0 ? False : True) If Not $bIsAct Then ;Fallback WinActivate($gsMatchVLC_Win_Fb, $g_sMatchWin_FbTxt) $hWin = WinWaitActive($gsMatchVLC_Win_Fb, $g_sMatchWin_FbTxt, $giACTWAIT) $bIsAct = ($hWin = 0 ? False : True) If $bIsAct Then ExitLoop If $gPID And Not ProcessExists($gPID) And Not WinExists($gsMatchVLC_Win) Then Exit Sleep(1000) EndIf ;ConsoleWrite("VLC is " & ($bIsAct ? "" : "not ") & "active" & @CRLF) Until ($bIsAct) WinMoveArray($hWin, $aRect) If $sKeys <> "" Then ControlSend($hWin, "", 0, $sKeys) ;Send($sKeys) Sleep(100) If $g_bMaxonAct Then WinSetState($hWin, "", @SW_MAXIMIZE) EndIf Return $hWin EndFunc ;==>WaitActiveVLC_Win Func WaitNotExists_Win($hWin, $aRect, $sKeys) ;Returns False if Title Does Not exist but $hWin DOES exist ;Returns True if $hWin + $title DOES NOT exist Do $gbActivated = False WinWaitClose(WinGetTitle($ghVlc)) Until (Not $gbActivated) If WinExists($hWin) Then If $sKeys <> "" Then ControlSend($hWin, "", 0, $sKeys) ;Send($sKeys) Sleep(100) If $g_bMaxonAct Then WinSetState($hWin, "", @SW_RESTORE) WinMoveArray($hWin, $aRect) Return False EndIf Return True EndFunc ;==>WaitNotExists_Win Func WinMoveArray($hWin, $aC) ;Moves $hWin to coords specified by array If IsArray($aC) And UBound($aC) == 4 Then ;_ArrayDisplay($aC, "Win Move") WinMove($hWin, "", $aC[0], $aC[1], $aC[2], $aC[3]) EndIf EndFunc ;==>WinMoveArray Func Write_Cfg($iSetting, $sValue) IniWrite($gsINI_PATH, $gsCFG, $gaSettings[$iSetting], $sValue) EndFunc ;==>Write_Cfg  Edited March 3, 2018 by Bilgus It Deleted My Code! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now