How to start (part 2)

From Scriptwiki
Jump to: navigation, search

If you haven't read the part 1 of How to start, i suggest you read it first.
So, you have read it, and know where to find remote editor, variables tab, etc. it's time to learn how to make scripts.

Aliases

Aliases are commands, used to perform various tasks, ie. /join and /part are build-in aliases, you can make custom aliases yourself in the remote editor (shortcut alt+r).
You can create aliases in two places, aliases-tab (example line in the editor: /hello /say hello) and in remote-tab (example line in the editor: alias hello { say hello } ).
Even though you can make commands in aliases-tab, it is recommended to use remote-tab, since you cannot make identifiers or anything else than commands in aliases-tab, so all the examples here are made in the remote-tab.

It is time to make the first script.

alias hello { echo -ag Hello world! I am a mircscript! }

Now we have a new command, which can be executed by writing /hello in the editbox (editbox is the box where you normally write your text to be sent to a channel/nickname) What this command does, it echoes "Hello world! I am a mircscript!" in your active window, echo means that no-one else but you can see it.

Now it's time to use some identifiers in our script.

alias test { echo -a $calc(1+2) }

Fairly easy to understand what this script does, by writing /test it echoes the sum of 1 and 2 (1+2) in the active window.

To use parameters in your scripts, you can use $1, $2, $1- identifiers to get the parameters, which are space separated tokens (words).

alias test { echo -a Your first parameter was: $1 Second was: $2 And the whole line was: $1- }

So if we wrote /test hello world, the script would echo "Your first parameter was: hello Second was: world And the whole line was: hello world".

At this point would be a good time to mention that you NEED to have spaces around identifiers, ie. "hello:$1" wouldn't work, since : and $ are in conjunction, also "hello: $1," wouldn't work, since mirc would think that you wanted to use identifier called $1, and not $1 (note that this doesn't apply to normal brackets when used inside another identifier, ie. $calc($1-)).

Now, let's make a calculator command with what we've learned.

alias calculator { echo -a $1- = $calc($1-) }

Writing /calculator 1+2+3 would echo "1+2+3 = 6".

Identifiers

Identifiers are created the same way as commands, the difference is that they are used to return data, like $time is mirc's own build-in identifier and it returns the current time.
An easy way to test identifiers is to write //command into the editbox, the double backslashes force mirc to evaluate the line (ie. //echo -a Current time is: $time).

Simple custom identifier.

alias testing { return Just testing. }

//echo -a $testing would return "Just testing." to the echo command, and the text would be echoes to the active window.

Identifiers accept parameters inside brackets, $identifier(param1,param2,param3).

alias testing { return $calc($1 + $2) }

//echo -a $testing(4,5) would give 4 and 5 as parameters to the identifier, which returns the sum of these parameters, so this would result in echoing number 9.

Events

Events are scripts that trigger when something happens, ie. when someone joins a channel, or sends a message to a channel.
Events starts with "on" as the first thing on the line, and is followed by (usually) level, event name, trigger text, target, commands.

A simple text event would look like this:

on *:text:!hello:#my_own_channel:{ msg $chan Well hello there! }

Where the first asterisk is the userlevel, * means "any level", "text" is the event name (this event is called "on text" event), "!hello" is the trigger text (triggers when someone writes !hello), #my_own_channel is the target, and the commands are enclosed inside the brackets.

note: Be sure that your syntax is correct, and you use the brackets correctly, or you will end up with broken scripts.

Examples

Now we have a basic knowledge on how to make commands, identifiers and events, let's try doing a bit more complicated scripts.

; we'll use this identifier to enclose the first parameter into brackets, so that "hello" becomes "(hello)"
alias add_brackets { return ( $+ $1 $+ ) }

; an alias to echo time and given parameters to active window
alias echo_time { echo -a This command was executed at $time with parameters: $1- }

; on text event to trigger on incoming text on a channel
on *:text:!mynick *:#:{
  msg $chan $add_brackets($nick) wanted to know his own nickname, silly $nick $+ .
  echo_time $nick asked his nick
}

When the on text event is triggered, the script will msg the channel "(someguy) wanted to know his own nickname, silly someguy.", and echo to your active window "This command was executed at 12:36:24 with parameters: someguy asked his nick".
Note that $+ is used to attach things together, since you cannot use $nick. you'll need to use $nick $+ .

See also