This page provides various
scripts that I have written over the years. Most of them were written and tested
on Mac OS X, and some are specific for OS X,
but most should run on any platform where Perl is installed.
File & Folder Management
Files on OS X can have resource forks and other extended attributes and
as of Tiger (10.4) the 'cp' command copies these extended attributes.
This script copies files to a specified destination
but suppresses the copying of extended attributes.
This is useful when the destination is on a non-HFS volume
in order to avoid the "._" files that would otherwise be produced.
This Perl script can be conveniently invoked from an AppleScript droplet:
Attempts to identify which files (with a specified suffix) might be duplicates
by looking at the MD5 digests.
macosxhints article for further discussion.
Displays the "creator code" and "type code" of Macintosh files.
(See also 'setFileInfo' and 'show_app_creator_code')
Reproduces the functionality of 'grep -l'. (This is useful since there are some
patterns that are difficult with 'grep'.)
Illustrates how to go on to the next file when using the <> magic to
iterate over all lines in all input files.
Lists the files in the specified directory.
It gives the info from 'stat' (i.e. similar to 'ls -l')
Lists the files in the specified directory
but shows numerical sequences as one item.
E.g. the files file1.txt, file2.txt, file3.txt would show as file[1-3].txt
(See also 'nextFilename')
Displays the name to be used for the next file in a specified directory
where the files are named in a numerical sequence.
E.g. if the files file1.txt, file2.txt, file3.txt exist,
the command 'nextFilename file1.txt' would output "file4.txt".
(See also 'listdir_seq')
Displays the values of the bytes at specified offsets in a specified file.
(The name 'peekFile' is supposed to suggest the 'peek' command of BASIC)
Edits a binary file by replacing the bytes at specified offsets
with the supplied values.
(The name 'pokeFile' is supposed to suggest the 'poke' command of BASIC)
Renames files where necessary so that the filenames are distinct
on case-insensitive filesystems (like HFS+ on OS X)
Renames files where necessary so that the filenames are acceptable to MS Windows
Renames files where necessary to remove leading or trailing whitespace
in the filenames
Reports on files whose full paths are longer than a specified threshold.
This is useful when long paths are causing trouble when copying files to some
This script illustrates a simple use of the File::Find module.
Resolves a Finder alias.
Sets the "creator code" and "type code" of Macintosh files.
(See also 'getFileInfo' and 'show_app_creator_code')
Displays the 4 character "creator code" for the specified application
Displays the contents of files as base 2 numbers
Splits a (presumably large) directory by moving its files
into several sequentially named sub-directories.
Tests the speed of file copying - reports on the read, write, and transfer
speeds while copying a file from a specified source to a specified destination.
(This is intended for use when troubleshooting slow copying of files from a
networked drive for example.)
Sample usages are explained in the comments at the top of the script.
Displays info when a specified directory/folder changes
Displays info when a specified file changes
Writes specified text to a temporary file and outputs the name of that file.
This illustrates how to write to temp files in a secure way.
Outputs (to STDOUT) a specified number of random bytes
Outputs (to STDOUT) a specified number of bytes that cycle from 0..255
Determines the string that is the common start of a list of strings
Combines specified columns from several CSV files into one CSV file.
(If you are interested in CSV utilities, see also:
Reports on which out of a set of expected values occurred in the specified files
Creates a Perl module with the info from the 'enum's in a C/C++ header file.
Illustrates how to create a module where all the variables are automatically
Shows the letters (in alphabetic order) that occur in specified strings.
Prints out all permutations of an array of strings
Checks on C pre-processor macros and reports macro definitions
that are not properly parenthesized
Performs a set operation (union, intersection, etc) on data from files.
Checks text files for the presence of gremlins.
(A "gremlin" is the BBEdit name for an unwanted character, often invisible,
that has somehow gotten into your text file.)
Outputs the contents of an HTML file after stripping off the HTML tags.
Illustrates how to use the HTML::TreeBuilder module.
Search the text of the US constitution
(as provided by www.usconstitution.net)
for a user-supplied search term and show the matching lines
with a few lines of surrounding context.
Could easily be modified to search any other text document.
Gets the "resident set size" of a specified process.
This provides a rough measure of how much RAM that process is using.
Reports on the top RAM-consuming processes, swap-file usage and pageouts.
Reports on the 'sudo' timestamp
Illustrates the effect of various
Gets the "external IP address" by extracting it from the HTML
returned from a publicly available web site that provides this service.
ash (AppleScript shell)
Allows interactive use of AppleScript (like a Unix shell)
Runs the script (shell script or AppleScript)
that is currently that is currently in the Clipboard
- i.e. the script text that had previously been copied to the Clipboard.
(see also 'runScriptFromWebPage')
Runs the script (shell script or AppleScript)
that is currently selected on the frontmost web page
(see also 'runScriptFromClipboard')
An example of a very simple "shell" program - intended as an illustration of the
Illustrates how to wait for a keypress in Perl
Converts epoch time (seconds since January 1, 1970) to a date in
ISO 8601 format.
An example of a
named pipe or FIFO.
Run this script and then read (e.g. with 'cat') from the fifo
and it will read a random jpg file from the image directory.
(See also 'logFifo')
This script echos the name of the currently active user.
(this is useful when using Fast User Switching)
This script also serves as an illustration of using the 'scutil' command.
This script can be used to download the Darwin source code
(i.e. the open-source part of OS X) assuming you have an APSL account.
It illustrates the use of the Perl "LWP" module.
Reports the values of specified properties of an 'ioreg' object.
(The 'ioreg' command on OS X displays values of the "I/O Kit registry"
which includes a lot of information about the current state of the hardware
on an Apple Mac machine.)
An example of a
named pipe or FIFO.
Run this script and then write (e.g. with 'echo') to the fifo.
Each message sent will be output to STDOUT tagged with the current time.
(See also 'fifimage')
Shows info about the cookies that are in a Cookies.plist file
Illustrates how to run an embedded Perl script
(source embedded in the current script)
as a separate process. The new process (running the embedded Perl script)
is completely independent of the parent process.
It will continue to run even after the parent dies.
This script shows the the environment (variables, etc) that is used
for scripts run via 'launchd'.
It also serves as an illustration of how to run 'launchd' scripts.
Text to semaphore converter that creates a QuickTime movie showing the
sequence for a given text string.
The flag images are derived from those on the above Wikipedia page.
QuickTime command-line tools "qt_tools" to turn a sequence of images
into a movie. If you want to run this, get the
zip file with semaphoreMovie script and images.
Speaks text using the
NATO Phonetic Alphabet
Illustrates how text output can be sent to STDERR as well as STDOUT.
This script can be used to test output redirection in shell scripts
Purports to check for viruses - but in fact doesn't do anything other than
traverse the filesystem while reporting its progress
Illustrates how to read and write to files in Perl.
Outputs specified strings to STDOUT separated by zeros.
This is useful, for example, to generate a spelling dictionary
in the format apparently used by OS X's built-in spell checker.