AndyS19

Calling object methods

9 posts in this topic

I'm writing code to manage print jobs.  The on-line searches I did showed different ways of calling a print job method.
I see these ways of calling print job methods:

$oPrintjob.Delete_   (Method name followed by underline)
$oPrintJob.Resume()  (Method name followed by open/closed parens)
$oPrintJob.Status    (Method name followed by nothing at all)

I don't understand syntax where the method name is followed by an underline.

 

When is it correct to use the method name followed by an underline vs the open/closed parens vs nothing at all?

Share this post


Link to post
Share on other sites



It appears that .Delete_ is just how it's called. If you want to delete a printer then you use .Delete_.

http://blogs.technet.com/b/heyscriptingguy/archive/2007/11/02/hey-scripting-guy-how-can-i-remove-all-the-network-printers-on-a-computer.aspx


Get Scite to add a popup when you use a 3rd party UDF -> http://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/user-calltip-manager.html

Share this post


Link to post
Share on other sites

In AutoIt the preferred method to call methods is with parens. So it is easier to distinguish them from properties. Methods can have parameters so parens are needed.

$oPrintjob.Delete_()
$oPrintJob.Resume()
$oPrintJob.Status() ; Is this really a method?

 


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-02-03 - Version 1.4.7.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-03 - Version 1.2.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

OK, but the .Delete() doesn't work

Here's a test script that shows that only the .Delete_ works; not .Delete() or just .Delete

#AutoIt3Wrapper_au3check_parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6
#include <GUIConstantsEx.au3>
#include <GuiButton.au3>
#include <WindowsConstants.au3>

Opt("MustDeclareVars", 1)

Global $iMemo

Global $sPrinterName = "Brother PT-2430PC"

_Main()

Func _Main()
    Local $iPrintTestPagebtn, $iShowAllJobs
    Local $iDelete1JobMu, $iDelete1JobMp, $iDelete1JobMn

    GUICreate("Methods", 500, 400)
    $iMemo = GUICtrlCreateEdit("", 10, 10, 475, 320, $WS_VSCROLL)
    GUICtrlSetFont($iMemo, 9, 400, 0, "Courier New")
    $iPrintTestPagebtn = GUICtrlCreateButton("Print Test Page", 10, 340, 100, 25)
    $iShowAllJobs = GUICtrlCreateButton("Show all Jobs", 110, 340, 100, 25)

    $iDelete1JobMu = GUICtrlCreateButton("Delete Using underline", 10, 367, 125, 25)
    $iDelete1JobMp = GUICtrlCreateButton("Delete 1 Using parens", 135, 367, 125, 25)
    $iDelete1JobMn = GUICtrlCreateButton("Delete 1 Using nothing", 260, 367, 125, 25)

    GUISetState()

    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                ExitLoop
            Case $iPrintTestPagebtn
                printTestPage($sPrinterName)
            Case $iShowAllJobs
                showAllPrintJobs($sPrinterName)
            Case $iDelete1JobMu
                delete1PrintJob($sPrinterName, "underline")
            Case $iDelete1JobMp
                delete1PrintJob($sPrinterName, "parens")
            Case $iDelete1JobMn
                delete1PrintJob($sPrinterName, "nothing")
        EndSwitch
    WEnd

    Exit
EndFunc   ;==>_Main

Func delete1PrintJob($sPrinterName, $sMethod)
    Local $wbemFlags, $oWMIService, $oPrintJobs, $oPrintJob, $thisprintername
    MemoWrite("")

    $oWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
    If (Not IsObj($oWMIService)) Then Return
    $wbemFlags = BitOR(0x20, 0x10) ; $wbemFlagReturnImmediately and wbemFlagForwardOnly
    $oPrintJobs = $oWMIService.ExecQuery("SELECT * FROM Win32_PrintJob", "WQL", $wbemFlags)

    For $oPrintJob In $oPrintJobs
        $thisprintername = StringSplit($oPrintJob.Name, ",", 2)[0]
        If (StringCompare($thisprintername, $sPrinterName) == 0) Then

            Switch ($sMethod)
                Case "underline"
                    $oPrintJob.Delete_
                Case "parens"
                    $oPrintJob.Delete()
                Case "nothing"
                    $oPrintJob.Delete
            EndSwitch

            MemoWrite("+++: Deleted: " & $oPrintJob.Document & " " & $oPrintJob.TimeSubmitted & @CRLF)
            ExitLoop
        EndIf
    Next
EndFunc   ;==>delete1PrintJob

Func showAllPrintJobs($sPrinterName)
    Local $wbemFlags, $oWMIService, $oPrintJobs, $oPrintJob, $cnt, $thisprintername
    MemoWrite("")

    $oWMIService = ObjGet("winmgmts:\\localhost\root\CIMV2")
    If (Not IsObj($oWMIService)) Then Return

    $cnt = 1
    $wbemFlags = BitOR(0x20, 0x10) ; $wbemFlagReturnImmediately and wbemFlagForwardOnly
    $oPrintJobs = $oWMIService.ExecQuery("SELECT * FROM Win32_PrintJob", "WQL", $wbemFlags)
    For $oPrintJob In $oPrintJobs
        $thisprintername = StringSplit($oPrintJob.Name, ",", 2)[0]
        If (StringCompare($thisprintername, $sPrinterName) == 0) Then
            MemoWrite("+++: PrintJob #" & $cnt & ": " & $oPrintJob.Document & " " & $oPrintJob.TimeSubmitted)
            $cnt += 1
        EndIf
    Next
EndFunc   ;==>showAllPrintJobs

Func printTestPage($sPrinterName)
    Local $params, $str
    MemoWrite("")

    $params = "/k " ; /k = Print Test Page
    $str = $params & " /n " & '"' & $sPrinterName & '"'

    DllCall("printui.dll", "none", "PrintUIEntryW", _
            "hwnd", 0, _
            "ptr", 0, _
            "wstr", $str, _
            "int", @SW_HIDE)

    MemoWrite("+++: Printing Test page" & @CRLF)
EndFunc   ;==>printTestPage

Func MemoWrite($sMessage)
    GUICtrlSetData($iMemo, StringStripWS($sMessage, 3) & @CRLF, 1)
EndFunc   ;==>MemoWrite

 

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

$oPrintJob.Status() ; Is this really a method?

not, this is property:

https://msdn.microsoft.com/en-us/library/aa394363(v=vs.85).aspx

 

Edit:
so this should be used like this:

Local $sPrintJob_Status = $oPrintJob.Status

 

Edited by mLipok

ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  for other useful stuff click the following button:

Spoiler

Last update: 2016-12-23
Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. 

My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft Components * TeamViewer API -UDF * JavaManagement UDF * VIES over SOAPPOP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF * WinSCP UDF *

My contribution to others projects: * _sql.au3 UDF  *

Useful links: * Forum Rules * Forum etiquette *  Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * 

Wiki: Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Best coding practices * 

IE Related:  * How to use IE.au3  UDF with  AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities *

I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions *  EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *

"Homo sum; humani nil a me alienum puto" - Publius Terentius Afer
"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, The Art of Computer Programming
:naughty:  :ranting:, be  :) and       \\//_.

Anticipating Errors :  "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty."

 

Share this post


Link to post
Share on other sites

OK, but the .Delete() doesn't work

I didn't say it should be .Delete(). I just said that every method should have parens when being used in AutoIt.
So it should be .Delete_().


My UDFs and Tutorials:

Spoiler

UDFs:
Active Directory (NEW 2017-02-03 - Version 1.4.7.0) - Download - General Help & Support - Example Scripts - Wiki
OutlookEX (NEW 2017-02-03 - Version 1.2.4.0) - Download - General Help & Support - Example Scripts - Wiki
ExcelChart (2015-04-01 - Version 0.4.0.0) - Download - General Help & Support - Example Scripts
Excel - Example Scripts - Wiki
Word - Wiki
Tutorials:
ADO - Wiki

 

Share this post


Link to post
Share on other sites

Oh, I see.  Do you know where the Delete_ (with the underline) is documented.  I've searched MDN and other sites, but I don't see anything.

 

Share this post


Link to post
Share on other sites

OK, thanks

 

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

  • Similar Content

    • genius257
      By genius257
      I've made a library, based on AutoItObject UDF with the goal of implementing getter and setter functionality and make it possible to define new object properties in as few steps as possible.
      Thank you to @trancexx for getting me on the right track, and all users in Hooking into the IDispatch interface for the code to get me going.
      If I've forgotten to add credit, please let me know
      Things to be added
      Garbage collection Method support Support for more/all AutoIt variable-types Accessors Inheritance equivalent of @error and @extended Example:
      #include <AutoItObject_Internal.au3> $oIDispatch = IDispatch() $__getter = DllCallbackRegister(CustomGetter, "long", "IDispatch;IDispatch") $__setter = DllCallbackRegister(CustomSetter, "long", "IDispatch;IDispatch") $oIDispatch.__defineGetter("a", $__getter) $oIDispatch.__defineSetter("a", $__setter) $oIDispatch.a = "start" MsgBox(0, "", $oIDispatch.a) $oIDispatch.a = "end" MsgBox(0, "", $oIDispatch.a) $oIDispatch.b = 3.14 $oIDispatch.b += $oIDispatch.b MsgBox(0, "", $oIDispatch.b) $oIDispatch.__defineMethod("c", FuncName(MsgBox)) $oIDispatch.c(0, "title", "text") Func CustomGetter($oIDispatch, $oIDispatch2);(return.value, this.value) $oIDispatch.a = "_" & $oIDispatch2.a & "_" EndFunc Func CustomSetter($oIDispatch, $oIDispatch2);(this.value, caller.value) $oIDispatch.a &= $oIDispatch2.a EndFunc  
      0.1.1
      AutoItObject_Internal.au3
      0.1.2
      AutoItObject_Internal.au3
    • WoodGrain
      By WoodGrain
      Hi All,
      I've coded the small script below, but it can't seem to get the instance of Windows Media player as it keeps going to @error, I've not used com objects before so any assistance would be appreciate. I already have WMP open and minimised. I retrieved "WMPlayerApp" from the AutoIT info tool, I've included a copy below.
      I'm using these sources:
      https://msdn.microsoft.com/en-us/library/dd564085.aspx
      https://msdn.microsoft.com/en-us/library/dd564018.aspx
      $oWMP = ObjGet("", "WMPlayerApp") If @error Then MsgBox(0, "Can't get WMP", "Couldn't connect to the WMP instance") Exit EndIf $wmpPlayState = $oWMP.playState MsgBox(0, "Play State", $wmpPlayState) $wmpSongName = $oWMP.currentMedia.name MsgBox(0, "Play State", $wmpSongName) I've also seen references to the below, but I want to get an existing open WMP:
      ObjCreate("wmplayer.OCX") and have looked at the WMP.udf but can't see how it will do either of the functions I've coded above.
       
    • harvester2001
      By harvester2001
      Hi
      I need help, I found script to check computer model. I want use it in loop to chceck multiple pc`s (targets.txt with computer names).
      But when i try use inside loop i get error: Variable must be of type "Object" and I dont know how to fix this
      Plz help
       
      Local $targets = @ScriptDir& "\targets.txt" Local $log_name = "\scan.log" $msgBox = MsgBox(4, "Scan", "need tergets.txt") Example() Func Example() If $msgBox = 7 Then exit EndIf If Not FileExists($targets) Then MsgBox($MB_SYSTEMMODAL, "", "File: targets.txt - no exist !") Exit EndIf FileOpen($targets, 0) Global $arr[1000] ReDim $arr[_FileCountLines($targets)+1] For $i = 1 to _FileCountLines($targets) $line = FileReadLine($targets, $i) $arr[$i] = $line ; chceck if pc is online Local $iPing = Ping($arr[$i], 250) If $iPing Then ; ONLINE Local $strComputer = $arr[$i] $colItems = "" $Output="" $Output = $Output & "Computer: " & $strComputer & @CRLF $Output = $Output & "==========================================" & @CRLF $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystemProduct", "WQL", _ "0x10" + "0x20") If IsObj($colItems) then For $objItem In $colItems $Output = $Output & "Vendor: " & $objItem.Vendor & @CRLF $Output = $Output & "SN: " & $objItem.IdentifyingNumber & @CRLF $Output = $Output & "Name: " & $objItem.Name & @CRLF $Output = $Output & "UUID: " & $objItem.UUID & @CRLF if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop $Output="" Next Else Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_ComputerSystemProduct" ) Endif Else ;OFFLINE _FileWriteLog(@ScriptDir & $log_name, $arr[$i]&" OFFLINE") EndIf Next EndFunc  
       
    • Leo1906
      By Leo1906
      I have a problem here. I have a Script wich opens a Gui with an iExplore Object embedded. The GuiCreate is in an external Function an called in the main while of the script.
      When the Gui closes the Gui gets destoyed. But unfortunately the iExplore Obejct still remains. You can see this by the RAM usage of the exe. After opening a Website the RAM usage stays at  ~ 50 Mb RAM (depending on the website).
      I want it to fall back to the normal state (around 4 MB).
      You can see my attempts to delete the object properly. According to the help an embedded object should get deleted when the GUI gets deleted ...?
       
      GUICreate("Display", 600, 360, -1, -1) Local $oIE = _IECreateEmbedded() $obj = GUICtrlCreateObj($oIE, 0, 0, 600, 360) GUICtrlSetColor(-1, 0xff0000) _IEPropertySet($oIE, "addressbar", False) _IEPropertySet($oIE, "menubar", False) _IEPropertySet($oIE, "statusbar", False) _IEPropertySet($oIE, "toolbar", False) _IEPropertySet($oIE, "resizable", False) _IENavigate($oIE, $dest&"\index.html") GUISetState(@SW_SHOW) While 1 Local $iMsg = GUIGetMsg() Local $tmsg = TrayGetMsg() Select Case $iMsg = $GUI_EVENT_CLOSE ;~ $obj = 0 ;~ _WinAPI_DeleteObject ( $obj ) ;~ _IEQuit($obj) ;~ _MemGlobalFree($obj) ;~ $oIE = 0 ;~ _WinAPI_DeleteObject ( $oIE ) ;~ _IEQuit($oIE) ;~ _MemGlobalFree($oIE) GUIDelete() Case $tmsg = $exit _exit() EndSelect WEnd  
      I would be very happy for your help  
      Kind regards
    • Hasoth
      By Hasoth
      Hi,
      I've encountered a problem while I tried to close popup in application that is using embeded GUI object. If site has function "onbeforeunload" asking if you really want to exit you will get a popup. In embeded gui object it completly freezes functions and I can't click/close/do anything. I've created sample code with microsoft function example and in external window. Any idea how to close popup in GUI embeded IE? I've tried navigating using _IENavigate($oIE, $sURL, 0) or  changing focus _IEaction($oIE,"focus") but nothing works.
      #include <GUIConstantsEx.au3> #include <MsgBoxConstants.au3> #include <IE.au3> HotKeySet("{F1}", "myExit") ;GUI setup $GUI_main = GUICreate("Menu", 800, 800, -1, -1) Global $oIE =_IECreateEmbedded() $ObjectIE = GUICtrlCreateObj($oIE, 0, 30, 800, 770) $cButton1 = GUICtrlCreateButton("Start test inside GUI window", 0, 0, 400, 30) $cButton2 = GUICtrlCreateButton("Start test outside GUI window", 400, 0, 400, 30) GUISetState(@SW_SHOW, $GUI_main) ;GUI While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $GUI_main Case $ObjectIE Case $cButton1 fTestFunction1() Case $cButton2 fTestFunction2() EndSwitch WEnd ;function inside GUI Func fTestFunction1() Local $sURL = "http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onbeforeunload.htm" $oIE.Navigate($sURL) _IELoadWait($oIE) $oIE.Navigate("https://www.google.com") MsgBox($MB_TOPMOST, "", "should appear while question box opened", 2) Sleep(2500) Send("!o") EndFunc ;function outside GUI Func fTestFunction2() Local $sURL = "http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/onbeforeunload.htm" $oSecondIE = _IECreate ($sURL) _IELoadWait($oSecondIE) $oSecondIE.Navigate("https://www.google.com") MsgBox($MB_TOPMOST, "", "should appear while question box opened", 2) Sleep(2500) Send("!o") EndFunc ;shortcut to exit Func myExit() GUIDelete($GUI_main) Exit EndFunc