|
 |
You are here: iOpus > iMacros > Support > iMacrosTutorial: FoxPro
Tutorial: FoxPro
Prerequisites: iMacros Scripting Edition
Goal: Remote-control the iMacros Web browser with FoxPro to automate your web tasks. To achieve this, we call the iMacros Scripting Interface directly from FoxPro .
Prerequisites:
This tutorial and the downloads were provided by Jeff Grippe.
All FoxPro elements (Project Files, Form Files, Report Files, etc.) are stored in FoxPro Tables. This project consists of a single project and a single form. The only exception to this is programs which are stored in ASCII text files and have .PRG extensions.
There is also a program file that is necessary to compile this into a runable EXE. The project and form runs in the FoxPro IDE. It also contains the code for the PRG (program) which is needed to compile into a runable EXE (it is two lines of code)
The sample code also contains FoxPro placement code for Visual Basic's SPLIT function which is not found in FoxPro. The SPLIT function is often used in the VBS and VBA code examples because it is an easy way to process the data returned by the iMacros EXTRACT command. The source code below includes comments about what SPLIT does and how it was coded for FoxPro.
The following FoxPro example program is an exact translation of the Visual Basic example program that comes with iMacros.
The form is called form1 (the default name)
It has the following form variables:
-
iim1 - used to hold an instance of the iMacros class
-
lbl_message - used to hold the text of the message displayed in the status text box
-
scmdline - used to hold the parameters which may be need for iim such as -tray
It has the following controls:
-
label1 - label1 has a caption property that is set to "Visual FoxPro Example Program"
-
label2 - label2 has a caption property that is set to "Status"
-
text1 - text1 is used to display the status information. Its ControlSource property is set to thisform.lbl_message
-
command1 - command1 is the button whose caption is "Get US$ Exchange Rate"
-
command2 - command2 is the button whose caption is "Order Lunch"
-
command3 - command3 is the button whose caption is "Order Dinner"
-
check1 - check1 is the checkbox whose caption is "Run macros in system tray". There is no variable attached to this control. It value is queried using the expression thisform.check1.value
It has the following code:
Note: In FoxPro a line beginning with a * is a comment. Comments can also be placed at the end of a line using &&
1. The Init Method of the form
| thisform.iim1 = CREATEOBJECT("imacros") && create an instance of the iMacros Class |
2. The Click Method of Command1
| LOCAL iret, iplay, sdata, spos, d0, d1
* Initialize and start IM browser
IF thisform.check1.Value = 1 && if the run in system tray option is checked
thisform.sCmdline = "-tray" && this parameter is needed
ELSE
thisform.sCmdline = ""
ENDIF
iret = thisform.iim1.iimGetInterfaceVersion()
thisform.lbl_message = "Version: " + iret
thisform.Refresh
iret = thisform.iim1.iimInit(thisform.scmdline, .t.)
* Tip: With iret = thisform.imm1.immInit(thisform.scmdline, .f.) you can ask the Scripting Interface to
* connect to an already open IM browser. If there is no IM browser open, the return value will be negative.
iret = thisform.iim1.iimDisplay("Extract Example")
* Set a new command line for the next PLAY command
IF thisform.check1.Value = 1 && if the run in system tray option is checked
iret = thisform.iim1.iimset("-tray", "")
ENDIF
* Note: If you do not want a tray icon to appear, use "-silent" instead of "-tray"
* Normally the Scripting Interface waits about 10 seconds before a Scripting Interface
* timeout error occurs (for example, if a user closed the IM browser while it was running,
* please do not confuse this with the BROWSER timeout errors if a web page loads to slow.)
* iret = thisform.iim1.iimSetInternal("INTERFACETIMEOUT", 1) && reduce the interface timeout to 1s
iplay = thisform.iim1.iimPlay("wsh-extract-rate")
sdata = thisform.iim1.iimGetLastMessage()
iret = thisform.iim1.iimExit
DO CASE
CASE iplay = 2
thisform.lbl_message = "Done!"
thisform.refresh
* VB code uses Split function to split the string around the text "[EXTRACT]"
* and place the result into an array
* The UBound function is used to check if the text "[EXTRACT]" was actually found
* ---------- original VB Code ----------------
* sSplit = Split(sdata, "[EXTRACT]")
* d0 = sSplit(0)
* if UBound(Ssplit) > 0
* d1 = sSplit(1)
* endif
*
* VFP "SPLIT" code with use AT and SUBSTR:
spos = AT("[EXTRACT]", sdata) && look for "[EXTRACT]" in the data
IF spos = 0 && [EXTRACT] not found in data
d0 = sdata && set d0 to the entire string
d1 = ""
ELSE
d0 = SUBSTR(sdata, 1, spos-1) && set d0 to the first part of the string
d1 = SUBSTR(sdata, spos+9, LEN(sdata)-(spos+19))
ENDIF
=MESSAGEBOX("One US$ costs " + d0 + " EURO or " + d1 + " British Pounds (GBP)")
thisform.lbl_message = "Done!"
thisform.Refresh
CASE iplay = 1
thisform.lbl_message = "Done, but no data extracted"
thisform.Refresh
CASE iplay < 0
thisform.lbl_message = sData
thisform.Refresh
ENDCASE
RETURN |
3. The Click Method of Command2
| * Order Lunch
LOCAL iret
thisform.lbl_message = "Running..."
thisform.Refresh
IF thisform.check1.Value = 1 && if run macros in system tray checked
iret = thisform.iim1.iimInit("-tray")
ELSE
iret = thisform.iim1.iimInit()
ENDIF
iret = thisform.iim1.iimDisplay("Start First Part")
IF thisform.check1.Value = 1 && if run macros in system tray checked
iret = thisform.iim1.iimSet("-tray", "")
ENDIF
iret = thisform.iim1.iimPlay("wsh-start")
IF iret < 0
thisform.lbl_message = thisform.iim1.iimGetLastMessage()
thisform.refresh
ENDIF
iret = thisform.iim1.iimDisplay("Select lunch")
iret = thisform.iim1.iimPlay("wsh-lunch")
IF iret < 0
thisform.lbl_message = thisform.iim1.GetLastMessage()
thisform.Refresh
ENDIF
iret = thisform.iim1.iimDisplay("Submit Form")
iret = thisform.iim1.iimPlay("wsh-submit-button")
* Typically "iimplay" is the only command where errors can occur
IF iret < 0
thisform.lbl_message = thisform.iim1.iimGetLastMessage()
ELSE
thisform.lbl_message = "Done!"
ENDIF
thisform.Refresh
iret = thisform.iim1.iimExit
RETURN |
4. The Click Method of Command3
| * Order Dinner
LOCAL iret
thisform.lbl_message = "Running..."
thisform.Refresh
IF thisform.check1.Value = 1 && if run macros in system tray checked
iret = thisform.iim1.iimInit("-tray")
ELSE
iret = thisform.iim1.iimInit()
ENDIF
iret = thisform.iim1.iimDisplay("Start First Part")
IF thisform.check1.Value = 1 && if run macros in system tray checked
iret = thisform.iim1.iimSet("-tray", "")
ENDIF
iret = thisform.iim1.iimPlay("wsh-start")
IF iret < 0
thisform.lbl_message = thisform.iim1.iimGetLastMessage()
thisform.refresh
ENDIF
iret = thisform.iim1.iimDisplay("Select dinner")
iret = thisform.iim1.iimPlay("wsh-dinner")
IF iret < 0
thisform.lbl_message = thisform.iim1.GetLastMessage()
thisform.Refresh
ENDIF
iret = thisform.iim1.iimDisplay("Submit Form")
iret = thisform.iim1.iimPlay("wsh-submit-button")
* Typically "iimplay" is the only command where errors can occur
* (for example, a timeout error)
IF iret < 0
thisform.lbl_message = thisform.iim1.iimGetLastMessage()
ELSE
thisform.lbl_message = "Done!"
ENDIF
thisform.Refresh
iret = thisform.iim1.iimExit
RETURN |
The Program file (PRG) that would be required would have the following lines of code:
DO FORM FORM1
READ EVENTS |
The form would also need the following line in its Unload Method
|