Category Archives: OS X

The OS that has solved all my problems. I used to be a Windows admin person for a small company, who used Linux at home, and wished for an OS that was like Linux but could run PhotoShop – enter OS X.

Combo Pack: CakePHP and Leopard Virtual Hosts

I’ve been checking out CakePHP, and while I haven’t really fully explored it yet, I’m pysched to have learned how to get numerous test sites up and running under Leopard’s default installation of Apache and PHP. It was a bit of an adventure, so I’m taking notes here that will a) remind me how I did it and (b) hopefully save somebody else some time. This assumes some technical skill at the command line, and use of a trusty text editor.

Step One: Get MySQL, PHP, and Virtual Hosts running on Apache

I’ll be running all my applications from my home user “Sites” directory (~/Sites). If you haven’t already done so, install MySQL – I followed these directions with a strong cup of coffee and got it working.

Next, I followed this excellent Cake Tutorial, which includes how to get PHP and Cake up and running (as well as the Cake Command Line tool), and begins to cover virtual hosts set-up (which we’ll modify a bit later). It also runs over how to get MySQL up and running, but I didn’t try it as I already had it installed. Getting PHP and VHosts up and running is as easy as commenting out two lines in httpd.conf.

Note that the Cake tutorial will say that you’ll be able to view the website right after editing httpd-vhosts.conf – this is not true. You will need to complete the next step and edit the /etc/hosts file as well, and then you can view the site.

Why Virtual Hosts Are Cool

Because you can simulate domains on your local computer. For example, you can create and view a development site like http://www.caketest.dev right in your browser, with full PHP/Database functionality, as opposed to something less fun like http://127.0.0.1/~username/caketest – this sort of URL breaks many of CakePHP’s references, which means your external CSS and JavaScript files won’t get loaded.

Give Me More Domains, Dammit

OK, by now you might want to have several testing domains setup – a great way to avoid trying out new things on a live website. After a bunch of research and experimentation, here’s how I got it working.

For reference, my user folder is “mmayes” – I’ll be referring to it in the following commands.

We’ll need to open some buried files, so let’s use the command line. The “mate” command here opens the file in Textmate, but you can use vi, vim, emacs, nano, etc.

sudo mate /etc/hosts
sudo mate /private/etc/apache2/extra/httpd-vhosts.conf

Stop web sharing in System Prefs -> Sharing.

I have two cake test sites up and running: caketest and cakeblog. Here’s what my httpd-vhosts.conf looks like. Note the <Directory … > options are to allow .htaccess to work:

NameVirtualHost *

# default folder when none specified

  DocumentRoot "/Users/mmayes/Sites"



  DocumentRoot "/Users/mmayes/Sites/caketest"
  ServerName caketest.dev
  ServerAlias www.caketest.dev
  
    Options FollowSymLinks
    AllowOverride All
  



  DocumentRoot "/Users/mmayes/Sites/cakeblog"
  ServerName cakeblog.dev
  ServerAlias www.cakeblog.dev
  
    Options FollowSymLinks
    AllowOverride All
  

And here’s what /etc/hosts looks like:

### Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1    localhost
127.0.0.1   www.cakeblog.dev
127.0.0.1   www.caketest.dev
255.255.255.255    broadcasthost
::1             localhost
fe80::1%lo0    localhost

Restart web sharing in System Prefs, and you should be able to view each domain.

I assume to add more domains, I can just proceed along these lines. Hope this makes sense! Cheers.

References:
Apache 2 Only Servers First Virtual Host | alexking.org
Installing CakePHP on OS X Leopard | KeithMedlin.com
Installing MySQL on Mac OS X | HiveLogic.com
CakePHP.org

UPDATE 11/11/08:
To get your Mac virtual hosts working on your Parallels Desktop machine, follow these easy instructions.

Using BBEdit’s Update Document Feature

I’m becoming a hardcore Textmate guy when it comes to getting things done. When I open any other Texteditor, it’s using Emacs or Vim, and I’m just messing around (and discovering that Textmate has many of their cool features), and wondering when, when!?, is Textmate 2.0 coming out? *sigh* I can’t wait to see the new features.

