Sign in to follow this  
Followers 0
bucky002

Making Udfs

11 posts in this topic

#1 ·  Posted (edited)

How would I make a UDF to open notepad? I tried taking a look at Process.au3 and I got confused with how they got them to work. Like

Func _RunDOS($sCommand)
    Return RunWait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
EndFunc ;==>_RunDOS
Don't you have to declare $sCommand? I'm confused. :)

And if I made a UDF to open notepad what would the script look like that calls the UDF notepad?

Thanks. :(

Edited by bucky002

Share this post


Link to post
Share on other sites



with that when a user types

_RunDOS(COMMAND)

then what ever they typed in is going to be inserted into

$sCommand in the return if im not mistaken, i havent really written any UDFs ive though about it but havent had time

Share this post


Link to post
Share on other sites

Yeah basically if you used:

_RunDOS("Ping 127.0.0.1 > C:\ping.log")

It should write the ping results into a txt file called ping.log on your C: drive.

Share this post


Link to post
Share on other sites

_RunDOS('Notepad.exe');<< 'Notepad.exe' is now actually $sCommand

Func _RunDOS($sCommand); $sCommand is what they call a parameter
    Return RunWait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
EndFunc;==>_RunDOS


[center]Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.[/center]

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

with that when a user types

_RunDOS(COMMAND)

then what ever they typed in is going to be inserted into

$sCommand in the return if im not mistaken, i havent really written any UDFs ive though about it but havent had time

yeah, I think when you type something for that parameter, it is automaticly decared as a local, i think

and to set optional parameters you could do:

func _RunDos($sCommand="ipconfig")

Edited by zerocool60544

[center]AutoIT + Finger Print Reader/Scanner = COOL STUFF -> Check Out Topic![/center][center][font=Arial Black]Check out ConsultingJoe.com[/font][/center][center]My Scripts~~~~~~~~~~~~~~Web Protocol Managing - Simple WiFi Scanner - AutoTunes - Remote PC Control V2 - Audio SpectrascopePie Chart UDF - At&t's TTS - Custom Progress Bar - Windows Media Player Embed[/center]

Share this post


Link to post
Share on other sites

Thanks all.

So is this how it would look? UDF:

#include-once

_NotePad('Notepad.exe')

Func _NotePad($sCommand)
    Return RunWait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
EndFunc

And this is what the script would look like?

#include <Notepad.au3>
 
Func _NotePad()
    Sleep(500)
EndFunc

Eh, I'm probably missing what I'm supposed to do.

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

on the last ur still going to have to define it

#include <Notepad.au3>

Func _NotePad('Notepad.exe')
    Sleep(500)
EndFunc

your UDf would be

#include-once

Func _NotePad($sCommand)
    Return RunWait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
EndFunc

not

#include-once

_NotePad('Notepad.exe')

Func _NotePad($sCommand)
    Return RunWait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
EndFunc

cuz thats going to define what to run already when in fact you want them to define their own thing to run hence UDF(USER DEFINED FUNCTION)

you can set notepad.exe as the default

Edited by thatsgreat2345

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

How would I make a UDF to open notepad? I tried taking a look at Process.au3 and I got confused with how they got them to work. Like

Func _RunDOS($sCommand)
    Return RunWait(@ComSpec & " /C " & $sCommand, "", @SW_HIDE)
EndFunc;==>_RunDOS
Don't you have to declare $sCommand? I'm confused. :)

And if I made a UDF to open notepad what would the script look like that calls the UDF notepad?

Thanks. :(

Read the stuff in the helpfile about Local and Global. Inside your UDF, you want to declare all you variables with Local because you don't want to step on any variables that might be used in the script that is calling it. When the calling script uses your UDF, it will put parameters (if any) inside the parenthesis. For example:

$MyComd = "notepad.exe"
_RunDOS($MyComd)

The string "notepad.exe" gets passed to the _RunDOS() function (inside the process.au3 file that you included). The _RunDOS() function uses that string internally as the Local variable $sCommand. In other words, when _RunDOS() is executed, $sCommand = "notepad.exe". In this case it doesn't have to use the actual 'Local' command to declare the variable, it is declared in the Func() command by "Func($sCommand)". But any more variables that the UDF needs internally (not declared on the Func() line) should use Local.

If you want the UDF function to use the same variable as the calling script, you use the "byref" in your Func() declaration.

Clear as mud? :D

Edit: Added "byref" reference.

Edited by PsaltyDS

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

Thanks, I'm starting to understand.

I got to go to bed now, so I'll check back later.

Thanks again. :)

