Skip to content

Instantly share code, notes, and snippets.

@codeartery
Last active July 21, 2024 05:32
Show Gist options
  • Save codeartery/cd9dfa4da99645e753f9315e4dbaf1f0 to your computer and use it in GitHub Desktop.
Save codeartery/cd9dfa4da99645e753f9315e4dbaf1f0 to your computer and use it in GitHub Desktop.
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()
Set oFso = CreateObject( "Scripting.FileSystemObject" )
gFileNotFound = 53
End Sub
Function ReadMessages( waitForMessage )
Dim file
If Not IsObject(wscript) And IsObject(window) Then
file = window.location.pathname
Else
file = wscript.scriptfullname
End If
With oFso.OpenTextFile( file & ":FMStream", 1, True )
If(.AtEndOfStream And Not waitForMessage )Then Exit Function
Do While(.AtEndOfStream And waitForMessage )::Loop
ReadMessages = .ReadAll() : .Close()
End With
WriteTo( file, False ) = ""
End Function
Property Let WriteTo( file, append, data )
Dim iomode
If( append )Then
iomode = 8
Else
iomode = 2
End If
If oFso.FileExists(file) Then
With oFso.OpenTextFile( file & ":FMStream", iomode, True )
.Write( data ) : .Close()
End With
Else
Err.Raise( gFileNotFound )
End If
End Property
End Class
REM@usage
' Put the full or mini class/sub/function in your script to use.
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
REM initialize the object
Set oFms = New FileMessageStream
Dim otherFile
otherFile = "c:\location\of\~usage.1b.vbs"
REM wait for usage2 to send a message (run ~usage.1b.vbs now)
Dim response
response = oFms.ReadMessages( True )
MsgBox response, vbSystemModal, WScript.ScriptName
REM send a new message to usage2.
oFms.WriteTo(otherFile, False) = "This is from usage 1a" & vbNewLine
REM append to earlier message
oFms.WriteTo(otherFile, True) = "More from usage 1a"
REM@usage
' Put the full or mini class/sub/function in your script to use.
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
REM initialize the object
Set oFms = New FileMessageStream
REM read any unread messages. don't wait for one to be sent
Dim response
response = oFms.ReadMessages( False )
If IsEmpty(response) Then
MsgBox "No messages for usage 1b"
Else
MsgBox response, vbSystemModal, WScript.ScriptName
End If
REM send a message to usage1
oFms.WriteTo("c:\location\of\{~usage.1a.vbs", True) = "This is from usage 1b"
<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="ie=9">
<title>FileMessageStream: Usage 2a</title>
<hta:application name="exampleApp" />
<script language="VBScript">
REM@usage
' Put the full or mini class/sub/function in your script to use.
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
Set oFms = New FileMessageStream
</script>
<style>
html { display: table; }
body { display: table-cell; background-color: #ddd; }
html, body { width: 100%; height: 100%; margin: 0; }
/***************************************************/
#messages {
width: 90%;
margin: 0 5%;
}
.header {
width: 90%;
margin: 40px 5%;
}
.sent {
background-color: blue;
color: white;
border-radius: 40px;
float: right;
font-size: 14pt;
padding: 5px 20px;
}
.received {
background-color: green;
color: white;
border-radius: 40px;
float: left;
font-size: 14pt;
padding: 5px 20px;
}
#inp {
width: 100%;
background-color: white;
border: 0;
padding: 10px 20px;
font-size: 14pt;
border-radius: 40px;
}
</style>
</head>
<body>
<div class="header">
<input type="text" id="inp" onkeyup="if(event.keyCode == 13) sendMsg(this.value)" autofocus>
</div>
<hr>
<div id="messages">
<!--from streams-->
</div>
<script>
var otherfilePath = 'c:/location/of/~usage.2b.hta'
function sendMsg( msg ) {
messages.innerHTML += '<div class="sent">' + msg + '</div><br clear="all"/><br>'
oFms.WriteTo( otherfilePath, true ) = msg
inp.value = ""
}
setInterval(function() {
var msg = oFms.ReadMessages( false )
if (msg != null ) messages.innerHTML += '<div class="received">' + msg + '</div><br clear="all"/><br>'
}, 200);
inp.focus()
</script>
</body>
</html>
<!DOCTYPE html>
<head>
<meta http-equiv="X-UA-Compatible" content="ie=9">
<title>FileMessageStream: Usage 2b</title>
<hta:application name="exampleApp" />
<script language="VBScript">
REM@usage
' Put the full or mini class/sub/function in your script to use.
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
Set oFms = New FileMessageStream
</script>
<style>
html { display: table; }
body { display: table-cell; background-color: #ddd; }
html, body { width: 100%; height: 100%; margin: 0; }
/***************************************************/
#messages {
width: 90%;
margin: 0 5%;
}
.header {
width: 90%;
margin: 40px 5%;
}
.sent {
background-color: blue;
color: white;
border-radius: 40px;
float: right;
font-size: 14pt;
padding: 5px 20px;
}
.received {
background-color: green;
color: white;
border-radius: 40px;
float: left;
font-size: 14pt;
padding: 5px 20px;
}
#inp {
width: 100%;
background-color: white;
border: 0;
padding: 10px 20px;
font-size: 14pt;
border-radius: 40px;
}
</style>
</head>
<body>
<div class="header">
<input type="text" id="inp" onkeyup="if(event.keyCode == 13) sendMsg(this.value)" autofocus>
</div>
<hr>
<div id="messages">
<!--from streams-->
</div>
<script>
var otherfilePath = 'c:/location/of/~usage.2a.hta'
function sendMsg( msg ) {
messages.innerHTML += '<div class="sent">' + msg + '</div><br clear="all"/><br>'
oFms.WriteTo( otherfilePath, true ) = msg
inp.value = ""
}
setInterval(function() {
var msg = oFms.ReadMessages( false )
if (msg != null ) messages.innerHTML += '<div class="received">' + msg + '</div><br clear="all"/><br>'
}, 200);
inp.focus()
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment