AutoHotkey Community

It is currently May 26th, 2012, 10:18 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 7 posts ] 
Author Message
PostPosted: October 24th, 2009, 7:38 am 
Hello!

Lets just start off with... I love AHK. I am pretty new but have made about 30 or so scripts that have helped automate many simple but repetitive tasks for the company I work for. Feedback is greatly appreciated. Like to learn.

I want to automate pulling data from this application. It's a Tera Term session via an SSH connection to a county system. Main reason I am doing it is because we need an easy way to capture certain screens and save it into our electronic imaging system.

The issue I am having is as follows. I want to parse out the lower menu portion of the screen that I am capturing to the clipboard. I was trying to use regexreplace but I can only get it to work on a couple of the strings.

The screen shot below is what I am capturing. I have different menu options depending on the screen but most are very similar.

I was wondering if there are restrictions that I am just overlooking in the documentation.

Can I not load the whole section into a variable... are there issues with spaces? I dont want to lose the format of the clipboard section. Ideas? Open comments about the code structure?

Once I get the menu portion off I want to use the "continued.." statement that will be left as the last line to know if I need to navigate to the next screen.

Screen Start.... (the formatting of this is all wacked out due to being pasted here.) in red is what I want to work with.


- - Property Account Summary (R2040816) - -
Property: R2040816 2S132CD-00800 088.10 HOOBER, ROBERT & DIANA (556979)
CRESTWOOD HEIGHTS, LOT 6, ACRES .19 15646 SW WILLOW CT
SHERWOOD, OR 97140