Edited by bucky002

Share this post


Link to post
Share on other sites

#10 ·  Posted

Ok, let's flip to chapter 10 of my C++ Reference book, translate it into AutoIt, and type it out here:

Added notes in parenthesis.

Functions are used to encapsulate(contain) a set of operations and return information to the caller.

Once a function is written, we need only be concerned with what the function does.

That is, what data it requires and what outputs it produces.

The details, "how" the function works, need not be known.

The use of functions provides several benefits.

First, it makes programs significantly easier to understand and maintain. The main program can consist of a series of function calls rather than countless lines of code.

A second benefit is that well written functions may be reused in multiple programs. The AutoIt standard UDF library is an example of the reuse of functions.

A third benefit of using functions is that different programmers working on one large project can divide the workload by writing different functions.

A function is declared with a function body, which is a block of code enclosed with Func...EndFunc. The header of the declaration is the way of telling AutoIt the number of parameters, so it can perform error checking. Here are some examples of functions.

Sample Functions:

Func _MyFunction($title, $param)
   Run("notepad.exe")
   Send($param)
   MsgBox(0, $title, "Hello!")
EndFunc

;;I know this function can be written shorter, but KIS (Keep It Simple), OK?
Func _MyAdditionFunc($a, $b)
  Local $sum
  $sum = $a + $b
  Return $sum
EndFunc

Func _HelloWorld();;Ahh... the standard example
  MsgBox(0, "AutoIt", "Hello World!")
EndFunc

For function _MyFunction:

Name Of Function: _MyFunction

Number of Parameters: 2

Local names of parameters: $title, $param

Returns: Nothing

For function _MyAddititionFunc:

Name Of Function: _MyAdditionFunc

Number of Parameters: 2

Local names of parameters: $a, $b

Returns: 1 value

For function _HelloWorld:

Name Of Function: _HelloWorld

Number of Parameters: 0

Local names of parameters: -

Returns: Nothing

PART 2: CALLING FUNCTIONS:

(Wrote this part myself :) )

Sample Program:

_MyFunction("AutoIt Rawz!", "This is a piece of sample text")
$sum1 = _MyAdditionFunc(1, 3)
$sum2 = _MyAdditionFunc(4, 20)
$totalsum = _MyAdditionFunc($sum1, $sum2)
MsgBox(0, "Yo! The Sum Is:", $totalsum)
_HelloWorld()
Exit

Func _MyFunction($title, $param)
   Run("notepad.exe")
   Send($param)
   MsgBox(0, $title, "Hello!")
EndFunc

;;I know this function can be written shorter, but KIS (Keep It Simple), OK?
Func _MyAdditionFunc($a, $b)
  Local $sum
  $sum = $a + $b
  Return $sum
EndFunc

Func _HelloWorld();;Ahh... the standard example
  MsgBox(0, "AutoIt", "Hello World!")
EndFunc

The first line calls the function _MyFunction with parameters "AutoIt Rawz!" and "This is a piece of sample text".

This causes AutoIt to jump to that function, and set the values of the function's parameters.

Now, AutoIt is at line 10, Func _MyFunction($title, $param).

It sets $title = "AutoIt Rawz!" an $param = "This is a piece of sample text" then moves on to the next line.

AutoIt executes each line in order, until it reaches a EndFunc or Return, and then returns to the caller.

After execturing the function, AutoIt moves on to line 2, $sum1 = _MyAdditionFunc(1, 3)

AutoIt jumps to function _MyAdditionFunc and sets $a=1 and $b=3.

Local $sum ;;Creates Local Varaiable

$sum = $a + $b ;;Preform stuff....

Return $sum ;;Return Value

This function has a Return statement, that causes AutoIt to return to the caller, and carries the return value, $sum with it.

The caller then sets $sum1 to be equal to the return value. { $sum1 = _MyAdditionFunc(1, 3) }

Same thing happens on lines 3 and 4.

Line 5 calls the function with no parameters and no return value.

I hope you understand functions by now...

#)

Share this post


Link to post
Share on other sites

#11 ·  Posted

Yes, I think I understand how to make UDF's now. :)

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
Sign in to follow this  
Followers 0