Thursday 24 May 2012

Copying text from webpage to text file

I warn you now, I think this is the most convoluted solution I've ever come up with.  I'm convinced there must be an easier way to do this, but this was certainly an interesting way of doing it.


I had a table of information in an HTML file which I was rendering in an OcxContainer of type "Shell.Explorer" within the client application.  In fact I was first using lfiledump to create the file, and then rendering it like this...

$OCXHandle$ = $ocxhandle("OCX.DUM1") ;create handle to OcxContainer
$OCXHandle$->NAVIGATE(url,-,-,-,-)   ;navigate to file path
show                                 ;update screen

Which gave me something like this...




I now needed to export this information to a text file, easy for customers to upload to our helpdesk, or even paste straight into their response.  Thinking back, I was creating the HTML myself so it was controlled, I probably could have search and replaced the source code to reformat the results the way I wanted them.  However, I ended up doing it this way...


$OCXHandle$->EXECWB(17,-,-,-) ;select all
$OCXHandle$->EXECWB(12,-,-,-) ;copy
$OCXHandle$->EXECWB(18,-,-,-) ;clear selection
fieldsyntax ocx,"HID"         ;hide OcxContainer
fieldsyntax clipboard,""      ;show EditBox
$prompt = "clipboard.dum1"    ;fire <FieldGetsFocus> trigger


Then in the <FieldGetsFocus> trigger I had the following code...


macro "^INS_SELECT^DETAIL"    ;paste the text and fire <Detail> trigger



Then in the <Detail> trigger I had this code...

fieldsyntax ocx,""            ;showOcxContainer
fieldsyntax clipboard,"HID"   ;hide EditBox
if ( clipboard != "" )        ;process the text
  list = $replace(clipboard,1,"%%^","·;",-1)
  delitem list,1
  cont = ""
  while ( list != "" )
    getitem temp,list,1
    delitem list,1
    if ( temp != "" )
      temp = $replace(temp,1," ","·;",-1)
      getitem name,temp,1
      getitem valu,temp,2
      len = ( $length(name) / 2 )
      if ( len = len[trunc] )
        name = "%%name%%%  . . . . . . . . . . . . . . . . . "
      else
        name = "%%name%%% . . . . . . . . . . . . . . . . . "
      endif
      cont = "%%cont%%name[1:40]%%% '%%valu%%%'.%%^"    
    endif
  endwhile
  filedump/append cont,fnam   ;create the file
endif


This resulted in the following text file...

plugins/javascript/jquery.js  . . . . .  '1.6.1'.
plugins/javascript/jquery-ui.js . . . .  '1.8.13'.
javascript/sits_ajax.js . . . . . . . .  '851.1'.
javascript/dmu.js . . . . . . . . . . .  '850.1'.
javascript/translationajax.js . . . . .  '841.1'.
javascript/pod_bar.js . . . . . . . . .  '841.1'.
javascript/siw_elu.js . . . . . . . . .  '850.1'.
javascript/siw_elu_1.js . . . . . . . .  '850.1'.
javascript/siw_enl.js . . . . . . . . .  '850.1'.


As you can see, the result is exactly what I was after.  Having said that, the method is more than a little bit crazy, you don't need to tell me that!  I think it took me a whole day to come up with this solution, but I think it proves that sometimes you really have to think outside the box.  Hopefully I, nor anyone else, will ever need to use this in the future though!

For anyone using an OcxContainer of type "Shell.Explorer", I've found Microsoft's own MSDN site to be very useful, especially these links...


Summary: Sometimes solving a problem takes a spark of inspiration, sometimes it takes a lot of hard work with trial and error, and sometimes it takes plenty of both!

1 comment:

  1. Or use xmltostruct to transform the HTML to a struct and then process that.

    ReplyDelete