Anyways, there is one thing I keep using BBEdit for – repetitive HTML newsletter design. With all the inline styles you have to do with newsletters, any sort of automation setup pays off.

click for full sizeBasically, you take 2 files, one with your variable declarations and one with your HTML template, and run the Update -> Document command to spit out the completed HTML file. Here’s how it works – I’ll run through the step I take to do the ChronicBabe.com newsletter. In this example, I’m creating this newsletter: How to get out of a rut.

  1. I receive the newsletter’s content in an MS Word document. I copy the entire thing into a new BBEdit window, and clean up all the MS Word gobbly-gook with TextSoap.
  2. I open my variables file in a new window, and just drag and drop the appropriate text into the variables. Note that this file needs to be formatted a certain way. At the top we need:
    <!-- #bbinclude "template.html"

    where “template.html” is the relative path to our HTML template, using the same folder your variables are in makes it easy. Our variables then look like:

    #h1_text# = "put your header text here"

    And at the very end of the file we need:

    -->
    <!-- end bbinclude -->

    Note that you cannot have quotes inside the variables! You need to either escape them with a backslash (which means un-escaping the finished file), or use their HTML entity. This is why I separate as many links from the content as possible, like in the Amazon and Fab 5 Sites sections of this particular newsletter.

  3. click for full sizeWhen you’re all done, select Markup -> Update -> Document. If all goes well, your variable file will “suck in” the template file, complete with the variables. If it looks like it stopped short, that likely means you have a stray quote somewhere. Locate it, Undo the update, and try again.
  4. Though variables are all commented out, I usually clean everything up and cut/paste the final HTML, sans variable declarations, into a clean file.
  5. From there, I save and reopen the newly minted HTML newsletter in Textmate for further tweaking and image placements. Often this is just placing text links, as my client often provides them as “… check out this site (link to: www.somesite.com).” Textmate makes this trivially easy with the hyperlink helper function, and I hardly touch the mouse, which is how I like it.
  6. From there, the newsletter is ready to go!

For monthly newsletter service, I typically charge a flat fee to keep billing easier. This saved me so much time that I actually lowered the fee for my client. Call me crazy, but when you run a referral based business, it rewards you with more business. Cheers.

Textmate & Running Python Scripts in Interactive Mode

So yeah, as I check out Emacs, I also checkout things in Textmate that I might not know about, and I’m learning that Textmate can do *lots* more cool stuff than I thought it could. One of those is (yes!) Textmate does have Python debugging built in (both PyCheckMate as well as the standard Python debugger are ready to go out of the box). And with a little tweaking, I can send a Python script I’m working on to the Terminal in interactive mode (Shift+Apple+R). Typically this command just runs the script in Terminal, but this little tweak leaves you in interactive mode.

Open the Textmate bundle editor. Go down to Python -> Run Script (Terminal) and replace this ( $(esc “${TPY}) ):

wpython

With this ( python -i ):

Textmate Python Interactive mode

Now, when you run this, your script will run, and you’ll be left with a Python prompt for playing around and further debugging.

Building Emacs on Leopard: emacs osx make: *** [src] Error 2

So, I’ve been a Texmate guy for awhile. I dig it. In fact, I dig it so much, that sometimes I don’t want to leave, I wish I could just do everything from my trusty text editor. I also wish it could debug better.

This second thing, the debugger, has led me to try things like Komodo and Wingware. But next to the beauty of Textmate, they seem clunky. Instead of focusing on the text, they seem to distract from it.

So, I decided to give Emacs another run. I say “another” because I tried it many years ago, and while I found it interesting, I just never stuck with it. But in those days, I did more design than development. And as such, I like to reach for the mouse as little as possible. With Emacs, you can supposedly set up your environment so that you never have to leave it. You can check email, edit text, log into servers, ftp, etc. I’m very curious.

So, I’m trying it again. But first, I needed to install it on my Intel Mac, running Leopard. But running the make command kept spitting out nasty errors, until I applied the patch I found here.

