Archive for the ‘OS X’ Category

Unix shell Hackery

Saturday, February 4th, 2012

I was helping a friend try to set up a git repository of a rather large existing software project.  He loves guis, and the github gui tool was choking on the initial commit/upload.  Here is the series of shell commands I sent him via email to execute.  It’s pretty cool what you can do with unix and how it lets you communicate.

$EXT is the root directory where you have stored the existing code base

# copy everything over to a completely new directory
1. cp -r $EXT ~/th2

# cd into that directory
2. cd ~/th2

#how much space is that directory taking
3. du -h | tail

#remove the git part of that directory
4. rm -rf ~/th2/.git

#how much space is it taking up now
5. du -h | tail

# how many files are in this whole directory tree
6. find . | wc -l

# what are the most common file extensions,  change -n30 until you think it's giving a reasonable report
7.  find . -type f | grep -v git |   sed 's/\(.*\)\([\/.][^\/]*$\)/\2/'  | grep -v "^\/" | sort | uniq -c  | sort -nr | head -n 30

#create a file size by type script
8.
echo "#/bin/bash" >> size_by_extension.bash
echo "size=`find . -type f | grep "$1$" | xargs -L1 ls -lrt  | awk '{sum+=$5} END{print sum}'`" >> size_by_extension.bash
echo 'echo "$size $1"' >> size_by_extension.bash

./chmod +x size_by_extension.bash

# what file types use the most space
9. find . -type f | grep -v git |   sed 's/\(.*\)\([\/.][^\/]*$\)/\2/'  | grep -v "^\/" | sort | uniq | xargs -L1 ./size_by_extension.bash  2> /dev/null  | sort -nr 

There may be more elegant ways to get some of this done, but all in all it is pretty cool. imagine trying to communicate a similar workflow with gui oriented commands.

Security Anomally in CoreGraphics Event Taps

Thursday, September 4th, 2008

When I was playing around with changing modifier keys for OS X I stumbled onto some inconsistencies with permission requirements and abilities for two very similar event types.

The code I used here is modified from http://osxbook.com . You can view the code on github here https://github.com/paddymul/osx_keyboard_play
There are two programs included in the tar ball.  getKeyPressCode and insertKeyPressOnModifier. They have a very similar structure.  I will start by describing getKeyPressCode

GetKeyPressCode has two functions, main and a call back function.  A call is made to CGEventTapCreate , which passes the myCGEventcallBack function in (I’m hazy on my c code,  it must somehow pass in a pointer), and event mask flags for the type of event, in this case kCGEventKeyDown or kCGEventKeyUp.
The callback in getKeyPressCode displays the keycode for any character you press, in any application on the system. If you press the = key, it replaces that with ‘f’ .
To run getKeyPressCode, you need to be superuser (sudo) or have Assistive devices enabled.  Otherwise the program will fail saying, event tap failed to create.  Also notice that when you go to a  systemwide password box (such as you would be prompted for in keychain) , no keyUp or keyDown events are fired, even though the program was run with superuser privileges.

The second program is insertKeyPressOnModifier.  This program has the same form, the difference is that it catches modifier keys — CGEventFlagsChanged.  This program displays the EventFlags for modifier keys (“CAPSLOCK” , “SHIFT”, “CTRL”, “OPTION”, “OPTION/ALT”, “APPLE”) when a modifier key is pressed.  When any modifier key is pressed, the program inserts an “=” or “+” depending on whether or not shift was pressed.  Again this behaviour takes place in any application on the system.

Now the interesting thing is, you don’t need superuser rights or assistive devices enabled to run insertKeyPressOnModifier.  Even more interesting is, inserKeyPressOnModifier still fires events when you are in a system wide password box, it will also insert ‘=’ characters.  This seems like a potential security hole.

I filed a bug report for this a year ago, apple hasn’t responded or fixed the hole.  This I have observed this behavior on a Mac Book Pro running Tiger and Leopard.  I’m not well versed in C, CoreGraphics, OS X internals, or general security measures like this.  There could be a very good explanation for the behavior, to me though, it seems like an inconsistency that could be a hole.  I haven’t seen the password box behavior mentioned anywhere else.

You can look at the header file for CGEvent on your mac here

/Developer/SDKs/MacOSX10.4u.sdk/Developer/Headers/CFMCarbon/CoreGraphics/CGEvent.h

I bought a 30 inch screen

