Skip to content

Instantly share code, notes, and snippets.

@VADemon
Created April 26, 2018 23:32
Show Gist options
  • Save VADemon/156281d037da00f4d4de1adcab3e584a to your computer and use it in GitHub Desktop.
Save VADemon/156281d037da00f4d4de1adcab3e584a to your computer and use it in GitHub Desktop.
vYouOpen GUI to open local Youtube videos in browser
#include-once
; #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w 7
; #INDEX# =======================================================================================================================
; Title .........: _ShellFile
; AutoIt Version : v3.2.12.1 or higher
; Language ......: English
; Description ...: Create an entry in the shell contextmenu when selecting an assigned filetype, includes the program icon as well.
; Note ..........:
; Author(s) .....: guinness
; Remarks .......:
; ===============================================================================================================================
; #INCLUDES# ====================================================================================================================
#include <Constants.au3>
; #GLOBAL VARIABLES# ============================================================================================================
; None
; #CURRENT# =====================================================================================================================
; _ShellFile_Install: Creates an entry in the 'All Users/Current Users' registry for displaying a program entry in the shell contextmenu, but only displays when selecting an assigned filetype to the program.
; _ShellFile_Uninstall: Deletes an entry in the 'All Users/Current Users' registry for displaying a program entry in the shell contextmenu.
; ===============================================================================================================================
; #INTERNAL_USE_ONLY#============================================================================================================
; None
; ===============================================================================================================================
; #FUNCTION# ====================================================================================================================
; Name ..........: _ShellFile_Install
; Description ...: Creates an entry in the 'All Users/Current Users' registry for displaying a program entry in the shell contextmenu, but only displays when selecting an assigned filetype to the program.
; Syntax ........: _ShellFile_Install($sText, $sFileType[, $sName = @ScriptName[, $sFilePath = @ScriptFullPath[, $sIconPath = @ScriptFullPath[,
; $iIcon = 0[, $fAllUsers = False[, $fExtended = False]]]]]])
; Parameters ....: $sText - Text to be shown in the contextmenu.
; $sFileType - Filetype to be associated with the application e.g. .autoit or autoit.
; $sName - [optional] Name of the program. Default is @ScriptName.
; $sFilePath - [optional] Location of the program executable. Default is @ScriptFullPath.
; $sIconPath - [optional] Location of the icon e.g. program executable or dll file. Default is @ScriptFullPath.
; $iIcon - [optional] Index of icon to be used. Default is 0.
; $fAllUsers - [optional] Add to Current Users (False) or All Users (True) Default is False.
; $fExtended - [optional] Show in the Extended contextmenu using Shift + Right click. Default is False.
; Return values .: Success - Returns True
; Failure - Returns False and sets @error to non-zero.
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _ShellFile_Install($sText, $sFileType, $sName = @ScriptName, $sFilePath = @ScriptFullPath, $sIconPath = @ScriptFullPath, $iIcon = 0, $fAllUsers = False, $fExtended = False)
Local $i64Bit = '', $sRegistryKey = ''
If $iIcon = Default Then
$iIcon = 0
EndIf
If $sFilePath = Default Then
$sFilePath = @ScriptFullPath
EndIf
If $sIconPath = Default Then
$sIconPath = @ScriptFullPath
EndIf
If $sName = Default Then
$sName = @ScriptName
EndIf
If @OSArch = 'X64' Then
$i64Bit = '64'
EndIf
If $fAllUsers Then
$sRegistryKey = 'HKEY_LOCAL_MACHINE' & $i64Bit & '\SOFTWARE\Classes\'
Else
$sRegistryKey = 'HKEY_CURRENT_USER' & $i64Bit & '\SOFTWARE\Classes\'
EndIf
$sFileType = StringRegExpReplace($sFileType, '^\.+', '')
$sName = StringLower(StringRegExpReplace($sName, '\.[^.\\/]*$', ''))
If StringStripWS($sName, $STR_STRIPALL) = '' Or FileExists($sFilePath) = 0 Or StringStripWS($sFileType, $STR_STRIPALL) = '' Then
Return SetError(1, 0, False)
EndIf
_ShellFile_Uninstall($sFileType, $fAllUsers)
Local $iReturn = 0
$iReturn += RegWrite($sRegistryKey & '.' & $sFileType, '', 'REG_SZ', $sName)
$iReturn += RegWrite($sRegistryKey & $sName & '\DefaultIcon\', '', 'REG_SZ', $sIconPath & ',' & $iIcon)
$iReturn += RegWrite($sRegistryKey & $sName & '\shell\open', '', 'REG_SZ', $sText)
$iReturn += RegWrite($sRegistryKey & $sName & '\shell\open', 'Icon', 'REG_EXPAND_SZ', $sIconPath & ',' & $iIcon)
$iReturn += RegWrite($sRegistryKey & $sName & '\shell\open\command\', '', 'REG_SZ', '"' & $sFilePath & '" "%1"')
$iReturn += RegWrite($sRegistryKey & $sName, '', 'REG_SZ', $sText)
$iReturn += RegWrite($sRegistryKey & $sName, 'Icon', 'REG_EXPAND_SZ', $sIconPath & ',' & $iIcon)
$iReturn += RegWrite($sRegistryKey & $sName & '\command', '', 'REG_SZ', '"' & $sFilePath & '" "%1"')
If $fExtended Then
$iReturn += RegWrite($sRegistryKey & $sName, 'Extended', 'REG_SZ', '')
EndIf
Return $iReturn > 0
EndFunc ;==>_ShellFile_Install
; #FUNCTION# ====================================================================================================================
; Name ..........: _ShellFile_Uninstall
; Description ...: Deletes an entry in the 'All Users/Current Users' registry for displaying a program entry in the shell contextmenu.
; Syntax ........: _ShellFile_Uninstall($sFileType[, $fAllUsers = False])
; Parameters ....: $sFileType - Filetype to be associated with the application e.g. .autoit or autoit.
; $fAllUsers - [optional] Add to Current Users (False) or All Users (True) Default is False.
; Return values .: Success - Returns True
; Failure - Returns False and sets @error to non-zero.
; Author ........: guinness
; Example .......: Yes
; ===============================================================================================================================
Func _ShellFile_Uninstall($sFileType, $fAllUsers = False)
Local $i64Bit = '', $sRegistryKey = ''
If @OSArch = 'X64' Then
$i64Bit = '64'
EndIf
If $fAllUsers Then
$sRegistryKey = 'HKEY_LOCAL_MACHINE' & $i64Bit & '\SOFTWARE\Classes\'
Else
$sRegistryKey = 'HKEY_CURRENT_USER' & $i64Bit & '\SOFTWARE\Classes\'
EndIf
$sFileType = StringRegExpReplace($sFileType, '^\.+', '')
If StringStripWS($sFileType, $STR_STRIPALL) = '' Then
Return SetError(1, 0, False)
EndIf
Local $iReturn = 0, $sName = RegRead($sRegistryKey & '.' & $sFileType, '')
If @error Then
Return SetError(2, 0, False)
EndIf
$iReturn += RegDelete($sRegistryKey & '.' & $sFileType)
$iReturn += RegDelete($sRegistryKey & $sName)
Return $iReturn > 0
EndFunc ;==>_ShellFile_Uninstall
#include <GUIConstantsEx.au3>
#include '_ShellFile.au3'
If @Compiled = 0 Then
Exit MsgBox($MB_SYSTEMMODAL, '@Compiled Returned 0.', 'Please compile the program before testing. Thanks.')
EndIf
Func CompGetFileName($Path) ; https://www.sinister.ly/Thread-AutoIt-Get-File-Extension-Filename-and-Parent-directory-from-Path
;by 1234hotmaster
If StringLen($Path) < 4 Then Return -1
$ret = StringSplit($Path,"\",2)
If IsArray($ret) Then
Return $ret[UBound($ret)-1]
EndIf
If @error Then Return -1
EndFunc
_Main()
Func _Main()
Local $sFilePath = ''
If $CmdLine[0] > 0 Then
$sFilePath = $CmdLine[1]
EndIf
Local $hGUI = GUICreate('vYouOpen - GUI', 370, 210)
GUICtrlCreateEdit(_GetFile($sFilePath), 10, 5, 350, 120) ; If a file was passed via commandline then random text will appear in the GUICtrlCreateEdit().
Local $iAdd = GUICtrlCreateButton('Add FileType', 10, 180, 75, 25)
Local $iRemove = GUICtrlCreateButton('Remove FileType', 90, 180, 95, 25)
Local $iClipboard = GUICtrlCreateButton('Copy to clipboard', 190, 180, 105, 25)
Local $iOpenlink = GUICtrlCreateButton('Open URL', 300, 180, 65, 25)
GUISetState(@SW_SHOW, $hGUI)
While 1
Switch GUIGetMsg()
Case $GUI_EVENT_CLOSE
ExitLoop
Case $iAdd
_ShellFile_Install('vYouOpen - GUI', 'mkv') ; Add the running EXE to the Shell ContextMenu.
If @error Then
MsgBox($MB_SYSTEMMODAL, 'Association NOT Created.', '".autoit" was not associated due to an error occurring.')
Else
MsgBox($MB_SYSTEMMODAL, 'Association Created.', '"RandomFile.autoit" file was created to show that the filetype ".autoit" has been associtated with ' & @ScriptName & '.' & @CRLF & "Do not delete or remove this .exe file without previously disabling the association!!!" & @CRLF & @CRLF & 'If you restart the computer you''ll see the icon of "RandomFile.autoit" is the same as the program icon.' & @CRLF & @CRLF & 'Now close the program and double/right click on "RandomFile.autoit" to display the random text in the edit box.')
EndIf
_SetFile(_RandomText(5000), @ScriptDir & '\RandomFile.autoit', 1) ; Create a file with Random text.
Case $iRemove
_ShellFile_Uninstall('mkv') ; Remove the running EXE from the Shell ContextMenu.
If @error Then
MsgBox($MB_SYSTEMMODAL, 'Association NOT Deleted.', '".autoit" was not deleted from the Registry due to an error occurring.')
Else
MsgBox($MB_SYSTEMMODAL, 'Association Deleted.', '".autoit" was successfully deleted from the Registry and is no longer associated with ' & @ScriptName & '.')
EndIf
Case $iClipboard
if IsDeclared("ytURL") then
ClipPut($ytURL)
else
MsgBox($MB_SYSTEMMODAL, "Failed", "ytURL not defined or could not be generated")
endif
Case $iOpenlink
if IsDeclared("ytURL") then
ShellExecute($ytURL)
Exit(0)
else
MsgBox($MB_SYSTEMMODAL, "Failed", "ytURL not defined or could not be generated")
endif
EndSwitch
WEnd
GUIDelete($hGUI)
EndFunc ;==>_Main
Func _GetFile($sFilePath, $sFormat = 0)
Local $hFileOpen = FileOpen($sFilePath, $sFormat)
If $hFileOpen = -1 Then
Return SetError(1, 0, 'No File Was Passed Via Commandline.')
EndIf
FileClose($hFileOpen)
Local $fileName = CompGetFileName($sFilePath)
Local $sData = "File name: " & $fileName & @CRLF
local $mathmax1 = StringInStr($fileName, " ", 0, -1)
local $mathmax2 = StringInStr($fileName, ")", 0, -1)
local $firstSpace
if $mathmax1 > $mathmax2 and $mathmax1 <> 0 then
$firstSpace = $mathmax1
else
$firstSpace = $mathmax2
endif
if $firstSpace == 0 then
$firstSpace = 1
endif
$sData = $sData & "First space position: " & $firstSpace & @CRLF
Local $firstHyphen = StringInStr($fileName, "-", 0, 1, $firstSpace)
if $firstHyphen == 0 then
Return SetError(1, 0, 'No Hyphen found.')
endif
Local $extensionDot = StringInStr($fileName, ".", 0, -1)
if $extensionDot == 0 then
Return SetError(1, 0, 'No Dot found.')
endif
$sData = $sData & "Dot Position: " & $extensionDot & @CRLF
Local $VID = StringMid($fileName, $firstHyphen + 1, $extensionDot - $firstHyphen - 1 )
$sData = $sData & "Video ID: " & $VID & @CRLF
Global $ytURL = "https://www.youtube.com/watch?v=" & $VID
$sData = $sData & "URL: " & $ytURL
Return $sData
EndFunc ;==>_GetFile
Func _RandomText($iLength = 7)
Local $iCount = 0, $iCRLF, $sData = '', $sRandom
For $i = 1 To $iLength
$sRandom = Random(55, 116, 1)
If $iCount = 100 Then
$iCRLF = @CRLF
$iCount = 0
EndIf
$sData &= Chr($sRandom + 6 * ($sRandom > 90) - 7 * ($sRandom < 65)) & $iCRLF
$iCount += 1
$iCRLF = ''
Next
Return $sData
EndFunc ;==>_RandomText
Func _SetFile($sString, $sFilePath, $iOverwrite = 0)
Local $hFileOpen = FileOpen($sFilePath, $iOverwrite + 1)
FileWrite($hFileOpen, $sString)
FileClose($hFileOpen)
If @error Then
Return SetError(1, 0, $sString)
EndIf
Return $sString
EndFunc ;==>_SetFile
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment