home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / archives / ckermit.tar / scrape < prev    next >
Text File  |  1999-05-26  |  11KB  |  315 lines

  1. # s c r a p e  --  Screen-scraping demo script for Kermit 95.
  2. #
  3. # Kermit 95 1.1.17 or later required.
  4. #
  5. # Author:  Max Evarts, The Kermit Project, Columbia University, 24 May 1999
  6. #
  7. goto BEGIN ; Jump around comments
  8.  
  9. Kermit 95 allows scripts to access the terminal screen via the functions:
  10.  
  11.   \fscrncurx()
  12.     Returns the 0-based X coordinate (column) of the Terminal screen cursor.
  13.  
  14.   \fscrncury()
  15.     Returns the 0-based Y coordinate (row) of the Terminal screen cursor.
  16.  
  17.   \fscrstr(ny,nx,n1)
  18.     Returns the string at Terminal-screen coordinates (nx,ny), length n1,
  19.     blanks included.
  20.  
  21. The script below is a real-life demonstration of these functions that was
  22. written to solve the following problem:
  23.  
  24.   A program displays scanned optical images.  Each image file is indexed
  25.   (up to 7 indexes) and we can use these indexes to find images.
  26.   Customers have a window with seven fields in which to put information to
  27.   find an image.  The customer presses a special key on the information
  28.   that is displayed on the Kermit 95 terminal screen and this information
  29.   should be forwarded to one of the seven index fields (filters).  We have
  30.   our own API and use program:
  31.  
  32.   CSAPI32.EXE SYSTEMNAME [SPYLINK NAME] [FILTER1/,] ... [FILTER7/,]
  33.        [INTERACTIVE(YES/NO)] [SHOW DUPLICATES(YES/NO)] [USE FILTER(YES/NO)]
  34.  
  35.   to run our browser and display images depending on filters 1-7.
  36.  
  37. Requirements:
  38.  
  39.   1. An initialization file in which we declare:
  40.       a) SYSTEMNAME (ip address - general parameter)
  41.       b) INTERACTIVE (YES/NO - general parameter)
  42.       c) SHOWDUPLICATES (YES/NO - general parameter)
  43.       d) USEFILTER (YES/NO - general parameter)
  44.       e) Screen name on which definition is active
  45.       f) SPYLINKNAME (type of document which I search: invoices, bills,
  46.      etc -- the parameter can be different on each screen)
  47.       g) Up to seven position of information (start column, end column,
  48.          row) associated with filters - parameters can be different on
  49.          each screen
  50.  
  51.   2. A script that, after the user presses the special key, runs CSAPI32
  52.      with parameters based on screen scraping and the initialization-file
  53.      settings.  This script should get the screen name (which appears in a
  54.      certain spot on every screen, e.g.  in line 2 from column 5 to 18
  55.      "Work with invoices").  This is required to forward the info to the
  56.      proper spylink (document type) and because filters which can be
  57.      different on each screen.  If the user presses the special key on a
  58.      non-declared screen, an error message is given.
  59.  
  60. Operation:
  61.  
  62.   We define a macro, READSCRN, that uses screen scraping functions to a)
  63.   determine if the current host screen is one of the screens that has been
  64.   defined and b) if it is, read values from the .INI file and the current
  65.   screen to build a command line for the image browsing application.
  66.  
  67. The script:
  68.  
  69. :BEGIN
  70.  
  71. <begin SCRAPEIT.KSC>
  72.  
  73. ; SCRAPEIT.KSC - Script to be called at startup of Kermit 95 to initialize
  74. ; screen scraping-functions for control of a local application, CSAPI32.EXE,
  75. ; from Kermit 95 using information displayed on the user's screen and
  76. ; taken from an initialization file, SCRP.CFG.
  77. ;
  78. ; Version 1.0 - 5/14/99
  79. ;   Change "echo" to "run" to make version live
  80. ; Version 0.6 beta - 5/6/99
  81. ;   Changes to READSCRN macro to place commas for empty values and separate
  82. ;   two or more contiguous filter values by spaces instead of commas - ME
  83. ; Version 0.5 beta - 5/5/99
  84. ;   First beta release
  85.  
  86. ; HOLDSCREEN function is used to update the user on an error condition,
  87. ; require them hit Enter to move on, then resume program flow at a given
  88. ; point.
  89.  
  90. def HOLDSCREEN echo, getc \%9 {Press Enter to continue: }, goto \%1
  91.  
  92. ; MAKEVAR function is used to create a variable from each item in the
  93. ; SCRP.CFG file by prefixing each left column item with an underscore and
  94. ; then assigning the value from the right column of scrp.cfg to it.
  95.  
  96. def MAKEVAR asg \%9 _\%1, _assign \%9 \%2
  97.  
  98. ; This variation does not evaluate the value being assigned to the variable
  99. def MAKEKVAR asg \%9 _\%1, asg \%9 \%2
  100.  
  101. ; Tell user what this file does
  102. echo
  103. echo Loading CSAPI imaging extensions...
  104.  
  105. ; Open the configfile (scrp.cfg) in an error-handling way
  106. def _noconfig
  107. asg _configfile \freplace(\v(startup)scripts/scrp.cfg,/,\\)
  108. echo Configuraton file = \m(_configfile)
  109. xif not exist \m(_configfile) { 
  110.   ec {FATAL - Configuration file \m(_configfile) not found}
  111.   holdscreen END
  112.   def _noconfig 1
  113.   end
  114. }
  115. open read \m(_configfile)
  116. xif fail {
  117.   ec {Fatal - Error reading configuration file \m(_configfile)}
  118.   holdscreen END
  119.   def _noconfig 1
  120.   end
  121. }
  122.  
  123. ; OK, file is here and it is good, read each line and feed it to MAKEVAR 
  124. ; or MAKEKVAR, handling blank lines and comment lines
  125.  
  126. echo Loading configuration...
  127. while true {
  128.   read \%a
  129.   if fail break
  130.   if eq "\fsubstring(1,1,\%a)" ";" continue
  131.   if eq "\%a" "" continue
  132.   xif eq "\fsubstring(1,9,\%a)" "keymapped" {
  133.      makekvar \%a
  134.      continue
  135.   }
  136.   makevar \%a
  137. }
  138. close read
  139. echo Configuration loaded successfully.
  140. echo
  141.  
  142. ; The READSCRN macro does the bulk of the real work. 
  143. ;
  144. ; It first initializes all of its internal variables, then it goes into a
  145. ; FOR loop that reads up to _maxscreen number of possible screenname
  146. ; locations looking for an exact match to the screen name variable at the
  147. ; exact location specified in SCRP.CFG.  If a match is found, _scrno is
  148. ; defined to represent the proper prefix for that screen.  Subsequently,
  149. ; _scrno is used to select all the appropriate variables for the active
  150. ; screen.  If no match is found then, _scrno remains undefined and the
  151. ; READSCRN macro exits immediately.
  152. ;
  153. ; From here the macro simply uses the \fscrnstr() function to read values
  154. ; from the screen at the locations defined for the active screen.  It then
  155. ; combines these values with the general and screen-specific variables and
  156. ; builds and runs the CSAPI32.EXE command line.
  157.  
  158. def READSCRN {
  159.  def _scrno, def _qscrn, def _fltr1, def _fltr2
  160.  def _fltr3, def _fltr4, def _fltr5, def _fltr6, def _fltr7
  161.  for \%i 1 \m(_maxscreens) 1 {
  162.     _asg _qscrn\%i \ftrim(\fscrnstr(\fdef(_scr\%i_name_r),-
  163. \fdef(_scr\%i_name_c),\fdef(_scr\%i_name_l)))
  164.     if eq {\m(_qscrn\%i)} {\m(_screenname\%i)} _asg _scrno scr\%i
  165.  }
  166.  if not def _scrno goto END
  167.  if = \fdef(_\m(_scrno)_fltr1_l) 0 asg _fltr1 {\44}
  168.  else asg _fltr1 \ftrim(\fscrnstr(\fdef(_\m(_scrno)_fltr1_r)
  169.                              \fdef(_\m(_scrno)_fltr1_c)
  170.                              \fdef(_\m(_scrno)_fltr1_l)))
  171.  if eq "\m(_fltr1)" "" asg _fltr1 {\44}
  172.  if = \fdef(_\m(_scrno)_fltr2_l) 0 asg _fltr2 {\44}
  173.  else asg _fltr2 \ftrim(\fscrnstr(\fdef(_\m(_scrno)_fltr2_r)
  174.                              \fdef(_\m(_scrno)_fltr2_c)
  175.                              \fdef(_\m(_scrno)_fltr2_l)))
  176.  if eq "\m(_fltr2)" "" asg _fltr2 {\44}
  177.  if = \fdef(_\m(_scrno)_fltr3_l) 0 asg _fltr3 {\44}
  178.  else asg _fltr3 \ftrim(\fscrnstr(\fdef(_\m(_scrno)_fltr3_r)
  179.                              \fdef(_\m(_scrno)_fltr3_c)
  180.                              \fdef(_\m(_scrno)_fltr3_l)))
  181.  if eq "\m(_fltr3)" "" asg _fltr3 {\44}
  182.  if = \fdef(_\m(_scrno)_fltr4_l) 0 asg _fltr4 {\44}
  183.  else asg _fltr4 \ftrim(\fscrnstr(\fdef(_\m(_scrno)_fltr4_r)
  184.                              \fdef(_\m(_scrno)_fltr4_c)
  185.                              \fdef(_\m(_scrno)_fltr4_l)))
  186.  if eq "\m(_fltr4)" "" asg _fltr4 {\44}
  187.  if = \fdef(_\m(_scrno)_fltr5_l) 0 asg _fltr5 {\44}
  188.  else asg _fltr5 \ftrim(\fscrnstr(\fdef(_\m(_scrno)_fltr5_r)
  189.                              \fdef(_\m(_scrno)_fltr5_c)
  190.                              \fdef(_\m(_scrno)_fltr5_l)))
  191.  if eq "\m(_fltr5)" "" asg _fltr5 {\44}
  192.  if = \fdef(_\m(_scrno)_fltr6_l) 0 asg _fltr6 {\44}
  193.  else asg _fltr6 \ftrim(\fscrnstr(\fdef(_\m(_scrno)_fltr6_r)
  194.                              \fdef(_\m(_scrno)_fltr6_c)
  195.                              \fdef(_\m(_scrno)_fltr6_l)))
  196.  if eq "\m(_fltr6)" "" asg _fltr6 {\44}
  197.  if = \fdef(_\m(_scrno)_fltr7_l) 0 asg _fltr7 {\44}
  198.  else asg _fltr7 \ftrim(\fscrnstr(\fdef(_\m(_scrno)_fltr7_r)
  199.                              \fdef(_\m(_scrno)_fltr7_c)
  200.                              \fdef(_\m(_scrno)_fltr7_l)))
  201.  if eq "\m(_fltr7)" "" asg _fltr7 {\44}
  202.  run CSAPI32 \m(_systemname)\{32}\fdef(_\m(_scrno)_spylnk)\{32}-
  203. \m(_fltr1)\{32}\m(_fltr2)\{32}\m(_fltr3)\{32}\m(_fltr4)\{32}\m(_fltr5)\{32}-
  204. \m(_fltr6)\{32}\m(_fltr7)\{32}\m(_interactive)\{32}\m(_showduplicates)\{32}-
  205. \m(_usefilter)
  206.  :END
  207.  if terminal-macro connect
  208. }
  209.  
  210. ; Set the key that was chosen in SCRP.CFG to execute the READSCRN macro
  211. ; ;
  212. set key \m(_keymapped) \Kreadscrn
  213.  
  214. :END
  215.  
  216. <end SCRAPEIT.KSC>
  217.  
  218. <begin SCRP.CFG>
  219.  
  220. ; Set general parameters
  221.  
  222. ; This is the key that is mapped to invoke the image viewing code.
  223. ; Default mapping is F1.
  224.  
  225. ; For a list of scan codes to choose a different key, see the "Table of
  226. ; Keyboard Key Codes" in the online manual.  Four backslashes are required
  227. ; to have an actual backslash persist through several variable assignments
  228. ; as the backslash is the Kermit script language quoting character.
  229.  
  230. keymapped    \\\\368
  231.  
  232. ; General command line parameters for CSAPI32
  233. systemname     *DEFAULT
  234. interactive     NO
  235. showduplicates     YES
  236. usefilter     NO
  237.  
  238. ; List of different unique screen text
  239. screenname1    {Sample Screen 1}
  240. screenname2    {Sample Screen 2}
  241.  
  242. ; Maximum of unique screens the READSCRN macro is expected to deal with
  243. ; *It is important that this is updated to reflect the actual number of
  244. ; screens because READSCRN will not look for more than the number of unique
  245. ; screens given here.*
  246. maxscreens    2
  247.  
  248. ; Parameters for screename1
  249. ; First the location for the unique text
  250. scr1_name_r    0
  251. scr1_name_c    0
  252. scr1_name_l    0
  253.  
  254. ; Spylink name
  255. scr1_spylnk    INVOICES
  256.  
  257. ; Filter screen coordinates - screename1.
  258. ; Replace the 0's with actual numbers for your application.
  259. scr1_fltr1_r    0
  260. scr1_fltr1_c    0
  261. scr1_fltr1_l    0
  262. scr1_fltr2_r    0
  263. scr1_fltr2_c    0
  264. scr1_fltr2_l    0
  265. scr1_fltr3_r    0
  266. scr1_fltr3_c    0
  267. scr1_fltr3_l    0
  268. scr1_fltr4_r    0
  269. scr1_fltr4_c    0
  270. scr1_fltr4_l    0
  271. scr1_fltr5_r    0
  272. scr1_fltr5_c    0
  273. scr1_fltr5_l    0
  274. scr1_fltr6_r    0
  275. scr1_fltr6_c    0
  276. scr1_fltr6_l    0
  277. scr1_fltr7_r    0
  278. scr1_fltr7_c    0
  279. scr1_fltr7_l    0
  280.  
  281. ; Parameters for screename2
  282. ; First the location for the unique text
  283. scr2_name_r    0
  284. scr2_name_c    0
  285. scr2_name_l    0
  286.  
  287. ; Spylink name
  288. scr2_spylnk    BILLING
  289.  
  290. ; Filter screen coordinates - screename2
  291. scr2_fltr1_r    0
  292. scr2_fltr1_c    0
  293. scr2_fltr1_l    0
  294. scr2_fltr2_r    0
  295. scr2_fltr2_c    0
  296. scr2_fltr2_l    0
  297. scr2_fltr3_r    0
  298. scr2_fltr3_c    0
  299. scr2_fltr3_l    0
  300. scr2_fltr4_r    0
  301. scr2_fltr4_c    0
  302. scr2_fltr4_l    0
  303. scr2_fltr5_r    0
  304. scr2_fltr5_c    0
  305. scr2_fltr5_l    0
  306. scr2_fltr6_r    0
  307. scr2_fltr6_c    0
  308. scr2_fltr6_l    0
  309. scr2_fltr7_r    0
  310. scr2_fltr7_c    0
  311. scr2_fltr7_l    0
  312.  
  313. ; <end SCRP.CFG>
  314.