Jump to content
rony2006

How to make a script to be a function in another script?

Recommended Posts

rony2006

Hello, 

I have the following working script:

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Outfile=ocr.Exe
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
;r-OCR
;By: ronriel

;updated oct. 20, 2008
;autoit-v3.2.12.1
;Tested  Windows XP SP2 with MS Office 2007





#Include <ScreenCapture.au3>
#include <WindowsConstants.au3>
#Include <Misc.au3>




_Singleton("script",0)
Opt("GUIOnEventMode",1)
Opt("WinTitleMatchMode", 4)
Opt("TrayOnEventMode",1)
Opt("TrayMenuMode",1)
HotKeySet("!q","ShowWin")

Local $err = ObjEvent("AutoIt.Error","Error")

$GUI = GUICreate("", 90, 40,-1,-1,0x80880000,$WS_EX_TOOLWINDOW)
WinSetTrans($GUI,'',200)
$B1 = GUICtrlCreateButton("Read &Region", 0, 0, 90, 20)
GUICtrlSetBkColor(-1,0xe6e6fa)
GUICtrlSetOnEvent(-1,"Capture")
$B2 = GUICtrlCreateButton("Read &File", 0, 20, 90, 20)
GUICtrlSetBkColor(-1,0xe6e6fa)
GUICtrlSetOnEvent(-1,"Capture")
GUISetState(@SW_SHOW)

$GUI2 =  GUICreate("", 0 , 0 , 0, 0,  BitOR($WS_POPUP,$WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xe6e6fa,$GUI2)
WinSetTrans($GUI2,'',60)
GUISetState(@SW_HIDE)

$GUI3 = GUICreate("", 0 , 0 , 0,0 ,  BitOR($WS_POPUP,$WS_BORDER), BitOR($WS_EX_TOPMOST, $WS_EX_TOOLWINDOW))
GUISetBkColor(0xFF0000,$GUI3)
GUISetState(@SW_HIDE)









TrayCreateItem("Info")
TrayItemSetOnEvent(-1,'Ex')
$Exit = TrayCreateItem("Exit")
TrayItemSetOnEvent(-1,'Ex')
TraySetState()
ControlClick($GUI,'',$B2)

Func ShowWin()
    If BitAND(WinGetState($GUI),2) Then
        GUISetState(@SW_HIDE,$GUI)
    Else
        $Mpos = MouseGetPos()
        WinMove($GUI,'',$Mpos[0]-45,$Mpos[1]-10)
        GUISetState(@SW_SHOW,$GUI)
    EndIf
EndFunc

Func Capture()
    Local $Image = @ScriptDir&"\ScreenCapture.png", $v = 0

    If @GUI_CtrlId = $B1 Then
        GUISetState(@SW_HIDE,$GUI)
        GUISetState(@SW_SHOW,$GUI3)
        Do
            Sleep(10)
            ToolTip("Read Region")
            $c = MouseGetPos()
            WinMove($GUI3,"",$c[0]-2.5,$c[1]-2.5,5,5)
            If _IsPressed(01) Then
                ToolTip('')
                $C1 = MouseGetPos()
                    Do
                        Sleep(10)
                        $C2 = MouseGetPos()
                            If $C2[0] <> $C1[0] And $C2[1] <> $C1[1] Then
                                GUISetState(@SW_SHOW,$GUI2)
                            EndIf
                            If $C1[0] < $C2[0] Then
                                $X = $C1[0]
                                $W = $C2[0] - $C1[0]
                            Else
                                $X = $C2[0]
                                $W = $C1[0] - $C2[0]
                            EndIf
                            If $C1[1] < $C2[1] Then
                                $Y = $C1[1]
                                $H = $C2[1] - $C1[1]
                            Else
                                $Y = $C2[1]
                                $H = $C1[1] - $C2[1]
                            EndIf
                        WinMove($GUI2,"",$X,$Y,$W,$H)
                        WinMove($GUI3,"",$c2[0]-2.5,$c2[1]-2.5,5,5)
                    Until Not _IsPressed(01)
                $v = 1
            EndIf
        Until $v = 1
        GUISetState(@SW_HIDE,$GUI2)
        GUISetState(@SW_HIDE,$GUI3)
        _ScreenCapture_Capture($Image,$x,$y,$x+$W,$y+$h,False)
    ElseIf @GUI_CtrlId = $B2 Then
        GUISetState(@SW_HIDE,$GUI)
        $Image = @ScriptDir & "\res\effstk.jpg"
        if @error then Return
    EndIf
    $OCR = OCR($Image)


$OCR1= StringRegExpReplace($OCR, "[^[:digit:]]", "")
    If @error then Return
    ClipPut($OCR1)
    ;msgbox (0, "ocr", $OCR1)
    Exit
EndFunc

Func OCR($Image)
    Local  $y = 0, $string

    $miDoc = ObjCreate("MODI.Document")
    $miDoc.Create($Image)
    If @error Then Return SetError(1)

    $miDoc.Ocr(9, True, True);language is English
    If @error Then
        Exit
        ClipPut("Error!")
        Return SetError(1)
    EndIf

    For $Word In $miDoc.Images(0).Layout.Words
        $delim = ' '
        If $miDoc.Images(0).Layout.Words($y).Rects(0).Bottom < $miDoc.Images(0).Layout.Words($y+1).Rects(0).Top Then $delim = @CRLF
        $string &= $Word.text & $delim
        $y += 1
    Next
Return StringTrimRight($string,1)
EndFunc




Func Error()
Endfunc

Func Ex()
    If @TRAY_ID = $Exit Then
        Exit
    Else
        MsgBox(0,"Info", "Press ALT+Q to start."&@CRLF&@CRLF&"r-OCR"&@CRLF&"By: ronriel (ronriel@yahoo.com)")
    EndIf
EndFunc

While 2
Sleep(100000)
WEnd

 

I want to include this script in a main script, and when I need to run the script from above to just "call" the function of it.

I tried to put Func blabla() Endfunc in the script from above and then blabla() in the main script but is not working.

 

My main script is made like:

#include script from above

Gui

While 1

code

blabla() 

 

Wend

 

 

How I can do this please?

Share this post


Link to post
Share on other sites
rony2006

This is the metode that I use now but is a little lag there. :(

And i cannot see the time of execution of the full script, from begining to end.

Share this post


Link to post
Share on other sites
rony2006

I only want to OCR a image from a directory and get the text in a variable.

 

Any help please?

Share this post


Link to post
Share on other sites
Melba23

rony2006,

Recast the script so that it reads as follows:

; Includes

; Opts & HotKeys

; Variable declarations

Main() ; Choose a suitable name for the function to use here

Func Main()

    ; The working code here

EndFunc

; And then the helper functions here

Func #####

EndFunc

Now you can include the file in your other scripts and simply call the function - which is how the various standard include files and all of the UDFs work.

M23


Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites
rony2006

@Melba23

I noticed that I already have a working function with the name "Capture ()".

I think this is the working code.

 

So I included ocr.au3 in my main.au3 but each time I run main.au3, only the ocr.au3 is runned (without calling any function from it).

 

My include section from main looks like:

#include <ImageSearch.au3>
#include <MsgBoxConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <ScreenCapture.au3>
#Include <Misc.au3>
#Include <ocr.au3>

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×