Saturday, November 20, 2010

Method for Checking the Values of Strings in Perl

Although I've been convinced that Perl is far from an ideal language to work with, I was required to recently do some text processing with it. Some strings seemed equal but apparently weren't because my code wasn't behaving as desired.

My brother came to my rescue twice, and came up with this handy script:
sub print_codes {
my $value = shift;
foreach (unpack("C*", $value)) {
print " '$_'\n";
}
}


Essentially one can pass the value of a string to print_codes() and it will print the codes for each character (including invisible ones) in the string.

In my case, we determined that 1. the input string was in UTF-16 (from a Gmail .csv export) but was being compared to a UTF-8 string, and 2. chomp was only removing \n and was leaving \r. Makeshift solutions for those issues:
  1. Remove extra \0 characters from the UTF-16 strings (careful if you deal with other languages, though):
    s/\0//g;
  2. Set chomp to remove \n\r at the beginning of your code:
    $/ = "\r\n";

Microsoft Office 2011 HUP Availability

14:25, 9 December 2010:
Update: Office 2011 is now available on the Home Use Program (HUP) site!

For those who qualify and have Macs, you've probably been wondering when Office 2011 will be available for the Microsoft Home Use Program. After some research on 26 October (when it was released for retail) turned up nothing, I sent a few emails and got this as a reply:
Thanks for your question. Office for Mac 2011 is expected to be available through the Home Use
Program in December 2010.

Let us know if you have any further question, thanks!
-Brittany

Further investigation revealed the Benefit Administrator FAQ from Microsoft (PDF or Google HTML cache), which contains the following statement on page 4:

"Office for Mac 2011 will be made available worldwide on December 9, 2010."

Here's to hoping...
Tuesday, November 9, 2010

How to Edit the Login Window in Snow Leopard

This shows how to open the login window files. It requires Apple's Developer Tools, which can be downloaded here for free if you have a free developer account.

I don't completely understand how the login window elements work together, so anyone with more knowledge can chime in through comments and I'll update this post. I do know these steps have worked since Jaguar (10.2) with some tweaks to the file location and without the compiled .nib file errors introduced with Snow Leopard's spring cleaning.

I recommend making a backup of the original .nib file. That way, if you somehow mess up and the login screen will not let you in, you can alway start up in Single User Mode and replace the copy with the backup. I also did all the changes through my root user so I didn't have to authenticate every change I made.
  1. Navigate to /System/Library/CoreServices/SecurityAgentPlugins/
  2. Right-click on loginwindow.bundle and choose "Show Package Contents".
  3. In the new window that opens, navigate to Contents/Resources/English.lproj/. If LoginWindowUI.nib shows as a folder, open and close Interface Builder, thus associating the .nib filetype.
  4. Open LoginWindowUI.nib and begin editing.

In Snow Leopard, the first time you do this, you will get the below error. Look here for information on how to get around this.
"The document "LoginWindowUI.nib" could not be opened. Interface Builder cannot open compiled nibs. Try opening the source document instead of the compiled nib."

How to Open Compiled .nib Files in Snow Leopard

When attempting to modify a file with Interface Builder, you'll possibly get the below error, particularly when editing system files:
"The document "LoginWindowUI.nib" could not be opened. Interface Builder cannot open compiled nibs. Try opening the source document instead of the compiled nib."
The problem comes from the .nib file not containing the classes.nib and login.nib files. The fix is pretty simple:
  1. First, download and unzip this, which has dummy versions of both needed files.
  2. Right-click on the file you want to edit and choose "Show Package Contents".
  3. A new window should come up, and there will be one file, keyedobjects.nib, listed.
  4. Copy classes.nib and login.nib into this folder.
  5. You should now be able to open the original .nib file.
Sunday, November 7, 2010

How to Easily Enable Root in Snow Leopard

Make sure to choose a secure password. This requires an administrator account.

  1. Open Terminal.
  2. Type the following:
    sudo passwd root
  3. Enter your administrator password.
  4. Enter a new password for your root account.

This can also be done through an OS X utility called Directory Utility:
  1. Open Directory Utility, which can be found in the /System/Library/CoreServices/ folder.
  2. Click the lock and enter your administrator password.
  3. In the Edit menu, choose "Enable Root User".
  4. In the Edit menu you can also give the root account a new password by choosing "Change Root Password".
Friday, November 5, 2010

How Make a Shell Script Run in Snow Leopard

