ARexx scripting: How to
1 Attachment(s)
I think section "Coders/Tutorials" is for ASM,C and C++. So i placed it here. Move it if i'm wrong.
Absolute beginners start here Because of some recent uploads to Aminet, i'd like to show users who are interested in learning how to create working ARexx scripts a shorter, faster and much more convinient way to interact with a tool like DirOpus4 While DO4-Version12.rexx from Aminet fills RamDisk with garbage data and is not aware of folders (it copies selected directories to ram drive for no reason), our small script just ignores entries that do not give any valid version information and deletes the output when done. First a simple script that reads version information from selected files and present them as a list. Code:
/* |
Using requesters
As this is much too simple and still uses intermediate files (which we have to
remove from RamDisk to save memory), here is a similar script to show each version string in a requester. This time we send our data to PIPE: instead to a file. This makes the script smaller and leaves no remains in RAM: Code:
/* |
Enhanced version info
This script can be enhanced, of course. Like DOS command version, we could pass
an argument like 'full' to get additional information. Try yourself before peeking the next script. Hints: Arguments are taken with Arg name [name]... name now contains the argument given We need to check if argument is given, find a good place for this Instead of concatinated strings, a line break eases reading. Find a way. Code:
/* |
Comparison of directories
The next is a small script to compare two folders. It does not require any
selection, it will highlight files in both panes which have the same name. The script is not optimized for speed but yet is 2 to 8 times faster then DO4-Intersect13.rexx. The script can be altered to highlight mismatches instead. Code:
/* ugly. If you want to test your scripting skills than alter it to work in "silent" mode Some suggestions on how to achieve this: -use intermediate files to store your results -use Setclip()GetClip() to store them in memory create a simple array using stems. A stem is identified by its name and a trailing number e.g.: /* presume numoffiles has return value '3' and search path is 'libs:' then :*/ this gives: found.1 = icon.library found.2 = rexxsys.library found.3 = workbench.library There are more possible solutions, like allocation of memory and write/read directly to mem (using rexxsupport.library). Piping might be used as well. |
ARexx Bug Killing
When you start with ARexx the first time you'll soon encounter the problem of
finding errors. (erm, no , the errors will find YOU :D) The script stops on any error and you won't see anything useful Debug Mode To enable debugging, put Trace('R') right after the first comment in your ARexx script to see result values or use any other option that suits you (e.g. Trace('A') for All) Before starting your script call 'TCO' from command line or command input (right Amiga + e). 'TCO' (short for TraCeOutput) resides in 'sys:rexxc' To close console call 'TCC' (abbr. for TraCeClose). There are 2 more commands and they come very handy when debugging is not need all the time. TS - starts tracing TE - ends tracing While hunting bugs, it might be a good idea to use 'say' command for testing. If you doubt a function to work as expected, put say infront. An output console (or shell) is required. For DOpus you could abuse DOpus Command 'Verify' to pop up a requester. Verify <symbol|string> where <symbol> is your variable and <string> is a text in single quotes like: Verify 'Var item2= ' item2 Have fun with ARexx |
Introduction and TOC
Never done anything in ARexx. Absolute newbie ?
Here is a short introduction: ARexx is a scripting language to send commands to a so called 'host'. A host is an application with a message port, that listens to ARexx. So what we do is simply sending a message containing our code. The host has to interpet it and if possible process it. Whether the host answers back with valid return codes or not is up to host. ARexx has 2 special vars to deal with return values. See later Chapter ARexx requires the RexxMaster to be up and running. RexxMast can be found in Sys:System. To create an ARexx script any editor will do. Ed from C: or emacs or your preferred editor. If you don't have an editor at hand, you can still use AmigaDOS commands to create a script. An ARexx script starts with a comment. A comment is one or more lines encapsuled in /* */. ARexx has no variable typing, that means a var has not to be declared as Int, Float, String. You might use any single char as a variable. welcome = 'Hello Folks' /* now welcome contains a string*/ welcome = 7 /* is now overwritten with a number */ welcome = RESULT /* and now contains what the host replied */ A very basic script looks like: Code:
/* Time for a TOC ******************************************** First steps Run from shell Using quotation marks Addressing hosts Symbols and operators introducing functions Analyze strings List of commands List of functions Console input Counting words String searching and replacing Interrupt Signals Checking return codes Tracing Decrypting trace output Special character Function usage Creating functions and commands Parsing arguments Code obfuscation Parsing with variable length Parsing strings from DirOpus4 Loops Adding libraries Create and open a rexx port |
Now if you got too much free time, then do it in AmigaDos:
Open a shell and use 'echo' For example echo >>ram:test.rexx /* test */ echo >>ram:test.rexx /* $VER: test 1.0 */ echo >>ram:test.rexx "welcome = 'Hello, world!'" echo >>ram:test.rexx Say welcome echo >>ram:test.rexx Exit to test your script cd ram: 2.RAM:> rx test the file ending *.rexx is not needed. But using it helps to identify rexx scripts among others. If you are running WinUAE with shared clipboard, then mark the commands and copy (ctrl+c) them to clipboard. Switch to WinUAE , start a shell and paste it by pressing rightAmiga+v, press 'Enter' The prompt will quickly repeat itself. Don't worry. :>cd ram: 2.RAM:>type test.rexx or run it 2.RAM:>rx test You can check version with 2.RAM:> version test.rexx An editor is really, really much more convinient :D |
Thank you for taking the time to publish the useful tutorials.
|
Thanks for the flowers ;)
Now after we did what the author said without asking why, it's time to have a look at syntax! Though DOS batchfiles and Rexx scripts look similar there are fundamental differences. Check out quotation marks. Where DOS expects double quotes ", Rexx uses single quotes '. THIS IS VERY IMPORTANT ! DOS will not deal with path names as strings if they contain white spaces, until wrapped in "". ARexx on the other hand uses ' single quotes. If you want to send a " from within a rexx script, wrap it in single quotes which is very hard to read and prone to errors. e.g. path0 = 'Ram Disk:' Address command 'cd 'path0 Though arexx correctly concatenates strings ('cd ' is first string and content of path0 is 2nd) it will throw an error. The path contains white space. The error code is sent by DOS command cd. Cd can't find "Ram". alter it to path0 = 'Ram Disk:' Address command 'cd "'path0'"' to make it work. The command string is now build from 'cd " ' + content of path0 + ' " '= cd " + Ram Disk: + " = cd "Ram Disk:" This is hell. The more you glue together, the more gibberish it looks like. from example above Code:
Address command 'version >' tempfile ' "'actdir||item'" file 'fullversion line in source that is causing trouble, but damn, which one of these flyspecks was set wrong ? Another big difference is the way we set comments. In DOS you just place a semicolon ; in front and everything behind is ignored till end of line like: version >NIL: icon.library version 46 ; check for peters lib In ARexx the semicolon is used to separate commands from each other in one line. f.e.: Address dopus.1;verify 'are you sure?';if rc=1 then exit; A comment in ARexx starts with this pair of characters /* and ends with its counterpart */ All text in between will be ignored, no matter how many lines of text are encapsuled. /* Our first script */ or for a single line Say Show('p',rexx) /* check for rexx port*/ Nested comments are allowed like /* outer /*inner comment*/ comment*/ Beware that this is difficult to read and if you forget to set one of these "slashterixes" ARexx may start to interpret the next word as a command. . |
Quote:
For example here you can find short but very cool AmigaDOS script to view version of the file in a standard system requester. It even compares if there is different version loaded in ram compared to file on the filesystem. |
Right now you have learned a new ARexx command.
Address Adress is used to switch between hosts, to send our messages to a new recipient. The first port that is always open when running rexx code is "REXX" where Address Workbench /* now we talk to workbench, prior host (REXX)becomes old host */ Address rexx /* makes REXX to our new port to talk to, and Workbench will be old host */ Address dOpUs.1 /* ha, you don't have to type it upper case :D , ARexx will change everything to upper case by default */ Address command 'echo "Hello eablings."' /* We send our command to DOS, but we do not change the host.*//* DOPUS is now new host, REXX is again old host */ Address dopus.1 'verify welcome' /* Again we send a command (verify is a DOpus command) Address command can be replaced with Shell commandto a host (dopus here) without changing our current host.*/ Shell command 'cd ram:' is similar to Address command 'cd ram:' Address /* without a host name will switch between current and previous*/ How do i know who is the caller ? Address() this is no command , but builtin function. have a look at the scripts above on where and how it is used |
Quote:
This is why i started this thread. But this is not a tutorial for DOS scripting ;) Placing a button in DirOpus and run version from there is the most easy way i can think of. Enough for today. ... to be continued But you won't get able to compare folders this way and you can't process many files that easy :D Have a closer look at Severin's script. it has hardcoded path names and messages. Comparing file version in different locations always tell you about RAM: :D |
Here's my version of the "Version number in DOpus requester" script. It was published as part of the DOpus4Plus package, so it's a little more complex because like all scripts in the package it has German and English strings included.
I'm posting it, because it illustrates how to change button texts for the DOpus requester (it's changed to "Next" when viewing several files and changed back before the script exits). Code:
/* Code:
/* generate a unique name ("req" + my process' id) */ |
Very nice. Always good to have additional examples. You excluded *.info files.
In WB3.1 and DOpus4.16 the left requester button has only one char (N underlined), but no text, probably the Ümläüts. :D |
Hi,
Nice tutorial. In my opinion the debugging is an issue while scripting with ARexx. Perhaps some more integrated environment needed there. Using PIPE: device is awesome! Both ARexx and PIPE: as well are perfect examples of Amiga "soul" there IMHO... Thanks, |
Would you be able to compile you tutorials into a PDF or AmigaGuide document and upload to Aminet ?
|
Quote:
I consider authoring pdf/aguide when this proved to be usefull. Help is appreciated for bug hunting/fixing, spell checking, style etc. Problem is, my explantions sound simple to me, but any other reader might just go :crazy |
Quote:
This will be discussed in more detail later on. |
Quote:
Rewriting the code block again and again with lots of - if - then - select -when - constructs isn't fun at all. Code:
pseudo code: |
Quote:
Code:
when la="<language>" then do Quote:
Code:
pseudo code: Quote:
|
All times are GMT +2. The time now is 19:24. |
Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.