project: making a matrix batch file


those who have seen the movies know the saying "the matrix has you…" this is probably my favorite project, and it will show you how to combine a lot of what we've covered to do something completely pointless, but cool.

if you open the dos prompt (win+r, type "cmd") and then press alt+enter, it switches to full-screen mode, and pressing it again will switch it back. the problem is that there is no built-in command to make the window full-screen like this for you. another problem we will explore in simulating the matrix screen is that it is rather difficult to make it appear as though the messages are actually being typed in "real-time" as they are in the movie. other than these little annoyances, creating the code itself is not difficult at all.

to start, open notepad (win+r, type "notepad"). considering we only want our specified output to display, and not the commands themselves, we will need to turn the echoes off.

 @echo off 

we also use the "@" symbol to suppress this command from being displayed as well. next, we want the colors of our screen to have a black background, with green text, so we will use the "color" command.

 color 0a 

now we run into our first problem. rather than just outputting our messages directly, how do we make it appear as though our messages are being typed out? the most reasonable solution that i found for dos alone is to use a "for" loop in combination with the "cls" (or clear screen) command to repeatedly print the same text so many times (to draw out the time of execution) before printing more text. now, computer processing time is a lot faster than human processing, so telling a computer to loop 100 times would almost seem instantaneous. this is how you loop a hundred lines of text in dos:

 for /l %%f in (1, 1, 100) do echo %%f 

remember that we're using the double-percent signs instead of single percent signs because we're working with a batch file and not the command line directly. the code above would echo the numbers 1 through 100, and rather quickly i might add.

to simulate a line of text being typed with this method, you could use the following:

 for /l %%f in (1, 1, 350) do echo "wake" && cls for /l %%f in (1, 1, 350) do echo. && echo "wake up" && cls for /l %%f in (1, 1, 350) do echo "wake up neo.." && cls 

in english, the first line more or less translates as "for as long as f has the value of 1, and f is less than 350, echo the word 'wake' and clear the screen, and then raise the value of f by 1 and repeat." so this line prints "wake" 350 times, clearing the screen each time (so we don't have the word displaying on multiple lines), and then moves on to the second line of code where we do the same thing only adding another word to our sentence. once it's done with the typing effect, you would want the text to stick (instead of immediately jumping to the next sentence), so you would type:

 echo. echo "wake up neo.." echo. pause 

when you type:

 echo. 

it means print a blank line. in our typing effect code above, we have it print a blank-line within the second duration of the cycle but not for the other two. this will add somewhat of a jumping effect, making the text appear as though it is bouncing up and down or scrolling a little bit (like the actual matrix screen). finally we tell the script to pause, which asks the user to press any to continue; we can then pick up where we left off, repeating the same patterns to add additional lines of text. depending on your monitor's refresh rate, you may not be able to see the flickering of text very well. you can adjust the number "350" to change how many loops your script takes if you want to.

anyway that just isn't good enough, the text kind of looks like it is being typed but then again not really. we're going to beef it up with some visual basic, although you can play around with the above code just for kicks. the "shell" object in wsh gives us access to the method "sendkeys," which as the name implies, sends keys to the active window. our first script will be titled "matrix.vbs" and saved in our temp folder. first the code, then an explanation:

 option explicit dim wshshell, title, mystring, spacecount, length, position, tmp set wshshell = wscript.createobject("wscript.shell") title = "the matrix" sub waitfor(var)   tmp = false   do until tmp     wscript.sleep 1000     tmp = wshshell.appactivate(var)   loop end sub sub key(msg)   spacecount = 0   length = len(msg)   for position = 1 to length     wshshell.sendkeys mid(msg, position, 1)     spacecount = spacecount + 1     wscript.sleep 250   next   wscript.sleep 3000   for position = 1 to length     wshshell.sendkeys "{ backspace} "     spacecount = spacecount - 1   next end sub waitfor(title) mystring = "wake up, neo..." key(mystring) mystring = "the matrix has you.." key(mystring) mystring = "follow the white rabbit.." key(mystring) mystring = "knock knock.." key(mystring) wshshell.sendkeys "{ enter} " wscript.quit() 

the first line "option explicit" tells the script to be in error mode so to speak, making sure we define all of our variables before using them etc… you can see two user-defined functions that i created; functions in vb are defined with the word "sub" rather than "function." you can also see a couple "for" loops, and a few built-in/system functions such as "len," which returns the length of a string. what this script more or less does is defines the title "the matrix" and then waits until a window with that title appears on the screen (attempting to make it the active window); when the window does arrive it types out our sentences. it might seem confusing, but take some time to study the code. we define our variables at the top, and then we create functions that we will call with our input at the bottom. it's logical to create the processing code before we call it, but not necessarily necessary. in other words, visual basic amongst other languages (but not all of them) will allow you to call a function before you define it.

for future reference, you could also throw the "run" method in there (wscript.run) to launch a specific program you wanted to send keys to. you know you want to.

the script above can be quirky; for example if you minimize the window it is writing to before it's done it will still continue sending keys, which could cause applications to jump around a bit. if you run the code right now, it will simply wait in the background until we bring up the window with our specified title, so let's work on that. to finish off our batch file that we started, it will end up looking like this:

 @echo off color 0a title the matrix set /p matrix= | cscript /nologo c:\temp\matrix.vbs exit 

we set the title to match the one specified in our vb script, and then the next line is a little tricksy. in order to stop our batch file in the middle of execution to allow the vb script to run (and type), we need a way to pause for user input. the code snippet "set /p matrix=" does exactly that; here we are creating an environment variable, the value of which will be defined by user input. considering batch files only execute one line at a time, you can't exactly tell the script to pause for user input and then tell it to start typing, because it has to wait for the user input before it will see the command to start typing. to solve that problem, we use the pipe "|" operator, which assigns the output of one command to the input of the first command :-) so, the output of our typing script is sent to the command-line input. i'm a freakin' genius.

once you've got it all typed out, save this file in your "temp" folder as "matrix.bat." navigate to your temp folder, right-click on the file you just saved, highlight "send to" in the menu, and then click "desktop (create shortcut)." now go to your desktop.

since we can't make the script run at full screen programmatically, we will need to adjust the properties of our shortcut icon to have it do this for us. right-click the icon, select "properties," and within the "options" tab find and click the button labeled "full screen" followed by "apply" for the changes to take effect. once you're all done, just double-click the shortcut icon to see our masterpiece in action :-)

if you'd like to, go ahead and change the shortcut's name and icon picture to disguise it as something else, or drop the shortcut in your startup folder hehe.




Tapeworm - 1337 Hax or Handbook
Tapeworm - 1337 Hax or Handbook
ISBN: N/A
EAN: N/A
Year: 2005
Pages: 74

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net