Sunday, June 29th, 2008

I should blog about this now, while the imprint is fresh in my head.

1. I am moving from a laptop to a Samsung 30 inch lcd

2. It is big, huge, the way you use the computer will change.

3. Moving from a laptop, you have to get an external keyboard, get one you like.  I’m typing this on my father’s iMac keyboard right now, it sucks.

4.  Bump up your mouse acceleration,  A mouse is necessary with this size screen, forget a trackpad, and even a mouse is innadequate.

5.  Window management on OS X is severely lacking.  I really want to get stumpwm working.

So far I like it.

It’s an apple thing you wouldn’t understand

Tuesday, March 11th, 2008

my iPhone drops a call, again, the third time in a conversation.

Scott:You need to get another phone, the reception on that is horrible

me: Your just not cool enough to understand the iPhone crackle.

The next iPhone

Monday, July 9th, 2007

I was talking to a friend and we were discussing the next version of the iPhone. We figured it needed to get cheaper for it to get more market penetration. Then it hit me, obviously the next version of the iPhone will be the iPhone shuffle. It will only have one button, that randomly calls people in your phone list. Jobs will introduce it like this, “We have been following trends in phone usage and many people use their phone just to blabber to people and tell them stuff that the other person doesn’t care about. Drunk Dialing is another hot trend. I present the iPhone Shuffle”.

Re mapping the caps lock key in OS X

Wednesday, May 2nd, 2007

I have heard tales for a while of the joy of having your caps lock key remapped to a more useful modifier key. For me the most useful modifier key that I know of is ESC, for vim. Alas, this seems to be very dificult to do in OS X. Here are some of the false starts I made when trying to do this.

.vimrc
in vim you remap keys with the following command
:mmap
this is great and a very useful feature, unfortunately I can’t directly map the caps lock key with mmap.

System Preferences
at least in 10.4, OS X lets you remap the caps lock key to Control, Command, Alt, or Nothing. This is great for most people, but it doesn’t help me make it ESC. It also doesn’t help me map it to ESC for one app (Vim) and nothing for other apps.
You can find this option in
System Preferences > Keyboard & Mouse > Modifier Keys

.inputRC / xmodmap
These are files that BASH (maybe all shells) and X11 read on startup to map keys. I didn’t examine them too much. From what I hear it is possible to remap the Caps Lock key with xmodmap, but I don’t run X11 regularly

Ukelele / .keylayout files
Ukelele is a program for creating key layout files, it comes highly recommended, I wasn’t impressed. I found it easier to hand edit the xml key layout files that OS X uses. You can read about their schema here. I found out that the caps lock key should generated a key code of 55 or 57 depending on whether it’s being turned on or off. On OS X they don’t generate that key code for a keylayout file to manipulate.

Writing A C program to modify keycodes
I ran across this code example OS X Internals and this code. Now I was on to something. I had one program that modifies key codes, and another one that traps the caps lock key (or any other modifier key being pressed. I was able to hack the code to generate a key code of “74″ (unused in the keylayouts I am using) every time the caps lock key is pressed. I was enormously satisfied with this.

Some hope for subversion on the mac

Tuesday, January 30th, 2007

As I alluded to in a previous post, it is possible to integrate subversion into finder with applescript.  In fact someone has already done this SVN scripts for Finder  .  I haven’t yet played with these, but they are open source, so I hope to contribute.

Switching to the mac (keyboard shortcuts)

Tuesday, January 23rd, 2007

This seems to be an obligatory blog post nowadays.  Two months ago I got my first mac.  I have used PCs since I was 7 and it was a tough change, I’m happier for it though.  There are things the mac just gets right, there are other things that will annoy out to no end.  Here are some of the things that I had the hardest time finding.

Cmd = Apple = Cloverleaf logo  = ⌘

Ctrl = ^

Shift = Up Arrow =
Option = alt = ⌥

⌘-Space = open the spotlight search box

this is the fastest way for me to find programs without cluttering up my dock.

⌘ ` = apple-tilde key switch windows in the current app
This was key to be able to navigate os x with the keyboard.

^ Scroll wheel = Zoom screen in

You need to enable this in

System Preferences > Keyboard & Mouse > Mouse > Zoom

This zooms the whole screen in

⌘^⌥8 = show negative image of the screen

This turns all your whites to blacks and vice versa, it makes some text much easier to read.   You need to enable this in

System Prefernces > Universal Access