Archive for the 'System' Category

May 29 2008

Comparing Directories

Published by under System

The typical use of diff from within a terminal (using the default Bash shell) is to compare files. However, with a little slight of hand, known a process substitution, we can use diff to compare the contents of two directories.

The syntax looks as follows:

<(commands)

>(commands)

Commands represents a single command (such as find) or a piped list of commands. The end result of using process substitution is that the commands act as a file. In the first example, the process substitution results in what looks like a file that can be fed into another command. The second example is the opposite, the process substitution enables one or more commands to act as a file for input. Let’s look at how we can use process substitution for comparing directories.

Let’s say we want to compare the contents of the two directories above. The screenshot was captured from Finder, and in this case, provides a concise view of each directory. However, if the directories are not as conveniently located (in the same sub-directory) or have many more files, or if you need to compare directories from within a terminal, then you could run the following from a terminal:

$ diff <(find tmp1) <(find tmp2)

Here we use the find command to walk through the directories. Using the process substitution, the results of the find command look as though they are files, which enables us to feed both into diff.

You can learn more about process substitution on the Bash man page at the Apple Developer Connection.

One response so far

May 16 2008

Login Screenshot Challenge – Part 3

Published by under System

As explained on the previous post, no doubt there is a means to capture a screenshot of the login screen, including all available user names. The question is, how to get there from here? Brett on the Cocoa developer mailing list provided a link to OpenGLScreenSnapshot, which is a Mac OS X application that demonstrates how to use OpenGL to grab a snaphost of the screen and save it to an image.

The example is an Xcode project which is easily imported and ran from with Xcode. The only problem is that the short example uses a menu to invoke the screencapture, so there is no means to logout and invoke the application.

I imagine the code change to make this an application that can be run from a terminal (at which point it could be run in the background using sleep) is trivial. However, problem is, I haven’t spent any time working with Xcode so it may take some time to figure this out.

Needless to say, it’s been an interesting challenge to try and capture the login screen. As I mentioned before, I think we’re 99% of the way there and if anyone can take the above example and make a command line version of the application, who knows, maybe that will bring this thing full circle.

It’s been a great diversion. So what’s the next challenge we can take on ?

8 responses so far

May 14 2008

Login Screenshot Challenge – Part 2

Published by under System

In Part 1 of this series I described my attempts to get a screenshot of the login screen on Mac OS X. The attempts included using hotkeys, Grab application, fast user switching and putting the system to sleep and using the terminal utility ‘screencapture.’ All to no avail. The real challenge at hand is that I am looking to accomplish this feat from just one Mac system.

There were several ideas shared by others, including using cron, apple remote desktop and ssh. Again, good intentions, however, no luck.

I was able to dig up one approach that get that gets us 99% of the way there. From a terminal, I can enter the following, where I specify the login name using the switchToUserID parameter:

/System/Library/CoreServices/”Menu Extras”/User.menu/Contents/Resources/CGSession -switchToUserID `id -u root`.

The last parameter `id -u root` uses the terminal command id to convert a user name to a number. Upon running this command, a login window is popped up for the login id specified, see the figure below.

If I change the login id to another user, the login window changes as well:

The 1% that remains is that what I really hope to capture is the login window where all available login ids are shown.

There is a related command (below) that displays the login window I am after, however, this brings me back to the same problem I started with, that is, no means to get a screenshot.

/System/Library/CoreServices/”Menu Extras”/User.menu/Contents/Resources/CGSession -suspend

With some further digging I was able to successfully get the screenshot that I’m after with a couple of approaches:

  1. Using a screen recording program such as ScreenFlow: enable fast user switching, start the recording and select the login window option from the fast user switching dialog. This will capture the sequence of events including the elusive login window.
  2. I also found a few screenshot applications that can successfully capture the login window while running in the background. For example, Christian at MonkeyBread Software wrote a freeware application in REALbasic TimedScreenshot (here’s the source code) that will capture the screenshot I am after:

So, it’s obvious we can get there from here. The question that remains is, what system level API/library can we call to make this happen?

Ideally I’d like to have a very simple application, written in as few lines of code as possible, using tools that are freely available (e.g. Xcode or even better, just terminal commands). If this involves running a program in the background using a terminal command such as sleep (as I did with screencapture in the previous post), that works for me.

Any developers (Cocoa or otherwise) who can take this challenge the last mile?

3 responses so far

May 12 2008

Login Screenshot Challenge – Part 1

Published by under System

In my last post I was showing various options for working with the root account. Near the end of the post I was planning to show how when logging in as root, the login dialog does not offer root as one of the available login names.

Attempting to capture the login screen has been an interesting diversion. Up to this point I’ve been unsuccessful in capturing the elusive screenshot.

So here’s a challenge for you…can you be the first to figure out how to capture a screenshot of the login screen for the root account? There are a couple of ground rules:

  1. You must be running Leopard (or at a minimum, the solution must work on Leopard).
  2. I’d like this to be a “free” solution, as in, no software to buy.
  3. Finally, I am looking for a solution that doesn’t require more than one Mac (e.g. using SSH from one Mac to another).

To save you some time, I’ve listed below the options I’ve tried:

Attempt #1 – Hotkeys
From the login screenscreen, I tried the following hotkeys:

- Shift+Command+3 (capture entire screen to file)
- Control+Shift+Command+3 (capture entire screen to clipboard)
- Shift+Command+4 (capture area defined with mouse to file)
Each of the above greeted me with a pleasant beep, nothing more.

Attempt #2 – Grab application
My next attempt was to try the timed capture option with the Grab application. Seemed like a clever idea, however, when I started the timer and logged out (to get to the login screen), Grab just seems to quietly go away. No warning, no error, no screenshot.

