Skip to content

Instantly share code, notes, and snippets.

@codeartery
codeartery / ShowWindow.vbs
Last active April 27, 2023 12:31
An AppActivate alternative that shows and activates a window using its process name, window title, or both.
Function ShowWindow( name, title )
REM@description
' An AppActivate alternative that shows and activates a window using its process name, window title, or both.
REM@author
' Jeremy England, http://codeartery.com/
REM@params
' name <string> The process name running the window. Use a comma delimited string to add more processes if you're unsure what process will be used. Leave blank "" if you don't know the process.
' title <regex> A regular expression string, to match the correct window title. Use "." if you don't know the title.
REM@returns
' ShowWindow <bool> Returns false if it doesn't find the window; true if it does.
@codeartery
codeartery / FormatString.vbs
Last active April 27, 2023 12:34
Evaluates data between { brackets } or user defined tokens, within strings, and allows for easy string appending.
Class FormatString
REM@description
' Evaluates data between { brackets } or user defined tokens, within strings, and allows for easy string appending.
REM@author
' Jeremy England, http://codeartery.com/
REM@mini
' Class FormatString:Private l,r,n,q:Sub Class_Initialize:Tokens="{}":n=vbLf:q="""":End Sub:Property Let Tokens(s):l=Left(s,1):r=Right(s,1):End Property:Property Let Append(f,s):f=f&Format(s):End Property:Public Default Function Format(s):Dim p,d:Format=s:p=InStr(1,Format,l)+1:If(p=1)Then:Exit Function:End If:d=Mid(Format,p,InStr(p,Format,r)-p):Format=Format(Replace(Format,(l&d&r),Eval(d))):End Function:End Class
Private tokenL, tokenR, n, q
Sub Class_Initialize
@codeartery
codeartery / QuickClip.vbs
Last active March 10, 2024 10:45
A quick way to set, get, or clear clipboard text in VBScript.
Function QuickClip( sText )
REM@description
' A quick way to set, get, or clear clipboard text.
REM@author
' Jeremy England, http://codeartery.com/
REM@params
' sText <string/null> - null will get the clipboard; a string will set it.
REM@return
' QuickClip <string> - The contents of your clipboard as a string.
REM@mini
@codeartery
codeartery / HTMLApp.vbs
Last active October 13, 2022 00:50
A class for creating an HTA window with the ability to return a value on close to the VBScript that called it.
Class HTMLApp
REM@description
' A class for creating an HTA window with the ability to return a value on close to the VBScript that called it.
' Use the r() javascript function to return a message and close the window.
' The return value will be returned to the VBScript Start() function.
REM@author
' Jeremy England, http://codeartery.com/
REM@mini
' Class HTMLApp:Public OnXReturn:Private h,b:Sub AppendHead(c):h=h&Replace(c,"""","&quot;"):End Sub:Sub AppendBody(c):b=b&Replace(c,"""","&quot;"):End Sub:Function Start:Start=CreateObject("WScript.Shell").Exec("mshta ""about:<html><head><style>html{background:#fff}</style><script>var c=1;function r(s){c=0;new ActiveXObject('Scripting.FileSystemObject').GetStandardStream(1).WriteLine(s);close()}onbeforeunload=function(){if(c)r("&OnXReturn&")}</script>"&h&"</head><body>"&b&"</body></html>""").StdOut.ReadLine:End Function:End Class
Public OnXReturn
@codeartery
codeartery / StdMessageStream.vbs
Last active August 29, 2022 09:42
Allows synchronous communication between VBScripts, if you call one from the other. Works with HTA files as well.
Class StdMessageStream
REM@description
' Allows synchronous communication between VBScripts, if you call one from the other. Works with HTA files as well.
' If one file is reading, the other needs to be writing.
REM@author
' Jeremy England, http://codeartery.com/
REM@mini
' Class StdMessageStream:Private s,f,x:Private Sub Class_Initialize:Set s=CreateObject("WScript.Shell"):Set f=CreateObject("Scripting.FileSystemObject"):End Sub:Property Let RunFile(a,p):Set x=s.Exec(a&" """&p&""""):End Property:Function Read:If IsObject(x)Then:Read=x.StdOut.ReadLine:Else:Read=f.GetStandardStream(0).ReadLine:End If:End Function:Sub Write(v):If IsObject(x)Then:x.StdIn.WriteLine(v):Else:f.GetStandardStream(1).WriteLine(v):End If:End Sub:End Class
Private oWss, oFso, oExe
@codeartery
codeartery / FileMessageStream.vbs
Last active September 5, 2020 22:48
Send messages to other scripts whether they're running or not using NTFS multiple data streams. Works with HTA files as well.
Class FileMessageStream
REM@description
' Send messages to other scripts whether they're running or not using NTFS multiple data streams. Works with HTA files as well.
REM@author
' Jeremy England, http://codeartery.com/
REM@mini
' Class FileMessageStream:Private o:Private Sub Class_Initialize:Set o=CreateObject("Scripting.FileSystemObject"):End Sub:Function ReadMessages(w):Dim f:If Not IsObject(wscript) And IsObject(window)Then:f=window.location.pathname:Else:f=wscript.scriptfullname:End If:With o.OpenTextFile(f&":FMStream",1,-1):If(.AtEndOfStream And Not w)Then:Exit Function:End If:Do While(.AtEndOfStream And w)::Loop:ReadMessages=.ReadAll():.Close():End With:WriteTo(f,0)="":End Function:Property Let WriteTo(f,a,d):Dim m:If(a)Then:m=8:Else:m=2:End If:If o.FileExists(f)Then:With o.OpenTextFile(f&":FMStream",m,-1):.Write(d):.Close():End With:Else:Err.Raise(53):End If:End Property:End Class
Private oFso, gFileNotFound
Private Sub Class_Initialize()
@codeartery
codeartery / Import.vbs
Last active April 25, 2024 06:08
Import/include/using code from an external VBScript file.
Function Import( vbsFile )
REM@description
' Import/include/using code from an external VBScript file.
REM@author
' Jeremy England, http://codeartery.com/
REM@params
' vbsFile <string> - A relative, absolute, or URL path to a file containing vbscript code.
REM@returns
' Import <bool> - Returns False if the import failed, and True if it succeeded.
REM@mini
@codeartery
codeartery / CmdOut.vbs
Last active April 25, 2023 19:10
Run a command prompt command and get its output in VBScript.
Function CmdOut( pCmd )
REM@description
' Run a command prompt command and get its output.
REM@params
' pCmd <string> - A command prompt command.
REM@returns
' CmdOut <string> - The output of the command.
REM@author
' Jeremy England, http://codeartery.com/
REM@mini
@codeartery
codeartery / FormatDateAs.vbs
Last active April 27, 2023 12:33
Format a date based off of a user defined 'd', 'm', 'y' pattern in VBScript.
Function FormatDateAs( dInput, sFormat, padZero )
REM@description
' Format a date based off of a user defined 'd', 'm', 'y' pattern.
REM@params
' dInput <date> - The date to format.
' sFormat <string> - The 'd', 'm', 'y' format you want the date in.
' padZero <bool> - Pads blank spaces with zeros when extra 'd', 'm', or, 'y's are provided than needed.
REM@returns
' FormatDateAs <string> - The formatted date.
REM@author
@codeartery
codeartery / IsProcessRunning.vbs
Last active April 27, 2023 12:24
Check if a process is running in VBScript.
Function IsProcessRunning( processNameExe )
IsProcessRunning = GetObject("WinMgmts:\\.\root\cimv2") _
.ExecQuery("SELECT * FROM Win32_Process WHERE Name LIKE '" & processNameExe & "'") _
.Count > 0
End Function