So, after trying to get the patch right a few times, I got it to work. For other’s convenience, here’s what to do, along with the full file so you don’t have to apply a patch.

  1. Replace emacs-22.x/src/unexmacosx.c with this version of unexmacosx.c
  2. From the emacs-22.x directory, run:
    ./configure –enable-carbon-app
    make
    sudo make install
  3. From there, it should work. You can run ‘emacs’ from the command line, and it should also put a GUI app in your Applications folder.

Cheers!

Getting MySQL-python Running on Leopard

As I pursue my python adventures, I of course wanted to learn how to do one of the things I do most – pull data out of MySQL. It seems the simplest way to start doing that is Python is to use MySQL-Python. So, after a downloading the package from SourceForge, and following the build instructions (tip: keep all this junk in a “src” folder in your user directory), I wound up with this loveliness:

error:
command ‘gcc’ failed with exit status 1

How annoying – I recall seeing this error when installing the Django database bindings (more on that some other time), and it’s uber stupid as gcc, a compiler, is indeed installed on OS X (as running a “gcc -V” in the terminal will tell you). Turns out, a little hacking is needed to get this running, and it’s pretty easy, thanks to Red Elephants’ excellent MySQL-Python and Apple OSX 10.5 (Leopard) article. If that still doesn’t work for you, try this:

  1. Open the site.cfg file
  2. Uncomment line 13, and change it to read:
    mysql_config = /usr/local/mysql/bin/mysql_config
  3. Save it, delete the build directory (rm -r build)
  4. Run: python setup.py clean
  5. Try again, starting from Step 5 of the Red Elephant article

Hope that does it for you. Once you have that up and running, you can dive in with this old, but handy, devshed.com article – MySQL Connectivity With Python (actually, this page seems to have the same info in a more readable format). There is one difference I’ve noted so far – to get the last insert id, you create a “cursor” object (as show in the article), and run (assuming you’re working at the interactive prompt):

>> cursor.lastrowid

If you see an “L” – which just means “long number,” (maybe because it’s an “int” field?) you can get rid of it with a print statement:

>> print cursor.lastrowid

And don’t forget the handy “dir” function, which tell you everything an object can do:

>> dir(cursor)

Also note that MySQL-Python has some handy support for transactions and rollbacks (page 7 of the devshed article). Hope this helps alleviate some headaches ;)

Clean File Names with Automater

Every web developer knows how it goes – now matter how many times you tell your clients not to includes spaces, slashes, etc. in the files they want posted on their website, they do it anyways – it’s part of the deadline-driven work flow. Let’s take a basic look at how to fix this easily and quickly using OS X and Automator.

Automator to the Rescue!

ottoApple’s Automater is a handy, built-in too that allows you to create “workflows” that would normally be time-consuming both to do, and to program a solution for. Need to rename a bunch of files, and copy them to another directory? Change a slew of file extensions? Make thumbnails out of a bunch of images? Encrypt your PDFs? Sure, you could write some Python/Ruby/Shell script to handle this. But, just about any tedious file-related task you can think of can be handled by Automator, and better yet, easily created using a friendly drag n’ drop interface. For more details, MacDevCenter has a great introduction on this. But generally, Automator is so easy to use, you can just figure it out as you go.

Along these lines, I was originally going to demonstrate my own “clean file name” action and how to create it, but after checking Apple’s website, somebody already created a nifty looking one called Make Names Web-Friendly – and it looks really cool. Download it, run the installer, and you’re ready to go. Let’s test it out on the following file name:

Some - stupid / file & name.txt

The easiest way to access our new tool is to right-click the file, and select:
More -> Automator -> Make Names Web-Friendly:

right-click to access the action

Next we are presented with a dialog box, asking how to handle various symbols:

web-friendly in action

I changed the “Convert spaces to” option from underscores to dashes (since the file name used a dash already), and clicked Continue. Viola! Our crummy filename is now nice and clean, and will not break any URL it’s used in:

some-stupid-file-and-name.txt

But Wait! There’s More!

There’s a whole slew of free and wonderful Automator Actions on the Apple website. So before you try to build your own, best wander over there and see if somebody already did it for you – I learned my lesson!