Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need help scripting a self-modifying HTA

46 views
Skip to first unread message

Paul Randall

unread,
Jan 23, 2009, 2:15:31 AM1/23/09
to
Hi,
Background: I'm working on building a VBScript that can build a series of
HTAs that display individual groups of pictures from a camera's flash card.
My script currently builds a list of all the pictures on the flash card,
sorted by date/time, and then splits the list into groups where a new group
is started any time there is an interval of at least an hour between
pictures. I think a gap of an hour is a good indicator that the subject may
have changed. Info from these group lists are put into the bodies of group
HTAs, which look like this:
<body>
1 of 3 9/8/2008 4:12:00 PM DSC_0181.JPG<br>
<img src="file://e:\DCIM\100NCD80\DSC_0181.JPG"><br>
2 of 3 9/8/2008 4:13:00 PM DSC_0182.JPG<br>
<img src="file://e:\DCIM\100NCD80\DSC_0182.JPG"><br>
3 of 3 9/8/2008 4:13:00 PM DSC_0183.JPG<br>
<img src="file://e:\DCIM\100NCD80\DSC_0183.JPG"><br>
This group size: 4,319,651<br>
</body>

Now that these group HTAs are displaying properly, I want to make it
possible for the photographer to add or edit a multi-sentence description
for the whole group of pictures and perhaps for each picture. It seems to
me it should be possible to use the onload event to build a dropdown list of
the IDs of the description IDs, so that load and save buttons and a common
editable text box can be used to edit any of the description divisions. I
have worked out logic to save this self-modified HTA to the file path it was
loaded from, as in the HTA listing below. My intent is to have the
photographer (or others) to be able to run the VBScript that builds the
group HTAs, then run each of the HTAs and add any desired descriptions, and
then write the group HTAs and pictures to CD/DVD for archival.

I'm thinking this might also be an easy way to pass around CD/DVDs
containing scans of old pictues to a select group of relatives who might be
able to add interesting commentary to the pictures in group HTAs (but they
would have to email the modified HTAs to some central location to build an
integrated set of HTAs & pictures, since writing it back to the CD/DVD would
be difficult).

I'm hoping to get constructive criticism as to whether the project is doable
and perhaps sample code to collect the info needed from the description DIVs
to build a dropdown selection, and buttons to move (load/save) the
descriptions between their divisions and an editable text box.

Here is the current version of the HTA; it is also included as an attachment
(remove the .txt):
Note that one line, <HTA:APPLICATION id="oMyHTA" APPLICATIONNAME="My App"
SCROLL="yes" SINGLEINSTANCE="no" WINDOWSTATE=""> </HTA:APPLICATION>, has to
be one long line because a multiline version of it will be converted back to
one long line by the Self.document.all.item(0).Outerhtml property. I'm
hoping to use a comparison of the Outerhtml at window onload and unload
times to decide whether to ask the user if the HTA should be saved.

Note too that the current version saves the the document's HTML at window
onload time with either 'yyy' or 'xxx' added, to prove that it actually was
changed.

<HTML XMLNS:HTA><HEAD><TITLE>Self Modifying HTA</TITLE>
<SCRIPT language=vbscript>
'Putting this script here prepositions the window.
'Windowstate=Maximize would override this.
'Global Variables:
Dim sOriginalHTA, sSourcePath
Const wdDlg = 600, htDlg = 380 ' dialog size
Const pxLeft = 100, pxTop = 100 ' positioning
window.ResizeTo wdDlg,htDlg
window.MoveTo pxLeft,pxTop
</SCRIPT>
<HTA:APPLICATION id="oMyHTA" APPLICATIONNAME="My App" SCROLL="yes"
SINGLEINSTANCE="no" WINDOWSTATE=""> </HTA:APPLICATION>

<SCRIPT language=VBScript>
Sub Window_Onload
sOriginalHTA = Self.document.all.item(0).Outerhtml
sSourcePath = Split(UnEscape(Self.location), "///")(1)

'Write the contents of the HTA source back to its original file,
' adding xxx or yyy depending on whether it is Unicode or
' ASCII.
With CreateObject("Scripting.FileSystemObject")
On Error Resume Next
.CreateTextFile(sSourcePath, True, False). _
Write(sOriginalHTA & "yyy")
If Err Then
'In case modifications include Unicode characters
On Error GoTo 0
.CreateTextFile(sSourcePath, True, True). _
Write(sOriginalHTA & "xxx")
End If
On Error GoTo 0
End With

'Read it into a string and see if anything changed.

sTemp = include(sSourcePath)
msgbox "A difference of 'xxx' or 'yyy' indicates " & _
"the original source code was replaced, " & _
"using Unicode or ASCII write, respectively" & vbCrLf & _
"Difference is: " & replace(sTemp, soriginalhta, "")
End Sub

