Mac OS X Quick Lock Screen

Want to be able to lock your computer with a keyboard shortcut?

Since OS 8 you can put the screens to sleep using Control + Shift + Eject (⌃ + ⇧ + ⏏).

Here is how you can lock your computer from a keyboard shortcut

1. Open up your system preferences and go to Security & Privacy.

2. Check box to require password after sleep and select desired timeout.

I have mine set to require a password after 5 seconds so if my screens go to sleep while I am right there I have time to wake it up again before requiring a password.

3. Use keyboard shortcut to put monitors to sleep.

⌃ + ⇧ + ⏏

Tracing PHP Memory Usage using Xdebug and Mamp on Mac

I recently wanted to see which parts of my app were using the most memory in a PHP script. I’ve been using Xdebug and webgrind on MAMP on my local machine and loving it. Derick Rethans wrote a post a few years ago about how to use Xdebug to profile memory usage and it was fairly easy to get it working.

1. Setup Xdebug on your Mac on MAMP.

2. Set Xdebug to output the needed function traces by adding the following to the php5.3.6.ini file:

xdebug.show_mem_delta = 1
xdebug.trace_format = 1
xdebug.trace_enable_trigger = 1
xdebug.auto_trace = 1
xdebug.trace_output_dir = "/tmp"

How to edit php .ini file in MAMP:

In context screenshot:

3. Restart MAMP

4. Download Derick’s parsing script to parse out memory usage from .xt trace files. I downloaded the script to my home directory to keep it simple.

5. Run PHP script that you want to measure memory usage from and confirm that .xt trace file is output to the /tmp directory. My output file was named

/tmp/trace.2043925204.xt

6. Run Derick’s memory parsing script against output .xt file

~/memory.php /tmp/trace.2043925204.xt memory-own 20

Example output:

parsing...

Done.

Showing the 20 most costly calls sorted by 'memory-own'.

                                 Inclusive        Own
function                 #calls  time     memory  time     memory
-----------------------------------------------------------------
{main}                        1  0.0345  2303008  0.0085  1894608
EpiRoute->addRoute          107  0.0058    97344  0.0034    92208
EpiApi->addRoute             61  0.0014    49304  0.0014    49304
EpiRoute->get                64  0.0050    65280  0.0017     6608
include_once                  8  0.0003     6376  0.0002     6120
Epi::getSetting             108  0.0024     5184  0.0024     5184
dbConnection                  2  0.0007     4760  0.0001     4568
EpiApi->get                  33  0.0062    63112  0.0014     4224
EpiRoute->post               43  0.0037    42736  0.0012     4064
EpiApi->post                 28  0.0058    54088  0.0013     3584
getApi                       61  0.0011     3344  0.0011     3344
mysqli->prepare               2  0.0003     2640  0.0003     2640
require                       4  0.0003     2312  0.0002     2312
session_start                 1  0.0001     2216  0.0001     2216
include                       5  0.0006     3192  0.0003      880
fsockopen                     2  0.0015      880  0.0015      880
func_get_args                 2  0.0000      840  0.0000      840
EpiTemplate->display          1  0.0008     4288  0.0002      776
EpiRoute::getInstance       108  0.0022      528  0.0022      528
EpiRoute->getRoute            1  0.0002      728  0.0001      480

Creating Mac Keyboard Shortcuts

TextMate recently released an update that changed one of my favorite keyboard shortcuts. This event reminded me that Mac OS X has provided a nice way to create your own keyboard shortcut key bindings. To create your own keyboard shortcut for an application, you will need to know the exact name given in the application’s menu.

Example

Creating new keyboard shortcut for changing tab in TextMate:
1. Found action in app menu to assign keyboard shortcut to

2. Open System Preferences

3. View keyboard shortcuts window

4. Click plus button to add new shortcut

5. Click add and celebrate!

Growl Notifications for Kynetx KRL Command Line Tool

KRL Commit Growl Notification
KRL Commit Growl Notification

I have been using the Kynetx KRL command line tool for several weeks now and it has made my development of Kynetx apps much easier. The only problem that I have had as I have been using the command line tool is that once I commit my app I have to wait a few seconds before being able to run the new version in my browser. Until now I have been doing a lot of command + tab switching between windows to check to see if it has finished saving.

I have now created a clean solution that allows me to know when the version has finished being committed to the Kynetx servers and had one unexpected benefit.

I started out by creating a simple bash alias that would pipe the output from the ‘krl commit’ command to a growl notification

# Growl notify after krl commit is done
alias krlc="krl commit | growlnotify -t "KRL" --image /Users/mikegrace/src/kynetx-x.png;"

I quickly realized that this wouldn’t work for me because piping the console to the growl notification means that the commit output wouldn’t be visible on the console.  I need to be able to see on the console what the output was in case there were errors or the latest saved version so I started looking for a better solution and came up with this

# Use growlnotify to alert user of commit status
krl() {
 if [[ $@ == "commit" ]]; then
  command krl commit | tee status.txt | growlnotify -t "KRL" --image /kynetx-x.png;
  cat status.txt;
 else
  command krl $@
 fi;
}

I created a function in my bash profile that runs when I run the krl command. When it sees me using the commit parameter it will do a krl commit and then tee that output to a status.txt file and pipe it to the growl notification. To have the output also show up on the console I cat the status.txt file back to the console. The unforeseen benefit here is that it is now really easy to share error output with others because it can be found in the status.txt file in the app folder.

I also created a bash script, available on my github, that takes care of the installation for you. I created this script purely for fun and I had a blast doing it!

I had a really great time doing all of this and learned a lot. There is a lot of power in being able to manipulate command line tools to make tasks easier.

As Bigweld would say, “See a need, fill a need”