Attempt #3 – Fast Switching
Next, I enabled fast user switching. My thinking was, maybe if I don’t logout per se, and request the Login Window (see the figure below) that the login screen might somehow be different (as in, something I can capture using hotkeys):

Seemed logical, however, first notice that the root account doesn’t show up in the list of available accounts. Next, upon choosing Login Window I was shuffled over to the same login screen as if logging out. No luck.

Attempt #4 – Getting a little more creative, I tried this from within a terminal:

screen -d -m bash -c “sleep 30; screencapture /Users/John/login.png”

This option uses a screen manager (more on that in an upcoming post) and calls the screencapture application after sleeping for 30 seconds. Once I ran this option from a terminal, I quickly logged out and patiently waited…I heard the camera sound of the screencapture application and was psyched, “yes!”…I navigated to my home directory and located the file login.png…I opened the file with the Preview application…and much to my dismay…it was an image alright…a big, black, blank image. So close, but no cigar.

So, now it’s up to you. Can you figure what it takes to capture the login screenshot? You can submit your idea as a comment (below) or drop me an email. And please verify your trick works with Leopard, as other published solutions I’ve found seem to work only on releases prior to Leopard.

I’m doing my best to see if I can find a volunteer to donate a prize (software, book, t-shirt, etc). If that doesn’t come through, what I can offer is a little lime-light, blog style. If you are the first to contact me with a solution (within the parameters above), we can collaborate on a post where you can tell the world how you got so smart :) and share your bio, picture, blog/website, etc.

If anyone reading is interested in donating a prize, contact me and we’ll get the ball rolling.

I am very intrigued to see if/how this can be done.

10 responses so far

May 09 2008

Working as Root

Published by under System

Root is the uber system account. Although handy at times (for example when installing applications), it’s generally recommended that root not be used as your everyday login. What follows are some options for working as root.

If you need to run a command as root, you can use the sudo command. For example, to run the script for daily system maintenance, you can run this command (the $represent the terminal prompt):

$ sudo periodic daily

Now that’s all well and good, however, it’s generally applicable only for one command. What if you are in a terminal and want to login as root to do some larger scale maintenance or system work? Try this:

$ sudo -s

This command will enable the root account and update your prompt as shown in the figure below:

The above screenshot assumes you are using the bash shell and have admin rights on the account you are logged in with. The $ typically represents a user account whereas # represents root.

The last option is to create/enable a root account, thus you can login (when restarting/rebooting) as root. This approach offers the most flexibility and power, however, use with caution as there are no limits to what (damage) you can do.

Root account option #1: Enter the following from a terminal:

$ dsenableroot

You’ll be prompted for your (current) password, as well as the new root password.

Root account, option #2: Enter the following from a terminal:

$ sudo passwd

You’ll be prompted for the same series of passwords as above.

Root account, option #3: Follow the steps below:

- Start the Directory Utility application (/Applications/Utilities/Directory Utility)
- Click the lock icon in the lower left corner (to unlock it)
- From the Edit menu (across the top of screen), select Enable Root User

To wrap up this post I was planning to show you how the login window changes when a root account is enabled. Unlike other accounts on your system where a name is typically displayed as one of the login options, the root account is not listed as one of the options (for security reasons).

However, getting a screenshot of the login screen is a considerable feat. This is a great segue to a post coming next week where I’ll show the tricks that I tried for capturing the elusive login screen and I will pose an intriguing challenge for you…

Comments Off

May 02 2008

Changing an Application Icon

Published by under System

One of the defining concepts of working on a Mac is that things (usually) work just as you think they should. This is the culmination of good design translated into working code. The short video that follows is an example demonstrating how easy and intuitive it is to change the icon for an application. Believe it or not, it’s as simple as copy/paste.

This is a great trick to have up your sleeve when working with development tools that create a default icon, for example the Script Editor. With this approach you can quickly change an icon to reflect that something visually represents what the application does. And to help, do a quick search on Google for Mac OS X icons and you’ll be amused for hours…

7 responses so far

Apr 28 2008

Multiple Monitors, Moving Menu Bar

Published by under System

Working with multiple monitors is a must when doing any serious coding work. Code on one side, output on the other. No more swapping applications from the foreground to background.

Mac OS X make working with multiple monitors a snap: plugin in the secondary monitor, open System Preferences, click on Displays, click on the Arrangement tab and arrange the displays by dragging and dropping. That’s it. In the figure below the larger box is the secondary monitor (larger view area) and the smaller is my laptop.

There is one default setting that I want to mention here that hopefully will save you some time. When you set up an additional monitor the menu bar stays on the original display. Depending on your arrangement, this may be no worry. In my case I wanted the secondary monitor to be the primary display. When working on an application in what is now my primary display, it’s anything but intuitive to move the mouse back over to the laptop display to click a menu item.

I spent some time searching for a tip, trick or hack to fix this little problem, to no avail. I went back into the system settings any number of times looking for a preference setting. I even did a search for a Mac Defaults option to specify where the menu should land. Again, no luck.

Here’s the punch line, the fix was so obvious, it wasn’t obvious. If you look closely at the figure above, notice the second line in the description area…”To relocate the menu bar, drag it to a different display.” Heh, was that there the whole time…??

So here’s the tip: to move the menu bar simply drag-n-drop as you prefer.

Okay, so here’s where I have a question for you. I recently upgraded to Leopard (10.5), and I’m curious if this feature existed on Tiger (10.4)? Essentially I’m looking for an excuse as to why I didn’t see the obvious solution to my question, right in front of me. Maybe you can help me out here :)

74 responses so far

Next »