[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: [SOLUTION] The Solitaire Cipher (#1

Dennis Ranke

9/29/2004 11:44:00 AM

Here is my solution, just one Deck class, no guessing:

###

class Deck
def initialize
@deck = Array.new(54) {|i| i}
end

def create_keystream(count)
stream = []
count.times do
letter = next_letter
redo unless letter
stream << letter
end
return stream
end

def next_letter
##
# move the jokers
##

2.times do |j|
# find the joker
index = @deck.index(52 + j)

# remove it from the deck
@deck.delete_at(index)

# calculate new index
index = ((index + j) % 53) + 1

# insert the joker at that index
@deck[index, 0] = 52 + j
end

##
# do the tripple cut
##

# first find both jokers
a = @deck.index(52)
b = @deck.index(53)

# sort the two indeces
low, hi = [a, b].sort

# get the lower and upper parts of the deck
upper = @deck.slice!((hi + 1)..-1)
lower = @deck.slice!(0, low)

# swap them
@deck = upper + @deck + lower

##
# do the count cut
##

# find out the number of cards to cut
count = value_at(53)

# remove them from the top of the deck
cards = @deck.slice!(0, count)

# reinsert them just above the lowest card
@deck[-1, 0] = cards

return letter_at(value_at(0))
end

def value_at(index)
id = @deck[index]
(id > 51) ? 53 : id + 1
end

def letter_at(index)
id = @deck[index]
(id > 51) ? nil : (id % 26) + 1
end

def to_s
@deck.map {|v| (v > 51) ? (v + 13).chr : (v + 1).to_s}.join(' ')
end
end

def encode(data, keystream)
result = []
data.size.times {|i| result << ((data[i] + keystream[i]) % 26)}
return result
end

def decode(data, keystream)
encode(data, keystream.map {|v| 26 - v})
end

def data_to_string(data)
data = data.map {|v| (v + 65).chr}.join
(0...(data.size / 5)).map {|i| data[i * 5, 5]}.join(' ')
end

if ARGV.size != 1
puts "Usage: solitaire.rb MESSAGE"
exit
end

data = ARGV[0].upcase.split(//).select {|c| c =~ /[A-Z]/}.map {|c| c[0] -
65}
data += [?X - 65] * (4 - (data.size + 4) % 5)

deck = Deck.new
keystream = deck.create_keystream(data.size)

puts 'encoded:', data_to_string(encode(data, keystream))
puts 'decoded:', data_to_string(decode(data, keystream))

###

That was fun to code :)

--
exoticorn/farbrausch
4 Answers

(Paul Randall)

1/23/2009 4:36:00 PM

0


"Joe Fawcett" <joefawcett@newsgroup.nospam> wrote in message
news:%23qZavrUfJHA.1288@TK2MSFTNGP02.phx.gbl...
>
> "Paul Randall" <paulr90@aol.com> wrote in message
> news:uWGEjpSfJHA.4868@TK2MSFTNGP05.phx.gbl...
>> 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
>>
>>
>
> 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.

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)

1/23/2009 4:56:00 PM

0


"Al Dunbar" <alandrub@hotmail.com> wrote in message
news:eaENxDWfJHA.3776@TK2MSFTNGP03.phx.gbl...
>
> "Joe Fawcett" <joefawcett@newsgroup.nospam> wrote in message
> news:%23qZavrUfJHA.1288@TK2MSFTNGP02.phx.gbl...
>>
>> "Paul Randall" <paulr90@aol.com> wrote in message
>> news:uWGEjpSfJHA.4868@TK2MSFTNGP05.phx.gbl...
>>> 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
>>>
>>>
>>
>> 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.
>
> Self-modifying code can be difficult to debug. Have you looked into
> leaving the code as-is but making use of DHTML techniques?
>
> /Al

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

1/23/2009 10:31:00 PM

0


"Paul Randall" <paulr90@aol.com> wrote in message
news:eRQfqtXfJHA.5956@TK2MSFTNGP06.phx.gbl...
>
> "Al Dunbar" <alandrub@hotmail.com> wrote in message
> news:eaENxDWfJHA.3776@TK2MSFTNGP03.phx.gbl...
>>
>> "Joe Fawcett" <joefawcett@newsgroup.nospam> wrote in message
>> news:%23qZavrUfJHA.1288@TK2MSFTNGP02.phx.gbl...
>>>
>>> "Paul Randall" <paulr90@aol.com> wrote in message
>>> news:uWGEjpSfJHA.4868@TK2MSFTNGP05.phx.gbl...
>>>> 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
>>>>
>>>>
>>>
>>> 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.
>>
>> Self-modifying code can be difficult to debug. Have you looked into
>> leaving the code as-is but making use of DHTML techniques?
>>
>> /Al
>
> 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.

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)

1/24/2009 2:26:00 AM

0


"Al Dunbar" <alandrub@hotmail.com> wrote in message
news:%23HGWOpafJHA.564@TK2MSFTNGP03.phx.gbl...
>
> "Paul Randall" <paulr90@aol.com> wrote in message
> news:eRQfqtXfJHA.5956@TK2MSFTNGP06.phx.gbl...
>>
>> "Al Dunbar" <alandrub@hotmail.com> wrote in message
>> news:eaENxDWfJHA.3776@TK2MSFTNGP03.phx.gbl...
>>>
>>> "Joe Fawcett" <joefawcett@newsgroup.nospam> wrote in message
>>> news:%23qZavrUfJHA.1288@TK2MSFTNGP02.phx.gbl...
>>>>
>>>> "Paul Randall" <paulr90@aol.com> wrote in message
>>>> news:uWGEjpSfJHA.4868@TK2MSFTNGP05.phx.gbl...
>>>>> 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
>>>>>
>>>>>
>>>>
>>>> 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.
>>>
>>> Self-modifying code can be difficult to debug. Have you looked into
>>> leaving the code as-is but making use of DHTML techniques?
>>>
>>> /Al
>>
>> 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.
>
> 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

Thanks for the clarification, Al.

I've been mucking about in Microsoft's "HTML and DHTML Reference",
http://msdn.microsoft.com/en-us/librar...(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