The Wayback Machine - https://web.archive.org/web/20171202235828/http://www.mactech.com:80/articles/mactech/Vol.15/15.09/ScriptingLanguages/index.html
TweetFollow Us on Twitter

Volume Number: 15 (1999)
Issue Number: 9
Column Tag: Systems

Scripting Languages

by Rich Morin and Vicki Brown

A Cross-OS Perspective

Background

Ask most computer users (or programmers) about scripting languages and you'll get a wide variety of answers. Most users, however, will give you examples of languages they "know" are scripting languages - AppleScript, the Unix shells, Visual Basic, JavaScript, Perl, Tcl/Tk...

Ask about system programming languages and you'll be given a different set of examples. This time C, C++, Java, and/or Pascal will be mentioned. Everyone knows which languages fit which categories, but not everyone can explain why. And what about languages such as Perl, which seem to have elements of both?

Although "scripting languages" are increasingly popular, their exact definition can be elusive. Are they simply variations on "command languages"? Can they be used for "real" programming? Because of the way languages evolve, no definition we could make can be definitive, but some background notes may help to clarify the situation.

Scripting languages have their roots in the "Job Control" languages (e.g., IBM's OS JCL) that are used on "batch processing" computer systems. JCL commands tell the system to run specified programs, using particular I/O resources (e.g., files and tape drives). The "command languages" found on "time-sharing" systems (e.g., Unix and VMS) add interactive window-dressing, but perform precisely the same functions.

Unix "shells" (e.g., bash, csh, ksh, and sh), along with Apple's MPW, are strongly influenced by these command languages. As a result, users of these shells still "program" their way through the day. By issuing commands to the system, they can move files, compile programs, and more. For example, a Unix user might issue these commands:

% mv foo.c bar.c						# rename "foo.c" to "bar.c"
% cc -o bar bar.c						# compile "bar.c" into "bar"
% bar											# run "bar"

Scripts

The line between entering commands and scripting is very fine. On most modern computer systems, it is trivial to record a "script" of user commands, edit it as desired, and reuse it later to perform similar operations. A typical Unix "shell script" of this sort might look like:

# wl - run word count, then print (lpr) a set of files
#
# Usage: wl file1 [file2] ...
T=/tmp/wl.$$	# temporary file
wc  -l $* > $T	# get line counts for files 
lpr $T $*	# print line counts, then files
rm  $T	# remove temporary file

Shell scripts tend to be easy to write, understand, and modify, at least in comparison to C programs that perform the same tasks. Many of the "standard" Unix tools are implemented as shell scripts. (A quick look over our FreeBSD system found 47 scripts, a little less than 10% of the total system commands). MPW users won't find this unusual; indeed, scripts are even more prevalent on MPW. (Approximately a quarter of the commands in the basic MPW distribution are scripts.)

"Glue" Languages

Unlike most compiled languages, which make use of code libraries written in the same language, scripting languages are often used as "glue" languages. That is, they are used to glue together tools and programs which may not, themselves, be written in the scripting language.

In the example above, the wl script glues together three Unix tools: wc, lpr, and rm. Each of these may, itself, be another script; it doesn't matter. What does matter is that we've brought them together (along with a variable definition and some comments), creating a useful tool.

When Does a Script Become a Program?

OS JCL has relatively primitive support for programming. Variables can be defined and conditional execution is supported, along with a bit of modularity, but that's about it. Today's command languages, in contrast, support all sorts of programming constructs.

The Unix and MPW shells each allow conditional execution and generalized control flow (e.g., if...else, looping), some level of modularity, variable creation and evaluation, and other desirable features. As a result, rather than sticking to simple, line-by-line scripts, it is quite possible to write "real programs" in these languages. As we said before, many of the normal administrative tools found in Unix and MPW are, in fact, are scripts.

Because scripting languages are "interpreted" (rather than compiled into machine code), they can show some amazing bits of run-time flexibility. For instance, the variables ($* and $T) in the Unix shell script above could contain any sort of text strings, including data values, file names, or even shell commands!

Similarly, the "backquote" operator can be used to generate data "on the fly", as:

% mv 'grep -l Mac *' dir  # move "Mac" files into "dir"

In the Unix command above, grep searches all files in the current working directory, listing (the names of) files which contain the string "Mac". This list is then substituted back into the command line, yielding something like:

% mv abc bcd cde ... dir

If the user had to write the script this way, it would lose much of its value, as the names of the files would have to be "hard-coded" into the script. Instead, the filenames are calculated at runtime, and they will likely be different every time this script is run.

Although neither mv nor grep "knows" how to do this sort of selective file move, users are able to combine them in ways that take advantage of each command's strong points. This re-use of low-level tools is very characteristic of scripting and can be extremely powerful.

The following bit of code from an MPW Startup file combines use of backquotes with if...else statements and variable evaluation (in {braces}) to create a tidy little programmatic construct, as well as a reusable bit of code that can be shared between users.

if "'Exists "{PrefsFolder}"UserProjectMenu'"
	Execute "{PrefsFolder}"UserProjectMenu
else if "'Exists "{MPW}"UserProjectMenu'"
	Execute "{MPW}"UserProjectMenu
else
	ProjectMenu	# the default project menu
end

There is no need for the script to know how a given user has his menus set up, nor even to know (ahead of time) what the path to the PrefsFolder or the MPW folder will be. As long as the pertinent variables are set by the user, this script will run correctly. More to the point, it will be flexible enough to serve the needs of different users.

AppleScript

Although users of conventional (i.e., command line) scripting languages may find AppleScript to be somewhat unusual, it does fit the model of what a scripting language can be. Under Mac OS, commands are issued, not by typing, but by selecting - menus, buttons, icons - and clicking. AppleScript allows users to save these commands in a file, suitable for executing (by double-clicking) at a later time. These files can then be annotated and extended in a manner similar to editing a shell script under Unix or MPW. Thus, it is reasonable to think of AppleScript, along with MPW and the Unix shells, as a scripting language.

The Record feature (analogous to the Unix script command) allows a user to record an exact sequence of events into a script. After recording, the user can then edit the script with a specialized Script Editor, adding variables, conditionals, and looping. Indeed, while a simple recorded AppleScript may bear little resemblance to what we think of as a program (being nothing more than a repetitive list of captured tasks), with the addition of a few variables and a loop or too, AppleScripts begin to look quite... real.

Some tasks may not be recordable, unfortunately, so you'll need to read the documentation carefully (and study examples) to get the most mileage out of your scripts. Even then, menu selection, button presses, and the like still won't be scriptable with vanilla AppleScript. If you're writing an automated script, you may not want the script to stop part way through and wait for the user to click the OK button!

Fear not - PreFab Player <http://www.prefab.com> is a commercial application created to work with AppleScript and provide access to all the controls that Apple and other application vendors left out. Specifically, Player "adds verbs that query and manipulate the Macintosh user interface, giving your scripts access to otherwise non-scriptable applications, desk accessories and control panels." This lets you include statements such as

do menu menu item "Page Setup" of menu "File"

within your scripts, removing the need for user intervention (and making your AppleScripts even more "program-like").

Advanced Scripting Languages

As useful as they are, conventional scripting languages have syntactic and other limitations, however, which tend to limit their efficiency and expressive power. For instance, even "advanced" Unix shells (e.g., bash and ksh) have very limited support for concurrency, data structuring, information hiding, object-oriented programming, regular expressions, etc.

Responding to these deficiencies, language developers have created extended scripting languages such as Perl, Python, and Tcl/Tk, among others. These languages are still able to invoke and glue together other programs, manipulate files, and set values on-the-fly, but they are also appropriate for writing much larger applications.

Perl, for example, provides compact but very powerful sets of data types (numbers, strings, and references) and structures (hashes and lists). Hashes, also known as an associative arrays, use text strings as indices. Lists, in Perl, are numerically indexable and can also act as stacks, queues, or even dequeues (double-ended queues). Perl emphasizes support for common application-oriented tasks; important feature of Perl include built-in regular expressions, "text munging", file I/O, and report generation.

Python has a great deal in common with Perl, but it emphasizes different things. Python's focus is on support for common programming methodologies such as data structure design and object-oriented programming. While such things are generally matters of opinion, Python also claims to have a more "elegant" (i.e., "less cryptic") syntactic notation.

Tcl/Tk shines at creating graphical user interfaces. A few dozen lines of Tcl/Tk can produce a very substantial collection of buttons, sliders, fill-in areas, and other widgets, nicely arranged and capable of doing calculations and/or invoking other applications. Tcl alone makes an excellent language for writing low-level system and kernel test tools; Tcl does not require a user interface, nor do Tcl scripts require the complete Operating System, with all its utilities, to be running.

In addition, through the efforts of these language enthusiasts, these languages have very substantial collections of add-on code (e.g., modules and libraries) for accessing databases, creating or parsing HTML, writing network daemons, etc. By leveraging the available add-ons, programmers can accomplish a great deal in an astonishingly small amount of new code.

System Programming Languages

Where do we draw the line between a scripting language and a system programming language? Perl, for example, is widely considered to be a scripting language, but it shares many characteristics with so-called "system" languages such as C. As an example, Perl can take advantage of sockets, fork processes (under Unix), and handle various aspects of input/output and process control "normally" reserved for "system" languages.

Perl can still be used as a glue language, but the Perl language itself has become so capable that this is no longer a requirement. In simple scripting languages, such as the Unix shell, there are only a small number of built-in commands; it's difficult to write much code without invoking another, "external" program or two. In contrast, the Perl language is so extensive (and extensible, via modules and XS code), that it is quite possible to write a complete Perl script that relies on nothing but Perl. (This is part of what makes Perl so portable).

MacPerl, for example, is a nearly complete port of Perl to the Macintosh, leaving out only those features that are specifically unsupported by the Mac OS (e.g., fork, exec, and various process and user control commands). MacPerl even sports a more extensive socket library than Unix Perl (specifically, it supports AppleTalk).

When combined with ToolServer (part of the MPW suite), MacPerl can even act as a glue language, taking advantage of backquoted commands or the system() function to call Apple application programs. MacPerl scripts can also call AppleScript (and vice versa), providing even more programming power and extensibility.

At some point, with so many readily available, easy to use (and inexpensive or free!) high-level scripting languages to choose from, why would anyone still resort to C, C++, or Java? We can think of several reasons, but execution speed and the desire to keep algorithms private are probably the dominant ones. Both of these reasons are going away, however, with increases in processor speed and the advent of compilers for traditionally interpreted scripting languages.

If you want to get down to the lowest level and work closely with the actual bits, or if you need special memory allocation or character I/O, a system programming language may still be the best choice. And, of course, you may not want to write the next great OS in a scripting language (then again, you might!). In any event, scripting languages definitely have a useful role to fill in most programmer's toolchests; if you haven't yet done so, we suggest that you give them a try!

References

MPW - Macintosh Programmers Workbench, is available for free download from Apple Computer. See http://developer.apple.com/tools/mpw-tools/index.html.

An extensive set of pointers to scripting language comparisons can be found on the Python language web site at http://www.python.org/doc/Comparisons.html.

Idiom Consulting has compiled a Catalog of Free Compilers and Interpreters, "... freely available software for ... things whose user interface is a language." The list is available at http://www.idiom.com/free-compilers.

For a comparison of Perl, Python, and Tcl/Tk (as well as another perspective on what makes a scripting language), see "Choosing a Scripting Language" in the Oct, 1997 issue of SunWorld (available at http://www.sunworld.com/swol-10-1997/swol-10-scripting.html).

Further information on Perl, Python, and Tcl/Tk can be found, respectively, at http://www.perl.com, http://www.python.org, and http://www.scriptics.com. Ports of all three languages are available for Mac OS.


A 30-year veteran of the computer industry, Rich Morin (rdm@ptf.com) writes the Silicon Carny column for SunExpert magazine and is a Contributing Editor for MacTech. He programs almost entirely in Perl these days, on Mac and Unixish systems. Rich is also the president of Prime Time Freeware (www.ptf.com), which publishes mixed-media (book/CD-ROM) collections of freely redistributable software. PTF's Mac-specific products include "MacPerl: Power and Ease" and "MkLinux: Microkernel Linux for the Power Macintosh".

Vicki Brown (vlb@cfcl.com) has been programming professionally since 1984. Unix is her favorite Operating System; the Mac OS is her favorite User Interface. Vicki is co-author of "MacPerl: Power and Ease" and co-host of the MacPerl feature in PerlMonth magazine. When she isn't writing, Vicki is employed as a Scientific (Perl) Programmer at a BioTech company on the San Francisco Peninsula. Her interests include programming, the World Wide Web, reading, writing, and spending time with her spouse and their cats.

 

Software Updates via MacUpdate

BBEdit 12.0.2 - Powerful text and HTML e...
BBEdit is the leading professional HTML and text editor for the Mac. Specifically crafted in response to the needs of Web authors and software developers, this award-winning product provides a... Read more
HoudahSpot 4.3.3 - Advanced file-search...
HoudahSpot is a versatile desktop search tool. Use HoudahSpot to locate hard-to-find files and keep frequently used files within reach. HoudahSpot will immediately feel familiar. It works just the... Read more
Smultron 10.0.3 - Easy-to-use, powerful...
Smultron 10 is an elegant and powerful text editor that is easy to use. You can use Smultron 10 to create or edit any text document. Everything from a web page, a note or a script to any single piece... Read more
WhatRoute 2.0.25 - Geographically trace...
WhatRoute is designed to find the names of all the routers an IP packet passes through on its way from your Mac to a destination host. It also measures the round-trip time from your Mac to the router... Read more
Skim 1.4.31 - PDF reader and note-taker...
Skim is a PDF reader and note-taker for OS X. It is designed to help you read and annotate scientific papers in PDF, but is also great for viewing any PDF file. Skim includes many features and has a... Read more
Stylizer 7.17.1127.805 - Real-time CSS e...
Stylizer helps you style websites in a fraction of the time. Features Real-time CSS - Immediate feedback saves countless hours. Say goodbye to the reload button! In Stylizer, your changes are... Read more
LaunchBar 6.9.3 - Powerful file/URL/emai...
LaunchBar is an award-winning productivity utility that offers an amazingly intuitive and efficient way to search and access any kind of information stored on your computer or on the Web. It provides... Read more
Capture One 11.0.0.282 - RAW workflow so...
Capture One is a professional RAW converter offering you ultimate image quality with accurate colors and incredible detail from more than 400 high-end cameras -- straight out of the box. It offers... Read more
Wireshark 2.4.3 - Network protocol analy...
Wireshark is one of the world's foremost network protocol analyzers, and is the standard in many parts of the industry. It is the continuation of a project that started in 1998. Hundreds of... Read more
Vivaldi 1.13.1008.34 - An advanced brows...
Vivaldi is a browser for our friends. In 1994, two programmers started working on a web browser. Our idea was to make a really fast browser, capable of running on limited hardware, keeping in mind... Read more

Latest Forum Discussions

See All

Shadow Fight 3 guide - how to become a s...
Shadow Fight 3 is a fun new brawler with a few RPG elements thrown in for some extra flavor. Set in feudal Japan, you'll rely on just your fists and perhaps a blade or two to help you win out against your opponent. Combat is multilayered and... | Read more »
Animal Crossing: Pocket Camp guide - how...
| Read more »
Passpartout: Starving Artist (Games)
Passpartout: Starving Artist 1.0 Device: iOS Universal Category: Games Price: $4.99, Version: 1.0 (iTunes) Description: Passpartout puts you into the shoes of a French artist trying to navigate the beautifully confusing art scene.... | Read more »
Pocket Rogues: Ultimate (Games)
Pocket Rogues: Ultimate 1.0 Device: iOS Universal Category: Games Price: $2.99, Version: 1.0 (iTunes) Description: Pocket Rogues - a whole world consisting of generated locations and dungeons. This is an honest and harsh Action-RPG... | Read more »
The best new games we played this week -...
We took a break for Thanksgiving, but your favorite weekly roundup is back. It's once again time to take a look at the best new games that released this past week (or two). There's a good bit of variety, and the quality is outstanding. There are... | Read more »
Rules of Survival guide - how to protect...
In Rules of Survival, anything goes. The only real rule is to stay alive, and your opponents are going to do everything in their power to follow it. That means sneaking, sniping, and sometimes flat out fire fights. It can be tough adapting to... | Read more »
The guide to every villager in Animal Cr...
Each villager in Animal Crossing: Pocket Camp has a different personality type, unique furniture preferences, and gives out one of a few different materials. With over 30 villagers, there's a lot to keep track of, especially if you're after a... | Read more »
148Apps' Ultimate Animal Crossing:...
Animal Crossing: Pocket Camphas simply become a way of life for many of us. It's meant to be a fun little time waster to pop into every now and then for a few minutes, but we can't help spending any stray moment available to us with our furry... | Read more »
Fight fireballs with more fireballs in t...
Elemental Combat is a multiplayer game that challenges you to blast away your foes with fire and various other elements. In VR though, not in real life. You cast these moves with your real hands though, thanks to motion controls. Think The Last... | Read more »
Animal Crossing: Pocket Camp guide - how...
Animal Crossing: Pocket Camp is a game all about having the latest, greatest furniture and amenities, and that requires a steady flow of cash and crafting materials. Looking at the vast number of furniture pieces available for you to make, it can... | Read more »

Price Scanner via MacPrices.net

Holiday sale: 13″ Apple MacBook Pros for $100...
B&H Photo has 13″ MacBook Pros on sale for up to $150 off MSRP as part of their Holiday sale sale. Shipping is free, and B&H charges sales tax for NY & NJ residents only: – 13-inch 2.3GHz... Read more
Holiday sale: 15″ MacBook Pros for $220-$420...
B&H Photo has 15″ MacBook Pros on sale for up to $220 off MSRP as part of their Holiday sale. Shipping is free, and B&H charges sales tax for NY & NJ residents only: – 15″ 2.8GHz MacBook... Read more
Holiday sale: 12″ Apple MacBooks for $100-$24...
B&H Photo has 12″ MacBooks on sale for up to $150 off MSRP as part of their Holiday sale. Shipping is free, and B&H charges sales tax for NY & NJ residents only: – 12″ 1.2GHz Space Gray... Read more
Holiday sale: 13″ Apple MacBook Airs for $100...
B&H Photo has 13″ MacBook Airs on sale for $100 off MSRP as part of their Holiday sale. Shipping is free, and B&H charges sales tax for NY & NJ residents only: – 13″ 1.8GHz/128GB MacBook... Read more
Cyber Week: 9″ Apple iPads starting at $299,...
B&H Photo has 9.7″ 2017 iPads on sale for $30-$60 off MSRP as part of their Cyber Week/Holiday sale. Shipping is free, and pay sales tax in NY & NJ only: – 9″ 32GB iPad WiFi: $299, $30 off –... Read more
Cyber Week: 12″ and 10″ Apple iPad Pros on sa...
B&H Photo has 10″ and 12″ iPad Pros on sale for up to $150 off MSRP as part of their Cyber Week/Holiday sale. Shipping is free, and B&H charges sales tax in NY & NJ only: 10″ iPad Pros... Read more
Cyber Week: Huge discounts on new Apple Macs...
B&H Photo has Apple MacBook Pros, MacBook Airs, 12″ MacBooks, and iMacs on sale for up to $220 off MSRP as part of their Cyber Week Holiday sale. B&H charges sales tax in NY & NJ only,... Read more
Cyber Week: Save $150 to as much as $420 on M...
Save $150-$420 on the purchase of a MacBook Pro, MacBook, or MacBook Air this Black Friday and Holiday weekend with Certified Refurbished models at Apple. In many cases, Apple’s refurbished prices... Read more
Cyber Week: Use your Apple Education discount...
Purchase a new Mac using Apple’s Education discount, and take up to $300 off MSRP. All teachers, students, and staff of any educational institution with a .edu email address qualify for the discount... Read more
Miss out on Cyber Monday iPad Pro sales? Save...
Apple has Certified Refurbished 2016 12″ WiFi iPad Pros available starting at $589. An Apple one-year warranty is included with each model, and shipping is free: – 32GB 12″ iPad Pro WiFi: $589... Read more

Jobs Board

Budget Coordinator - Compensation Svcs. &...
# Budget Coordinator - Compensation Svcs. & Apple People Organization - Part Time Job Number: 113160143 Austin, Texas, United States Posted: 14-Nov-2017 Weekly Read more
DevOps Software Engineer, *Apple* Pay - App...
Job Summary Imagine what you could do here. At Apple , great ideas have a way of becoming great products, services, and customer experiences very quickly. Bring Read more
*Apple* Video Partnerships - Apple (United S...
# Apple Video Partnerships Job Number: 113059126 Santa Clara Valley, California, United States Posted: 15-Nov-2017 Weekly Hours: 40.00 **Job Summary** Partnerships Read more
QA Automation Engineer, *Apple* Pay - Apple...
# QA Automation Engineer, Apple Pay Job Number: 113202642 Santa Clara Valley, California, United States Posted: 13-Nov-2017 Weekly Hours: 40.00 **Job Summary** At Read more
Site Reliability Engineer, *Apple* Pay - Ap...
# Site Reliability Engineer, Apple Pay Job Number: 113163930 Santa Clara Valley, California, United States Posted: 06-Nov-2017 Weekly Hours: 40.00 **Job Summary** Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.