Function include(sFileName)
'Return a string containing the entire contents of
' the specified file.
Dim objFSO
Dim objFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set objFile = objFSO.OpenTextFile(sFileName)
If Err then
Msgbox "Quitting - file " & sFileName & " can not be included."
Wscript.Quit
End If
On Error GoTo 0
include = objFile.ReadAll()
objFile.Close
End Function 'include(FileName)
</SCRIPT>
</HEAD>
<BODY>
<div id="GroupDescription">Description of group goes here
</div><br>
1 of 3 9/8/2008 4:12:00 PM DSC_0181.JPG<br>
<img src="file://e:\DCIM\100NCD80\DSC_0181.JPG"><br>
<div id="1">Caption/commentary For picture 1</div><br>
2 of 3 9/8/2008 4:13:00 PM DSC_0182.JPG<br>
<img src="file://e:\DCIM\100NCD80\DSC_0182.JPG"><br>
<div id="2">Caption/commentary For picture 2</div><br>
3 of 3 9/8/2008 4:13:00 PM DSC_0183.JPG<br>
<img src="file://e:\DCIM\100NCD80\DSC_0183.JPG"><br>
<div id="3">Caption/commentary For picture 3</div><br>
</BODY>
</HTML>

Thanks for any help you can give me.

-Paul Randall

SelfModify.hta.txt

Joe Fawcett

unread,
Jan 23, 2009, 6:08:40 AM1/23/09
to

"Paul Randall" <pau...@aol.com> wrote in message
news:uWGEjpSf...@TK2MSFTNGP05.phx.gbl...

I think it's doable. I'm not sure that it wouldn't be better to use a more
traditional method, that of a datastore and a single HTA.
The datastore, an XML document for example, would represent the images and
their groups and also have the descriptions and any other metadata
associated with them. The your HTA would have two modes. One that created
the XML and one that read a nominated XML and displayed the appropriate
images and text.
This way you have only one version of the HTA, not hundreds, the downside is
you need two files for it to work as well as the images.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Al Dunbar

unread,
Jan 23, 2009, 8:46:04 AM1/23/09
to

"Joe Fawcett" <joefa...@newsgroup.nospam> wrote in message
news:%23qZavrU...@TK2MSFTNGP02.phx.gbl...

Self-modifying code can be difficult to debug. Have you looked into leaving
the code as-is but making use of DHTML techniques?

/Al


Paul Randall

unread,
Jan 23, 2009, 11:36:09 AM1/23/09
to

"Joe Fawcett" <joefa...@newsgroup.nospam> wrote in message
news:%23qZavrU...@TK2MSFTNGP02.phx.gbl...
>

Thanks for the feedback, Joe.

I agree that a datastore has definite advantages. I think I can work fairly
well with basic HTML, but XML/XSLT/CSS would be a daunting learning task
unless I could find a book or web site with a lot of working sets of simple
sample code that I could understand and 'borrow' from. I think my ADO
scripting skills are good enough to build and use CSV or MDB files as a
datastore.

I currently have only one person in mind for using this application, so I
think I will try to develop this self-modifying HTA approach.

Thanks again,

-Paul Randall


Paul Randall

unread,
Jan 23, 2009, 11:55:31 AM1/23/09
to

"Al Dunbar" <alan...@hotmail.com> wrote in message
news:eaENxDWf...@TK2MSFTNGP03.phx.gbl...

Hi, Al

Perhaps I didn't explain it well enough. An HTA can contain data and HTML
tags to format that data, and script code to do various functions. In this
application the HTA's script code doesn't change -- only the info in the
description DIVs would be modified (and saved in the original HTA file) each
time the HTA is run and saved. I agree that self-modifying code can be a
real pain, and I think I need to add code to preserve a backup of the
original HTA in case something goes wrong. I think modifying the DIV
content is a DHTML technique. What DHTML techniques did you have in mind?

-Paul Randall


Al Dunbar

unread,
Jan 23, 2009, 5:31:16 PM1/23/09
to

"Paul Randall" <pau...@aol.com> wrote in message
news:eRQfqtXf...@TK2MSFTNGP06.phx.gbl...

This approach (a script that modifies the content of the file in which it is
contained) can still be problematic. It could be argued that this still
qualifies as self-modifying code, in the same way that changing a line like
this:

name = "bill"

to this:

name = "dave"

is a change to the "code". In your case the data being modified is not
embedded into the "script code" per se, but data embedded between DIV tags.
While that might make an interesting discussion on a theoretical level, it
doesn't advance your problme much farther ;-)

> I agree that self-modifying code can be a real pain, and I think I need
> to add code to preserve a backup of the original HTA in case something
> goes wrong. I think modifying the DIV content is a DHTML technique. What
> DHTML techniques did you have in mind?

DHTML doesn't change what actually exists within a DIV tag (or any
scriptable object in the page). Rather it changes how the page is rendered
by making the HTML code that is emitted to the browser conditional on what
the script decides to do.

That said, I'm extremely rusty, so have little specific detail to provide
other than the suggestion youconsider DHTML.

/Al


Paul Randall

unread,
Jan 23, 2009, 9:26:08 PM1/23/09
to

"Al Dunbar" <alan...@hotmail.com> wrote in message
news:%23HGWOpa...@TK2MSFTNGP03.phx.gbl...

Thanks for the clarification, Al.

I've been mucking about in Microsoft's "HTML and DHTML Reference",
http://msdn.microsoft.com/en-us/library/ms533050(VS.85).aspx for so long
that my conception of what DHTML really is may be rather distorted :-)
Right now I kind of feel that anything I script to control any of the things
this document calls DHTML methods, events, properties or whatever, is using
DHTML. It may be that the DHTML reference is just poorly worded or I don't
understand what it means. I do appreciate your input.

-Paul Randall


0 new messages