As a Mac OS X administrator and as someone who likes to use shell and perl scripts to automate things, I find myself doing a lot of debugging and logging. To make things simpler, I wrote a perl script which I call from other scripts to output errors, warnings, and info in an ordered, easy-to-read system-log-like format.
Save the script text to a file (logger.pl), open Terminal and change the file mode to allow execution (chmod u+x /path/to/logger.pl), and call the script within (or without) another a script like this:
The process list of Activity Monitor is very nice, but oftentimes I want a lightweight alternative that is easy to start, and has very little impact on system resources. In order to achive this goal, I started with the top command as my basis. In its standard mode, top is still a bit resource hungry, so I added a few options (remove costly traversals of kernel datastructures, update every 10 seconds, sort by resident memory size):
/usr/bin/top -R -F -s 10 -o rsize
For making this command easily usable (either by double-click or by Spotlight opening), I used the Execute this command feature (Execute command in a shell activated) with the exec command at the beginning of the above command line in the Save dialog of Terminal. Together with setting the Close the window option in Window Settings... > Shell, this ensures that very little memory is consumed (the shell is replaced by using exec). The resulting Terminal window containing top can be easiy terminated by just typing q when it is active. Additionally, the scrollback buffer should be disabled in Window Settings... > Buffer.
Saving the resulting file under the name topmem.term makes it possible to either double-click the file, or start it by typing topmem in Spotlight and hitting Command-Return. Additional commands can be built, which sort by different fields (e.g. -o cpu, -o time). I have put a ready-made topmem.term as shown above (with green on black colors and slight background transparency) on my website.
[robg adds: I haven't tried the linked topmem.term file, but the rest works as described.]
The great and free GeekTool (PowerPC | Intel) can write the contents of any file, and the output of any shell command, right onto the desktop. Unfortunately it doesn't support self-updating commands like top, and the official workaround (break the loop with top -lk and set Refresh to keep restarting it) is rather Heisenbuggy:
With k > 1, they become skewed by an overestimated CPU usage for top itself.
Either way, relaunching top every few seconds soon causes pid's to roll over 30000, so that recent processes must be hunted down the list rather than conveniently appearing on top.
In short, watching top this way perturbs it more than we'd like. In this hint, I describe a different (k = 0) workaround which appears to solve the problem, i.e. display top exactly like Terminal does.
Looking through applications in Terminal, I stumbled upon automator, which simply lets you run an existing Automator workflow from the Terminal. This could be useful in Perl, Bash, and other scripting.
[robg adds: The man page for the command is quite brief; it says just this: automator runs the specified workflow document. To create or edit a workflow, use the Automator application. There are no options.]
This hint allows you to randomize your e-mail signatures. I use this with Thunderbird, but this hint should work with any e-mail client that refers to an external file for your signature. I needed a solution that just worked and didn't really require anything more from me. Some of the plug-ins out there require some user action each time you write an e-mail -- which I didn't like. Also, most of my signature remains the same, so I wanted my solution to simply change one part of my signature (a tagline), the rest remains the same.
Basically, my solution is composed of a couple files which can be merged to form a random signature (and any supporting graphic files, etc). These should be placed in your normal directory holding your signature. For this example, let's say this is the directory /Users -> jbloggs -> signature/:
signature-template.html - my normal signature (and all of its support files), but with a minor change: I've replaced the tagline with a marker [TAGLINE].
taglines.txt - this is a list of all my taglines that I want to randomly choose from for each e-mail. There is one tagline per line, and any characters that might be special to the shell must be escaped properly.
Other files required to make up this solution are the following, which are to be placed in the directory /Library -> StartupItems -> SignatureRandomizer, which is owned by root, has its group set to wheel and has permissions 0755 (drwxr-xr-x):
SignatureRandomizer.watchdog - this is the magical bit. This shell script monitors my signature.html file to see if it has been used, and if it has, it creates another one by randomly selecting a line from my taglines.txt file and inserts it into the signature-template.html file.
SignatureRandomizer - this script controls the watchdog-script.
StartupParameters.plist - this is required to automatically start the service when my Mac boots up.
You may not have noticed, but a number of recent Apple updates (eg. at least the 10.4.5 through 10.4.7 updates) have included the SetFile utility in their installer packages, leaving a copy in /Library/Receipts.
To find what is available on your system, try either of these commands (the first may include some unrelated results, but it's faster, assuming you've created the locate database) in Terminal (found in /Applications -> Utilities):
$ locate SetFile | xargs ls -lut
$ find /Library/Receipts -name SetFile | xargs ls -lut
SetFile will be familiar to many users as the handy command line tool for making files and folders visible / invisible (if their visibility isn't already being determined by something else), changing their type or creator codes, setting file creation date, or other functions, as detailed in its man page
So for example, if you have a PowerPC Mac, and the 10.4.7 update was installed with the Delta (not Combo) installer, and you have a folder on your Desktop" called Inviso, it should be possible to hide that folder with this command:
Contents/Resources/SetFile -a V ~/Desktop/Inviso
Quit and restart the Finder (don't Force Quit or Relaunch from the Dock), or log out and back in for the change to take effect. Repeating the procedure, substituting v for V will reverse the change.
Maybe this will let users without the Developer Tools get in on the various MacOSXHints that call for SetFile, or save someone suffering from the appearance of a previously-hidden folder or symbolic link the bandwidth of downloading the entire Developer Tools package (version 2.3 weighs in at 915MB) just to get access to SetFile, which itself is only 41KB for the Universal binary.
I spend a lot of time in Terminal, and a lot of time in GUI applications. There have been a few hints about using the Finder with Terminal, so here's mine to add to the collection.
I created some shortcuts for CodeWarrior and Xcode, as those are the programs I use most with Terminal. The shortcuts execute a cd to change Terminal's active directory to match that of the open CodeWarrior or Xcode document. I did this by creating the following six functions in my .profile file.
I have long wanted a way to store and retrieve Desktop items. That is, change the items on my Desktop according to the task I'm currently working on -- dev, doc, design, surf. Well, I tried with some simple mv commands in Terminal, and it seemed to work fine. So I wrote a short script to handle this task.
I placed my script in ~/bin, and remember to make it executable (chmod 755 scriptname). The first time you run it, it will create a top-level storage directory in ~/Library/Desktops. It will then create a file named _current_ in that directory, which will contain the name of the "current" desktop items that are in ~/Desktop.
The first time the script is run, the "current" desktop item storage will be called "default." If the desktop script is run without any parameters, it will list the available storage names. The current one will be marked with >>. If a paramater is supplied to the script, it will either retreive the items from an existing storage bin, or create a new storage bin if none exists with that name.
I have only tested it on my PowerBook with 10.4, so I made 10.4 a requirement. I guess Spotlight is needed to get the update done in the Finder. I imagine that one should only use storage names with ordinary identifiers -- that is, they should start with a letter and not contain any spaces or other weird characters. I have not tested using non-standard names; the script would need some work to handle them properly, I think.
I do not take any responsibility for this script and what it does. mv can be destructive so be careful. It hasn't broken anything for me yet, but I have only been using it for a couple of days. Usually I only make scripts for own usage -- I'm no expert in scripting, so if you can see any flaws or obvious mistakes, please add a comment.
[robg adds: I tested this on a Core Duo mini, and it seems to work just fine. It's an interesting idea, sort of like a simplified version of true virtual desktops.]
A long time ago, I wrote a hint for automatically creating tcsh aliases so that you could launch Mac applications from Terminal by typing in their names (i.e., safari would open Safari, quicktimeplayer would open QuickTime Player, etc..). Because this works with the shell's built-in auto completion, it means that you can launch applications faster than Spotlight can find them, and certainly faster than hunting them down with the Finder!
Unfortunately, somewhere along the way, Apple switched the Mac's default shell from tcsh to bash. I stayed with tcsh because, well, habit, really, and I never got around to making the switch until now. It turns out that, with some syntax changes, the alias generator works just as well in bash as it did in tcsh.