Ramblings Of a Madman

Yeah, like i would know a good description…

Posts for Tag: osx

"Call piped shell commands from Objective-C"

If you happen to want to know how to run - say - the following command from inside an Objective-C application:

echo "something something" | multimarkdown

So that the fantastic [MultiMarkdown](http://fletcherpenney.net/multimarkdown/) runs its magic and transforms **something something** into **\something something\**, then you need to do as below:

- (NSString*) convertMD:(NSString *)data {

// Task for echo NSTask *echoTask = [[NSTask alloc] init]; [echoTask setLaunchPath: @"/bin/echo"]; // Tell which command we are running [echoTask setArguments: [NSArray arrayWithObjects:data, nil]];

// Task for multimarkdown NSTask *mdTask = [[NSTask alloc] init]; // Make a new task [mdTask setLaunchPath: @"/usr/local/bin/multimarkdown"]; // Tell which command we are running [mdTask setArguments: [NSArray arrayWithObjects:@"--snippet", nil]];

// Pipes for input and output NSPipe *pipeBetween = [NSPipe pipe]; // Handles getting the result of echo and injecting into md NSPipe *outPipe = [NSPipe pipe]; // Handles getting the result of md

// Rig it together [echoTask setStandardOutput:pipeBetween]; [mdTask setStandardInput:pipeBetween]; [mdTask setStandardOutput:outPipe];

// Execute [echoTask launch]; [mdTask launch];

// And get the result data NSData *result = [[outPipe fileHandleForReading] readDataToEndOfFile];

return [[NSString alloc] initWithData:result encoding:NSUTF8StringEncoding]; }

A couple of notes here:

- What this does is pipe the result of the echoTask (which will run **echo "\"**) as input (standard) to the mdTask, which will run **multimarkdown --snippet**; - The outPipe will simply take the output of the mdTask so we can return what multimarkdown created; - The commands need the full path in order for the tasks to run; - In case you are curious, i'm creating a markdown editor for OSX with realtime preview inline; - If you want to test this particular piece of code as-is, make sure you [install multimarkdown for OSX first](http://fletcherpenney.net/multimarkdown/download/).

Peace

'Error compiling pg Ruby gem on OSX'

If you get this error (or similar) while trying to run **bundle install** on one of your Ruby projects or trying to install the **pg** gem:

Gem::Installer::ExtensionBuildError: ERROR: Failed to build gem native extension.

/Users/void/.rvm/rubies/ruby-2.0.0-p0/bin/ruby extconf.rb --with-iconv-include=/usr/local/opt/libiconv/include --with-iconv-lib=/usr/local/opt/libiconv/lib checking for pg_config... no No pg_config... trying anyway. If building fails, please try again with --with-pg-config=/path/to/pg_config checking for libpq-fe.h... no Can't find the 'libpq-fe.h header *** extconf.rb failed *** Could not create Makefile due to some reason, probably lack of necessary libraries and/or headers. Check the mkmf.log file for more details. You may need configuration options.

The solution is to install the gem like this:

sudo env ARCHFLAGS="-arch i386" gem install pg -- --with-pg-include=/Library/PostgreSQL/8.4/include/ --with-pg-lib=/Library/PostgreSQL/8.4/lib/

**Note:** Replace 8.4 with your installed PostgreSQL version in the above command.

'New keyboard and KeyRemap4MacBook key bindings'

Bought a new keyboard, the DAS Keyboard Professional S. Maybe now i'll finally learn how to code properly :)

Also, took some time to create key bindings for media and brightness controls (for [KeyRemap4MacBook](https://pqrs.org/macosx/keyremap4macbook/)) for it. Feel free to use my private.xml file and modify as you see fit. The script will only modify keys when the external keyboard is connected. Also, you might need to use the app's EventViewer to change the Vendor and Device ID values :)

I tried to get the sleep key to lock my mac, but i think i'll need to tinker with AppleScript to accomplish that one. Just haven't had the time yet.

Here's the script:

DAS 0x24f0

DAS_KEYBOARD 0x0137

KeyCode::VK_OPEN_URL_APP_Lock file:///Applications/QuickLock.app

DAS Media Volume Mute private.das_vol_mute DeviceVendor::DAS, DeviceProduct::DAS_KEYBOARD __KeyToKey__ KeyCode::F1, KeyCode::VK_CONSUMERKEY_VOLUME_MUTE

DAS Media Volume Down private.das_vol_down DeviceVendor::DAS, DeviceProduct::DAS_KEYBOARD __KeyToKey__ KeyCode::F2, KeyCode::VK_CONSUMERKEY_VOLUME_DOWN

DAS Media Volume Up private.das_vol_up DeviceVendor::DAS, DeviceProduct::DAS_KEYBOARD __KeyToKey__ KeyCode::F3, KeyCode::VK_CONSUMERKEY_VOLUME_UP

DAS Media Play/Pause private.das_play_pause DeviceVendor::DAS, DeviceProduct::DAS_KEYBOARD __KeyToKey__ KeyCode::F5, KeyCode::VK_CONSUMERKEY_MUSIC_PLAY

DAS Media Prev private.das_prev DeviceVendor::DAS, DeviceProduct::DAS_KEYBOARD __KeyToKey__ KeyCode::F7, KeyCode::VK_CONSUMERKEY_MUSIC_PREV

DAS Media Next private.das_next DeviceVendor::DAS, DeviceProduct::DAS_KEYBOARD __KeyToKey__ KeyCode::F8, KeyCode::VK_CONSUMERKEY_MUSIC_NEXT

DAS Brightness Down private.das_brightness_down DeviceVendor::DAS, DeviceProduct::DAS_KEYBOARD __KeyToKey__ KeyCode::F9, KeyCode::VK_CONSUMERKEY_BRIGHTNESS_DOWN

DAS Brightness Up private.das_brightness_up DeviceVendor::DAS, DeviceProduct::DAS_KEYBOARD __KeyToKey__ KeyCode::F10, KeyCode::VK_CONSUMERKEY_BRIGHTNESS_UP

Lock private.das_lock __KeyToKey__ KeyCode::F12, KeyCode::VK_OPEN_URL_APP_Lock

'Dashlane - Great alternative to 1Password'

If - like me - you are looking for a solution to manage all your passwords, don't feel like spending 50€ on [1Password](https://agilebits.com), and didn't feel convinced by the [other](http://keepass.info) [alternatives](http://www.keepassx.org), then you will find joy when you learn about [Dashlane](https://www.dashlane.com).

It features identity, wallet, and password management, all encrypted on their servers, with automatic web form filling and strong password generation. And ... it's free!

The catch is that, if you want synchronization across all your devices, you need the [premium service](https://www.dashlane.com/premium) (about 20 dollars per year) which - in my opinion - is not too bad (by the way, you get to try that for 30 days for free). Considering that with - for example - 1Password you need to pay for updates anyways this seems like a better deal.

Of course - as always - [YMMV](http://www.urbandictionary.com/define.php?term=ymmv).

'GPG Tools for OSX Mail on Mavericks workaround'

If you use [GPGMail](https://gpgtools.org) (look it up if you don't know about email privacy ;)), and migrated to OSX Mavericks preview, then you know it does not work out of the box.

Here's how to trick the operating system to make it work:

- Quit Mail; - Download and [install this fix](http://support.gpgtools.org/discussions/everything/10837/r?go=aHR0cHM6Ly90LmNvL0VESlBRUVFUVGw=); - Run this sequence of commands:

$ sudo defaults write /Library/Mail/Bundles/GPGMail.mailbundle/Contents/Info.plist SupportedPluginCompatibilityUUIDs -array-add "$(grep -A 1 "PluginCompatibilityUUID" /Applications/Mail.app/Contents/Info.plist | grep string | cut -f 2 -d ">" | cut -f 1 -d "<")" $ sudo chown -R root:staff /Library/Mail/Bundles/GPGMail.mailbundle $ sudo chmod -R ug+r /Library/Mail/Bundles/GPGMail.mailbundle $ sudo chmod 644 /Library/Mail/Bundles/GPGMail.mailbundle/Contents/Info.plist

Keep in mind that on some shells the first command will not work (i.e. [fish](http://fishshell.com)). If that's the case, first switch to another one (bash,sh,csh,zsh) then run the commands.

Restart Mail and it should work.

'OSX Mavericks "You need a Java 6 runtime to run X" error dialog fix'

Ok, so here's the deal: If you upgrade to [OSX Mavericks](https://www.apple.com/osx/preview/), only had JAVA 7 installed, and try to run anything that requires JAVA you will probably get a nice dialog like this one:

All my attempts to make Mavericks use JAVA 7 instead have failed so i tried the next best thing: Install JAVA 6. Since you can't find JAVA 6 for Mac at the [Oracle](http://java.oracle.com) site, you'll have to install it from here: [Java for OS X 2013-004](http://support.apple.com/kb/DL1572).

After installing you'll end up with this following structure at the "/System/Library/Frameworks/JavaVM.framework/Versions" folder:

drwxr-xr-x 11 root wheel 374 29 Ago 14: lrwxr-xr-x 1 root wheel 10 29 Ago 14:58 1.4@ -> CurrentJDK lrwxr-xr-x 1 root wheel 10 29 Ago 14:58 1.4.2@ -> CurrentJDK lrwxr-xr-x 1 root wheel 10 29 Ago 14:58 1.5@ -> CurrentJDK lrwxr-xr-x 1 root wheel 10 29 Ago 14:58 1.5.0@ -> CurrentJDK lrwxr-xr-x 1 root wheel 10 29 Ago 14:58 1.6@ -> CurrentJDK lrwxr-xr-x 1 root wheel 10 29 Ago 14:58 1.6.0@ -> CurrentJDK drwxr-xr-x 7 root wheel 238 29 Ago 14:58 A/ lrwxr-xr-x 1 root wheel 1 29 Ago 14:58 Current@ -> A lrwxr-xr-x 1 root wheel 59 29 Ago 14:58 CurrentJDK@ -> /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents

So you might want to fix the path to the current JDK, so that you keep using the JDK 7 by default (Eclipse users, you know what i mean ;)):

$ sudo rm CurrentJDK $ sudo ln -s /Library/Java/ $ sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.7.0_25.jdk/Contents CurrentJDK

Another thing you might need to change is the JAVA_HOME environment variable but, since that depends on whatever shell you use, [Google it](https://duckduckgo.com/?q=set+JAVA_HOME+in+bash) ;)

Finally, verify that you are still using JAVA 7 by default:

$ javac -version javac 1.7.0_25 $ java -version java version "1.7.0_25"

Have fun.

'Create online documents with iCloud'

So yeah, Apple just put iWork for iCloud in beta for everyone. You can now create documents (text, spreadsheets, and presentations) online at [http://icloud.com](iCloud) provided you have an Apple ID, that is. Plus, if you happen to have bought iWork for any Apple devices, the docs will sync effortlessly across everywhere. I love Google Drive, but always found the lack of a desktop version a bit of a fail. iWork to the rescue now ;)

OSX Mavericks Developer Preview 4 boot stuck on wheel

After upgrading to the developer preview 4 I found that my mac mini does not boot with the external screen attached on the HDMI port. It starts booting and then the WindowServer crashes (no warning or anything) and the display presents the spinning symbol on the grey background forever and ever.

Just in case you are having the same problem - apparently, and according to this [link](http://forums.macrumors.com/showthread.php?t=1612506&page=5) - you can unplug the external screen and let it boot, connecting after more or less 2 minutes (or whatever time your mac takes to boot). After that it should recognize the screen as usual.

FISH Shell - How to set tmux window/pane title based on current command and directory

Here's a neat trick: Setting the current tmux window/pane title to the current running command. In case there is no command running, show the current working directory. But trim it a little, so we don't end up with huge window titles :)

In your config.fish file add the following functions:

# Set the tmux window title, depending on whether we are running something, or just prompting function fish_title if [ "fish" != $_ ] tmux rename-window "$_ $argv" else tmux_directory_title end end

function tmux_directory_title if [ "$PWD" != "$LPWD" ] set LPWD "$PWD" set INPUT $PWD set SUBSTRING (eval echo $INPUT| awk '{ print substr( $0, length($0) - 19, length($0) ) }') tmux rename-window "..$SUBSTRING" end end

The first one is a special function called by fish on every command execution. I'm using it to see what is being ran ($_ gives you the command in execution). If it's fish itself, then call tmux\_directory\_title to set PWD as the title.

The second function - as explained - sets the title as grabbed from the current working directory (PWD). Except it performs a little AWKing to trim it down to 20 chars max (the last ones).

Enjoy.

OSX - GCC without XCode

Yes, there is a way. Right here: [osx-gcc-installer](https://github.com/kennethreitz/osx-gcc-installer).

You can download a prebuilt binary or (unfortunately requiring XCode) roll out your own based on the most recent OSX update.

Have fun :)