When trying to run a new shell script in bash, you may get the following error:
-bash: ./samplescript: Permission denied
Navigate to the file and type the following:
sudo chmod u+x samplescript
You should then be able to run said file by typing this:
./samplescript

How to Change the Default Command Line Editor in Snow Leopard

These are instructions to change your default command line editor in Snow Leopard.

  1. Create or edit a file in your home directory called .profile (make sure you include the ".") using TextEdit or the editor of your choice. Note that files beginning with a period are invisible in Finder, so to use TextEdit you may have to open it with Terminal using the following command:
    open ~/.profileIf you get the following error, the file does not yet exist and needs to be created:
    The file /Users/randomuser/.profile does not exist.
  2. Add one of the following lines to the file based on your preferred editor:
    • emacs:
      export EDITOR=/usr/bin/emacs
    • vi:
      export EDITOR=/usr/bin/vi
    • TextEdit (may have side effects):
      export EDITOR=/Applications/TextEdit.app/Contents/MacOS/TextEdit

How to Easily Enable PHP on Snow Leopard

These are instructions to quickly and easily enable PHP on Snow Leopard. I won't discuss any other configurations on this post beyond those necessary to get PHP running.

  1. Open Terminal.
  2. Open /etc/apache2/httpd.conf in your favourite command line editor using sudo. I did the following:
    sudo emacs /etc/apache2/httpd.conf
  3. After authenticating, the file will open. Scroll down to the line that says:
    #LoadModule php5_module libexec/apache2/libphp5.so
  4. Remove the hash mark (#) to uncomment that line, then save and close the file. In emacs, this is done by pressing Control-x Control-s (save) followed by Control-x Control-c (close).
  5. Restart the Apache web server.
  6. PHP should now be up and running!

How to Start or Stop the Apache Web Server on Snow Leopard

These are instructions on how to start, stop, or restart the Apache web server on Snow Leopard.

GUI method:
  1. Open the Sharing prefPane in System Preferences. The checkbox by "Web Sharing" indicates if Apache is started or stopped.
    • To start Apache, check the "Web Sharing" box.
    • To stop Apache, uncheck it.
    • To restart Apache, uncheck then check it.

Command line method:
  1. Open Terminal.
    • Type the following to start Apache:
      sudo apachectl start
    • Type the following to stop Apache:
      sudo apachectl stop
    • Type the following to restart Apache:
      sudo apachectl restart

To test the status of Apache, visit http://localhost in your browser. If the page loads, Apache is started; elsewise it is stopped.

How to Easily Install MySQL on OS X

These are instructions to quickly and easily install MySQL on Snow Leopard, and will also work on Lion. I won't discuss mysqladmin or any other configurations on this post beyond those necessary to get the MySQL server running.

  1. Visit http://dev.mysql.com/downloads/mysql/. Make sure the platform is "Mac OS X".
  2. Click the "Download" button next to the 32- or 64-bit DMG archive. (For me, this was Mac OS X ver. 10.6 (x86, 32-bit), DMG Archive.)
  3. Let the .dmg file download, then open it. There should be a file called something similar to mysql-5.1.52-osx10.6-x86.pkg. Open it.
  4. Click through the installer with the default settings. The installation took about 10-15 seconds.
  5. The files install in /user/local/mysql/, so we need to set PATH variable to look in there. Create or edit a file in your home directory called .profile (make sure you include the ".") using TextEdit or the editor of your choice.
  6. Add this line to the file:
    export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"
  7. You can then start the MySQL server by installing the prefPane (instructions below) or opening the Terminal and typing the following:
  8. sudo mysqld_safe &
  9. MySQL should now be installed and ready to use.

Optional installs:
  • Install MySQL.prefPane. This has GUI on/off switch and startup options:
    1. Return to the mounted disk with the MySQL installer.
    2. Double-click on MySQL.prefPane. It will install after you authenticate.
  • Install Sequel Pro. This is a free full-featured GUI interface for database creation/maintenance:
    1. Visit http://www.sequelpro.com/ and click the "Download" button.
    2. Let the .dmg file download, then open it.
    3. Drag Sequel Pro to your Applications folder.
  • Install MySQLWorkbench. This is the official, albeit less-polished, interface for database creation/maintenance:
    1. Visit http://dev.mysql.com/downloads/workbench/. Make sure the platform is "Mac OS X".
    2. Click the "Download" button next to the 32- or 64-bit DMG archive. (For me, this was "Mac OS X (x86, 32-bit), DMG Archive".)
    3. Let the .dmg file download, then open it.
    4. Drag MySQLWorkbench to your Applications folder.