All Batch: Inq - Eff Date Paid: 11/16/2009
ID# Bill ID Levied Tax Tax Paid Interest Amount Paid Date Paid
--- -------------- ---------- ---------- --------- ----------- -----------
1. 1999.23681 0 2,119.44 2,055.86 <63.58> 2,055.86 11/15/99
2. 2000.56468 0 2,151.71 2,087.16 <64.55> 2,087.16 11/09/00
3. 2001.158096 0 2,358.36 2,287.61 <70.75> 2,287.61 11/09/01
4. 2002.147255 0 2,378.20 2,306.85 <71.35> 2,306.85 10/23/02
5. 2003.147949 0 2,421.00 2,420.59 161.81 2,582.40 09/10/04
Write Off 0.41
6. 2004.282804 0 2,453.93 2,380.31 <73.62> 2,380.31 11/15/04
7. 2005.287448 0 2,480.70 2,406.28 <74.42> 2,406.28 11/10/05
8. 2006.292672 0 2,497.49 2,422.57 <74.92> 2,422.57 11/13/06
*** Continued ***
qqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqq
(N)ext Page x (D#) Down x (B)ottom x (.) More
qqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqq
Enter Option From Above, or or 'X' to Exit: __

Screen End...


Here is my code... (about 70% done)

Code:
; Assembled by Shane Saviers
; Version: 1.1
; Dated: 10/20/2009

;TEST PARCEL 2028862

DelMenu1 :=
(
"qqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqq"
)

DelMenu2 :=
(
" (N)ext Page       x (D#) Down         x (B)ottom          x (\.) More"
)

DelMenu3 :=
(
"qqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqq"
)

DelMenu4 :=
(
"Enter Option From Above\, or  or 'X' to Exit: __"
)



#SingleInstance force

; Interface GUI!

Gui +AlwaysOnTop
Gui, 1:Font, s8, Arial
Gui, Add, radio, vTicor, Ticor
Gui, Add, radio, vFidelity, Fidelity
Gui, Add, radio, vChicago, Chicago
Gui, Add, radio, vLawyers, Lawyers
Gui, Add, Text,, Parcel Number:
Gui, Add, Edit, vParcelNumber
Gui, Add, Button, w120 default, Login
Gui, Add, Button, w120, Auto
Gui, Add, Button, w120, Clear
Gui, Add, Button, w120, Get
Gui, Add, Button, w120, View
Gui, Show, w140 h285 x742-y78, WATAX
return



GuiClose:
Gui Destroy
ExitApp




ButtonClear:
clipboard =
return


;Scrape Screen contents.
ButtonGet:
Gui, Show, w140 h285 x742-y78, WATAX
WinWait, 111.222.333.44:00 - Tera Term VT,
IfWinNotActive, 111.222.333.44:00 - Tera Term VT, , WinActivate, 111.222.333.44:00 - Tera Term VT,
WinWaitActive, 111.222.333.44:00 - Tera Term VT,
clipboard = %clipboard%
ClipSaved := clipboard
sleep 500
Send, {ALTDOWN}{ALTUP}{RIGHT}{UP}{UP}{UP}{ENTER}
send, {ALTDOWN}{ALTUP}{RIGHT}{UP}{UP}{UP}{UP}{ENTER}
clipboard = %ClipSaved%`r`n%clipboard%
;Replace spacing with underscore character
ClipboardParse := RegExReplace(clipboard,DelMenu4,"")
;ClipboardParse := RegExReplace(clipboard,DelMenu2,"")
;ClipboardParse := RegExReplace(clipboard,DelMenu3,"")
;ClipboardParse := RegExReplace(clipboard,DelMenu4,"")
msgbox %ClipboardParse%
return

ButtonView:
FileDelete, %A_MyDocuments%_%Username%.html
sleep 2500
FileAppend,
(
   <html>
      <head>
      <style type="text/css">
         h1 {color:#333; background-color: #ddd; padding: 3px 10px;}
         p {color:blue}
         body {text-align: center;}
         #container {margin-left: auto; margin-right: auto; width: 700px; text-align: left; padding: 3px 20px;)
      </style>
      </head>
         <body>
            <div id="container">
               <h1>Washington County Taxes</h1>
                  <pre>
                     <p>
                        %Clipboard%
                     </p>
                  </pre>
            </div>
         </body>
   </html>
), %A_MyDocuments%_%A_Username%.html

sleep 1500
Run, %A_MyDocuments%_%A_Username%.html

Gui, Show, w140 h285, Tax
return




ButtonLogin:
Gui, Submit

If (Ticor = 1)
{
   LoginID := "user"
   Password := "pass"   
}
Else
If (Fidelity = 1)
{
   LoginID := "user"
   Password := "pass"
}
Else
If (Chicago = 1)
{
   LoginID := "user"
   Password := "pass"
}
Else
If (Lawyers = 1)
{
   LoginID := "user"
   Password := "pass"
}
Else
{
   msgbox, Please Select Brand
   Gui, Show, w140 h285, Tax
return
}


Run, %ProgramFiles%\teraterm\ttermpro.exe /f=\\orportla-apl02\WaTax\WaTax.INI,,UseErrorLevel
If ErrorLevel
{

   MsgBox,48,ERROR,Sorry, It appears you don't have Terra Term installed `n`nIf you need assistance please contact PacNW IT.

   Exit,   
}
Else

;Trim whitespace at the START AND END of a string:
ParcelNumber := RegExReplace(OrderNumber, "(^\s+)|(\s+$)")
;Replace spacing with underscore character
;StringReplace,Name,Name,%A_SPACE%,_,All


WinWait, Tera Term: New connection,
IfWinNotActive, Tera Term: New connection, , WinActivate, Tera Term: New connection,
WinWaitActive, Tera Term: New connection,

send, {ENTER}

WinWait, SSH Authentication,
IfWinNotActive, SSH Authentication, , WinActivate, SSH Authentication,
WinWaitActive, SSH Authentication,
sleep 1000
Send, %LoginID%
sleep 500
Send, {SHIFTDOWN}{TAB}{TAB}{SHIFTUP}
sleep 500
Send, {ENTER}{SPACE}

IfWinNotActive, SSH Authentication Challenge, , WinActivate, SSH Authentication Challenge,
WinWaitActive, SSH Authentication Challenge,
sleep 500
Send, %Password%{ENTER}

WinWait, 111.222.333.44:00 - Tera Term VT,
IfWinNotActive, 111.222.333.44:00 - Tera Term VT, , WinActivate, 111.222.333.44:00 - Tera Term VT,
WinWaitActive, 111.222.333.44:00 - Tera Term VT,
sleep 2500
Send, %Password%{ENTER}

sleep 2000
send, 1{ENTER}

sleep 2000
send, inq{ENTER}

sleep 1500
send, inq{ENTER}

sleep 2000
send, 1{ENTER}

sleep 2000
send, 1{ENTER}

Gui, Show, w140 h285, Tax
return




ButtonAuto:
Gui, Submit

If (Ticor = 1)
{
   LoginID := "user"
   Password :="pass"
}
Else
If (Fidelity = 1)
{
   LoginID := "user"
   Password :="pass"   
}
Else
If (Chicago = 1)
{
   LoginID := "user"
   Password :="pass"
}
Else
If (Lawyers = 1)
{
   LoginID := "user"
   Password :="pass"
}
Else
{
   msgbox, Please Select Brand
   Gui, Show, w140 h285, Tax
return
}


Run, %ProgramFiles%\teraterm\ttermpro.exe /f=\\orportla-apl02\WaTax\WaTax.INI,,UseErrorLevel
If ErrorLevel
{

   MsgBox,48,ERROR,Sorry, It appears you don't have Terra Term installed `n`nIf you need assistance please contact PacNW IT.

   Exit,   
}
Else

;Trim whitespace at the START AND END of a string:
ParcelNumber := RegExReplace(OrderNumber, "(^\s+)|(\s+$)")
;Replace spacing with underscore character
;StringReplace,Name,Name,%A_SPACE%,_,All


WinWait, Tera Term: New connection,
IfWinNotActive, Tera Term: New connection, , WinActivate, Tera Term: New connection,
WinWaitActive, Tera Term: New connection,

send, {ENTER}

WinWait, SSH Authentication,
IfWinNotActive, SSH Authentication, , WinActivate, SSH Authentication,
WinWaitActive, SSH Authentication,
sleep 1000
Send, %LoginID%
sleep 500
Send, {SHIFTDOWN}{TAB}{TAB}{SHIFTUP}
sleep 500
Send, {ENTER}{SPACE}

IfWinNotActive, SSH Authentication Challenge, , WinActivate, SSH Authentication Challenge,
WinWaitActive, SSH Authentication Challenge,
sleep 500
Send, %Password%{ENTER}

WinWait, 111.222.333.44:00 - Tera Term VT,
IfWinNotActive, 111.222.333.44:00 - Tera Term VT, , WinActivate, 111.222.333.44:00 - Tera Term VT,
WinWaitActive, 111.222.333.44:00 - Tera Term VT,
sleep 2500
Send, %Password%{ENTER}

sleep 2000
send, 1{ENTER}

sleep 2000
send, inq{ENTER}

sleep 1500
send, inq{ENTER}

sleep 3500
send, 1{ENTER}

sleep 2000
send, %ParcelNumber%
send, {ENTER}

Gui, Show, w140 h285, Tax
return







#Persistent
Menu, tray, add
Menu, tray, add, About..., About
Menu, tray, add, Reload Script, Reload
return


; Reload request...
Reload:
Reload
Sleep 1000 ; If successful, the reload will close this instance during the Sleep, so the line below will never be reached.
MsgBox, 1,, The script could not be reloaded. Please try closing and re-opening.
return


; About splash screen!
About:
SplashTextOn, 400, 120, About..., `nThank you for using this script!`n If for some reason you have problems `nwith this script use the "Reload" option and `n if that fails please feel free to contact me at `n shane.saviers@somewhere.com
WinSet, Transparent, 200, About...
WinSet, Style, -0xC00000, About...
Gui, Add, Button, w50 default, OK
Sleep 8000
SplashTextOff


; End of Script...


Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: October 24th, 2009, 5:31 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
Wouldn't this get the appropriate section?

Code:
var=
(
- - Property Account Summary (R2040816) - -
Property: R2040816 2S132CD-00800 088.10 HOOBER, ROBERT & DIANA (556979)
CRESTWOOD HEIGHTS, LOT 6, ACRES .19 15646 SW WILLOW CT
SHERWOOD, OR 97140


All Batch: Inq - Eff Date Paid: 11/16/2009
ID# Bill ID Levied Tax Tax Paid Interest Amount Paid Date Paid
--- -------------- ---------- ---------- --------- ----------- -----------
1. 1999.23681 0 2,119.44 2,055.86 <63.58> 2,055.86 11/15/99
2. 2000.56468 0 2,151.71 2,087.16 <64.55> 2,087.16 11/09/00
3. 2001.158096 0 2,358.36 2,287.61 <70.75> 2,287.61 11/09/01
4. 2002.147255 0 2,378.20 2,306.85 <71.35> 2,306.85 10/23/02
5. 2003.147949 0 2,421.00 2,420.59 161.81 2,582.40 09/10/04
Write Off 0.41
6. 2004.282804 0 2,453.93 2,380.31 <73.62> 2,380.31 11/15/04
7. 2005.287448 0 2,480.70 2,406.28 <74.42> 2,406.28 11/10/05
8. 2006.292672 0 2,497.49 2,422.57 <74.92> 2,422.57 11/13/06
*** Continued ***
qqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqq
(N)ext Page x (D#) Down x (B)ottom x (.) More
qqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqq
Enter Option From Above, or or 'X' to Exit: __
Screen End...
)

!j::MsgBox % RegExReplace(var,"s).*?\*\*\* Continued \*\*\*\R(.*?)\RScreen End\.\.\.","$1")

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 24th, 2009, 5:52 pm 
Offline

Joined: August 22nd, 2009, 11:23 pm
Posts: 294
To keep the formating of your screen dump, use
ClipboardAll command

How it works ...
Quote:
ClipboardAll (saving and restoring everything on the clipboard)
ClipboardAll contains everything on the clipboard (such as pictures and formatting). It is most commonly used to save the clipboard's contents so that the script can temporarily use the clipboard for an operation. When the operation is completed, the script restores the original clipboard contents as shown below:

ClipSaved := ClipboardAll ; Save the entire clipboard to a variable of your choice.
; ... here make temporary use of the clipboard, such as for pasting Unicode text via Transform Unicode ...
Clipboard := ClipSaved ; Restore the original clipboard. Note the use of Clipboard (not ClipboardAll).
ClipSaved = ; Free the memory in case the clipboard was very large.ClipboardAll may also be saved to a file (in this mode, FileAppend always overwrites any existing file):
FileAppend, %ClipboardAll%, C:\Company Logo.clip ; The file extension does not matter.

To later load the file back onto the clipboard (or into a variable), follow this example:
FileRead, Clipboard, *c C:\Company Logo.clip ; Note the use of *c, which must precede the filename........

_________________
Image
"Man's quest for knowledge is an expanding series whose limit is infinity"


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 26th, 2009, 3:20 am 
Offline

Joined: October 24th, 2009, 7:50 am
Posts: 3
Location: Portland OR
sinkfaze wrote:
Wouldn't this get the appropriate section?

Code:
var=
(
*** Continued ***
qqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqwqqqqqqqqqqqqqqqqqqqq
(N)ext Page x (D#) Down x (B)ottom x (.) More
qqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqvqqqqqqqqqqqqqqqqqqqq
Enter Option From Above, or or 'X' to Exit: __
Screen End...
)

!j::MsgBox % RegExReplace(var,"s).*?\*\*\* Continued \*\*\*\R(.*?)\RScreen End\.\.\.","$1")



Thank you for responding.
Question on this one... Can you break down the sections of this that I dont follow... noted in red.

I think I understand what you are pretty much doing I just want to understand the syntax on this particular statement.

Thanks!


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 26th, 2009, 7:13 am 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5480
Location: the tunnel(?=light)
The s) option allows for the period to match all characters, meaning newlines (which don't typically match) will also match.

There are two different ways to use RegExReplace; one way is to match only the pattern you would like to replace in a given set of data, the other is to match the entire set of data and return only the pertinent matching portion via a backreference(s). For my example I chose to match the entire set of data, which would require me to use .*? to match everything from the beginning up to the relevant matching portion I really needed.

The line *** Continued *** ends with a newline character as does the line before Screen End..., but I don't need to include those newlines in the final matching data, so I used \R to specifically match those newlines so I could exclude them.

As mentioned above, one option for using RegExReplace is to match the entire set of data and specifically match only a subpattern you'd like to keep. To designate the subpattern you use the parenthesis:

Code:
RegExReplace(var,"s).*?\*\*\* Continued \*\*\*\R(.*?)\RScreen End\.\.\.")


Then in order to return the subpattern you call a backreference:

Code:
RegExReplace(var,"s).*?\*\*\* Continued \*\*\*\R(.*?)\RScreen End\.\.\.","$1")


If you have multiple subpatterns you would call backreferences in their matching order i.e. the first subpattern in your regex statement would be called by $1, the second by $2, etc.

To be honest, I could've made the statement shorter but I wanted to use a more blatant example in case you weren't familiar with regular expressions:

Code:
RegExReplace(var,"s).*?(q+.*?)\RScreen End.*","$1")

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 26th, 2009, 10:52 pm 
Offline

Joined: October 24th, 2009, 7:50 am
Posts: 3
Location: Portland OR
I assume that this would give me the value of the replaced text.
Code:
vRemoved := RegExReplace(var,"s).*?(q+.*?)\RScreen End.*","$1")

what would give me the result set of the first portion of the text stream that I was not replacing?

would I just run the regex statement with out assigning it a variable?
Code:
RegExReplace(var,"s).*?(q+.*?)\RScreen End.*","$1")


This is kind of how i am trying to incorporate it into the screen scrape.

Code:
clipboard = %ClipSaved%`r`n%clipboard%
Loop, parse, clipboard, %A_Tab%
{
   If A_loopField contains *** Continued ***
   {   
      RegExReplace(var,"s).*?(q+.*?)\RScreen End.*","$1")
      ;Send, N{ENTER}
      ;sleep 2000
      ;GoSub, GetNext

   }
   Else
   If A_loopField contains *** End of Display ***
   {   
       RegExReplace(var,"s).*?(q+.*?)\RScreen End.*","$1")
       ;send, {ENTER}                     
       msgbox Taxes Should be Complete Select View to proceed.
   }
}


I am thinking it would be easier to just say start at a certain word like *** Continued *** and delete to the end of the page as my next statement would be to append the next clipboard item and search for Continued or End of display before writing to an html file for display.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: October 28th, 2009, 8:58 am 
Offline

Joined: October 24th, 2009, 7:50 am
Posts: 3
Location: Portland OR
Hello again! I got this working for the most part using the great documentation that sinkfaze has around on various threads.

My questions is this... Here is my primary code I am using for the screen scrape process.

Code:
Loop, parse, clipboard, %A_Tab%
{
   If A_loopField contains *** Continued ***
   {   
      clipboard := RegExReplace(clipboard,"s)\*\*\* Continued \*\*\*.*?Exit: \_\_*\R*\R*\R*","$1")
      Send, N{ENTER}
      sleep 2000
      clipboard = %ClipSaved%%clipboard%
      GoSub, ButtonGet
   }
   Else
   If A_loopField contains *** End of Display ***
   {
      clipboard := RegExReplace(clipboard,"s).*?- -  P.*?Paid: ","$1")
      clipboard := RegExReplace(clipboard,"s)\*\*\* End of Display \*\*\*.*?Exit: \_\_*","$1")
      StringTrimLeft, clipboard, clipboard, 10
      StringReplace, clipboard, clipboard, Tax Due   Interest   Annual Due  Balance Due,`r`n %A_Tab%%A_Tab%%A_Tab%%A_Space%%A_Space%Tax Due   Interest   Annual Due  Balance Due
      clipboard = %ClipSaved%%clipboard%
      msgbox Taxes Should be Complete Select View to proceed.`r`n%clipboard%
   }
}



I have been trying to pull out header and footer information that is reapeated each screen and marry it up nice and pretty.

My current result is this...

Code:
Property: R50303  1S17DC-03905  051.50         CHAVEZ, JUAN (504478)
   JOHNSON ESTATE ADDITION TO                  4070 SW 173RD AVE
   BEAVERTON-REEDVILLE ACREAGE, LOT PT 280,    ALOHA, OR  97007
   ACRES .19

    All             Batch: Inq -                       Eff Date Paid: 11/16/2009
ID#         Bill ID  Levied Tax    Tax Paid   Interest  Amount Paid  Date Paid
---  --------------  ----------  ----------  ---------  -----------  -----------
1.      1998.8178 0    1,407.04    1,364.83    <42.21>     1,364.83  11/16/98
2.      1999.8335 0    1,443.63    1,400.32    <43.31>     1,400.32  11/02/99
3.    2000.115338 0    1,478.95    1,434.58    <44.37>     1,434.58  11/08/00
4.    2001.219026 0    1,633.44    1,584.44    <49.00>     1,584.44  11/09/01
5.    2002.242393 0    1,680.91    1,630.48    <50.43>     1,630.48  11/13/02
6.    2003.244404 0    1,909.89    1,852.59    <57.30>     1,852.59  11/05/03
7.    2004.221744 0    1,785.33    1,731.77    <53.56>     1,731.77  11/15/04
8.    2005.229108 0    1,990.24    1,930.53    <59.71>     1,930.53  11/15/05
9.    2006.231713 0    1,785.64    1,732.07    <53.57>     1,732.07  11/01/06

10.   2007.234439 0    1,992.73    1,932.95    <59.78>     1,932.95  11/13/07
                                   
            Tax Due   Interest   Annual Due  Balance Due
11.   2008.237722 0    2,042.96    2,042.96     245.15     2,288.11    2,288.11

   Third  Date Due  Levied Tax     Tax Due   Interest    Third Due  Balance Due
12.     Current Taxes for bill 2009.238559, Levied tax of 2,214.61
    1/3 due Nov 16       738.21      738.21                  738.21    3,026.32
    2/3                  738.20      738.20    <29.53>       708.67    3,734.99
    3/3                  738.20      738.20    <36.91>       701.29    4,436.28
                      Total Due:   2,214.61    <66.44>     2,148.17


I am stuck on getting item 9. to sit right on top of item 10. as if it was on the same page. Any ideas?

My other question would be is there a better way to implement this?

RegEx skills suck... but already grown over the past couple nights so hopefully one day I will be answering questons as apposed to asking all the time.

Greatly appreciate any help provided.
Thanks![/code]


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 7 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: AndyJenk, Cristi®, JSLover, Leef_me, patgenn123, rbrtryn, XstatyK and 71 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group