OS X – Upon my shoulder http://www.uponmyshoulder.com/blog // TODO: insert witty tagline Tue, 20 Jun 2017 20:25:30 +0000 en-US hourly 1 https://wordpress.org/?v=4.9.8 Automatically restart applications on OS X http://www.uponmyshoulder.com/blog/2011/automatically-restart-applications-on-os-x/ http://www.uponmyshoulder.com/blog/2011/automatically-restart-applications-on-os-x/#comments Sat, 07 May 2011 08:53:33 +0000 http://www.uponmyshoulder.com/blog/?p=433 I use GimmeSomeTune to provide hotkeys and some other goodies for iTunes. It works alright, but is veeeery crashy — usually every dozen hours or so on my machine.

How to fix that? Let’s relaunch it as soon as it crashes. Simple!

In a terminal:
for (( ; ; )); do open -W /Applications/Multimedia/GimmeSomeTune.app/; done

open is the bash command to launch applications on OS X. It works with all kinds of files: open somefile.avi will open that file in your default video player, VLC for example. The -W flag tells open to wait until the application exits before returning any value. By putting it all in a for loop, we effectively ensure that bash will launch GimmeSomeTune, wait until it crashes, then relaunch it, and so on.

Edit: this is a bad way of doing things. A better way is described here.

]]>
http://www.uponmyshoulder.com/blog/2011/automatically-restart-applications-on-os-x/feed/ 1
A bit of Terminal-fu http://www.uponmyshoulder.com/blog/2010/a-bit-of-terminal-fu/ http://www.uponmyshoulder.com/blog/2010/a-bit-of-terminal-fu/#respond Thu, 11 Nov 2010 20:59:46 +0000 http://www.uponmyshoulder.com/blog/?p=320 Yesterday, my little brother was typing something on a linux bash and suddenly went back to the beginning of the line with a simple Ctrl-a. It blew my mind.

I don’t know why, but it never occurred to me to look for shortcuts in Terminal (and bash in general), other than Ctrl-c for badly behaving processes. I just cursed myself and frantically typed on the left arrow key each time I typed cd instead of cp.

Turns out bash has plenty of them, and here are a few useful ones I’m really glad to know now:
Beginning of the line: Ctrl+a
End of the line: Ctrl+e
Delete the word under the cursor or before: Ctrl+w
Delete all chars before the cursor: Ctrl+u
Delete all chars after the cursor: Ctrl+k

Apparently these shortcuts work in a lot of text fields, everywhere.

You can also move the cursor word-by-word by typing Esc, then f or b. It’s not really practical though, so you’d be better of remapping the following keys in Terminal Settings > Settings > Keyboard:
control cursor left: \033b (\033 is actually Esc)
control cursor right: \033f

Now you can swiftly move word-by-word with Alt+left arrow and Alt+right arrow.

I guess the lesson here is there’s always to learn from your siblings — especially the geeky ones.

]]>
http://www.uponmyshoulder.com/blog/2010/a-bit-of-terminal-fu/feed/ 0
A keyboard shortcut to change files colours in the Finder? http://www.uponmyshoulder.com/blog/2010/a-keyboard-shortcut-to-change-files-colours-in-the-finder/ http://www.uponmyshoulder.com/blog/2010/a-keyboard-shortcut-to-change-files-colours-in-the-finder/#comments Wed, 19 May 2010 22:30:06 +0000 http://www.uponmyshoulder.com/blog/?p=244 My usual habit (some would call it an intermittent OCD, but meh) of sorting and organizing files and folders to a great extend and my almost as severe relentless longing for optimisation recently met in an existential question: is it possible to colour files in the Finder through a keyboard shortcut?

OS X lets you add colours -or “labels”– to a file by right-click or in the Finder’s File menu. GUI are cool, but get in the way of efficiency once you know your keyboard inside and out.

Well, after some research, it seems that such a shortcut does not exist. You can’t even create a shortcut the usual way, as the “Label” menu point stands for all labels. (I’ll eventually describe the usual method in this blog!)

The simplest way to implement such a feature was to create a set of AppleScripts that colour files that are currently selected, and launch them via the most amazing QuickSilver. Or Alfred. Or even Spotlight.

The AppleScripts look like that:

property file_color : 5
-- replace '5' above with the number for the color you'd like to use:
-- 0=none, 1=orange, 2=red, 3=yellow, 4=blue, 5=purple, 6=green, 7=grey 

tell application "Finder"
	activate
	set items_ to selection
	repeat with item_ in items_
		try
			set label index of item_ to file_color
		on error e
			display dialog e
		end try
	end repeat
end tell

This is the purple one. Therefore, I called it scp_SetColourPurple.scpt and dropped it off somewhere QuickSilver indexes – proceed this way for each colour you want to add.

All it takes now to colour a set of files is to select them in the Finder, type in Ctrl+Space (to summon QuickSilver) and the three letters to your colour of choice – scp to set the coulour to purple, scr to set colour to red, scy to set colour to yellow…

(Full disclosure: the above code was originally found somewhere on the internet, I can’t find where right now. If I find the link again, or if someone can point it to me, I’ll give props to the original author.)

]]>
http://www.uponmyshoulder.com/blog/2010/a-keyboard-shortcut-to-change-files-colours-in-the-finder/feed/ 2
Seitunes 1.0 http://www.uponmyshoulder.com/blog/2010/seitunes-1-0/ http://www.uponmyshoulder.com/blog/2010/seitunes-1-0/#comments Sat, 08 May 2010 21:43:13 +0000 http://www.uponmyshoulder.com/blog/?p=221 Seitunes v1 is here! I think everything I need it to do is in here. I’m not planning on adding new features in the near future – if anything, I’ll concentrate on optimizing performance.

Sei what?

Seitunes is a command-line interface for iTunes.

It allows you to see what’s playing in iTunes and control it (play, pause, set volume, toggle shuffle, select a playlist, set song rating) from your terminal.

Overview of v1.0

I finally decided to split the program in two separate parts.

libseitunes.(c|h) is a C API to access iTunes through Applescript calls. It is also under GPL.

Seitunes.(c|h) is the higher-level part of the application, which is now completely Applescript-free.

New Features

Support for rating (display and modification).

Playlists: Seitunes now displays the playlist currently playing. You can also see all available playlists, and select the one you want to play (with ‘/’).

Bug fixes

A bug caused Seitunes to think iTunes was completely stopped (hence not displaying data about artist and song) if you went to next track whilst playback was paused. iTunes actually reports such a condition as “stopped”, so I added some code to handle this case (it basically checks if there is a “current track”, and if so, displays the data).

GitHub

Seitunes is now on GitHub!

MeWare?

I’ve stumbled upon this very interesting blog post by Eric Sink, classifying software into three big categories: ThemWare (someone writes software, only other people use it), MeWare (someone write software, only he uses it) and UsWare (someone writes software, he uses it and other people do so).

I have currently no idea if anyone else uses Seitunes, or even downloaded it once to see. If you do use it, I’d be delighted to hear from you!

]]>
http://www.uponmyshoulder.com/blog/2010/seitunes-1-0/feed/ 2
Fix ncurses in OS X 10.6.3 http://www.uponmyshoulder.com/blog/2010/fix-ncurses-in-os-x-10-6-3/ http://www.uponmyshoulder.com/blog/2010/fix-ncurses-in-os-x-10-6-3/#comments Mon, 03 May 2010 22:51:20 +0000 http://www.uponmyshoulder.com/blog/?p=216 In response to my blog post about the issue affecting arrows under OS X 10.6.3, Jonathan Groll pointed out that copying the ncurses libraries from an old 10.6.2 install would fix the problems. It works, but manipulating libraries in that way still feels a bit wrong to me.

And that’s without even mentioning the security implications of getting these libraries from “somewhere on the internet” if you don’t have them laying around anymore.

Instead, let’s fix an Open Source system in the way Open Source is supposed to work: grab the sources and recompile!

Grabbing the sources

Apple provides the sources for the insides of OS X on opensource.apple.com. Considering ncurses-31 (the 10.6.3 version) is buggy, we’ll download a tarball of ncurses-27 (the 10.6.2 version) from Apple right there: ncurses-27.tar.gz.

Recompiling

Step 1: compile.

mbp:ncurses-27 florent$ make

Step 2: install.

mbp:ncurses-27 florent$ sudo make install
Password:
TargetConfig: MacOSX
cd /tmp/ncurses/Build && CFLAGS="-arch x86_64 -arch i386 -arch ppc -g -Os -pipe -isysroot /" CCFLAGS="-arch x86_64 -arch i386 -arch ppc -g -Os -pipe " CXXFLAGS="-arch x86_64 -arch i386 -arch ppc -g -Os -pipe " LDFLAGS="-arch x86_64 -arch i386 -arch ppc            "  /tmp/ncurses/Sources/ncurses/configure \
		--prefix=/usr --disable-dependency-tracking --disable-mixed-case \
		--with-shared --without-normal --without-debug --enable-termcap --enable-widec --with-abi-version=5.4 --without-cxx-binding --without-cxx --mandir=/usr/share/man
/bin/sh: line 0: cd: /tmp/ncurses/Build: No such file or directory
make: *** [install] Error 1

Oookay, by default this Makefile tries to install ncurses into /tmp/ncurses and complains because the directory doesn’t exist. Why not. There has to be an option to change that when calling make, but I went for the fastest choice:

mbp:ncurses-27 florent$ mkdir /tmp/ncurses/Build
mbp:ncurses-27 florent$ sudo make install

And wait for the magic to happen. It’s quite long (and verbose) actually, don’t worry if it takes a few minutes.

Once this was finished, I launched sudo update_dyld_shared_cache and ran some tests: no luck, ncurses still behaved badly.

Then I noticed the “make install” output seemed to show ncurses-27 was actually installed (great!)… In /tmp/ncurses (not so great).

Making it (actually) work

I don’t fully understand how OS X deals with these source tarballs. However, I noticed that /tmp/ncurses/Build contains a Makefile. It looks like what’s installed in /tmp/ncurses is merely an intermediary build that you can then install:

mbp:ncurses-27 florent$ cd /tmp/ncurses/Build/
mbp:Build florent$ make
mbp:Build florent$ sudo make install

I then ran sudo update_dyld_shared_cache again. This time, it prompted:

update_dyld_shared_cache[62767] current i386 cache file invalid because /usr/lib/libncurses.5.4.dylib has changed
update_dyld_shared_cache[62767] current x86_64 cache file invalid because /usr/lib/libncurses.5.4.dylib has changed

Which is good, because it shows we actually changed the compiled version of ncurses (libncurses.5.4)

I finally ran the tests again: ncurses now works like a charm!

]]>
http://www.uponmyshoulder.com/blog/2010/fix-ncurses-in-os-x-10-6-3/feed/ 2
Tunneling iTunes through SSH http://www.uponmyshoulder.com/blog/2010/tunneling-itunes-through-ssh/ http://www.uponmyshoulder.com/blog/2010/tunneling-itunes-through-ssh/#comments Tue, 27 Apr 2010 22:47:14 +0000 http://www.uponmyshoulder.com/blog/?p=186 If you want to listen to your music library on the go, you can tunnel iTunes through SSH in a few easy steps.

First of all, you have to enable sharing in iTunes: iTunes > Preferences > Sharing > Share my library on my local network.

This will announce your library on your local network using multicast DNS, provided by Apple’s implementation of zeroconf: Bonjour. The library can then be accessed on port 3689 of the computer mDNS announced.

Hence, to access your local library on a distant network, we will implement two things: ssh tunneling of port 3689 for the data to be transmitted, and mDNS announcement of the library on the new network so iTunes is aware of where to get it.

We will assume:
– local = where me & my laptop are
– remote = where iTunes is (=server).

Tunneling port 3689

We will tunnel port 3689 of the remote computer to a port of your choice on your local computer:

ssh -N florent@myServer -L 3690:localhost:3689 -f

-L is the standard syntax for SSH tunneling. man ssh tells us the three arguments stands for port:host:hostport; as in “send data received at my own port to the hostport of said host“. Here for example, it means: tunnel all data received on my port 3690 to this SSH connection’s localhost (myServer) on its port 3689.

Feel free to change 3690 to the port of your choice, replace florent@myServer by your own login/host combination, and add a -p if you use a non-standard port for SSH (which is a Good Thing(tm)!).

The -f argument means the SSH tunnel will stay in the background. Finally, -N tells SSH not to execute a command on the remote side (i.e. launch a terminal on the server), meaning we will only use this connection to forward data.

Announcing the library

We will use this command to declare the new library:

dns-sd -P iTunesServer _daap._tcp local 3690 localhost.local 127.0.0.1 &

This command sets up a DNS proxy (-P) called iTunesServer, retransmitting DAAP announcements, on the local domain, on port 3690 of host localhost.local (at address 127.0.0.1).

You should get the following output from dns-sd:

 Registering Service iTunesServer._daap._tcp.local host localhost.local port 3690
 0:24:30.140  Got a reply for service iTunesServer._daap._tcp.local.: Name now registered and active
 0:24:30.140  Got a reply for record localhost.local: Name now registered and active

Note that this will only advertise the library on your own computer. If you want other computers on your network to see the library you’re tunneling to, use the following command, where ComputerName is the Bonjour name of your computer (set in System Preferences > Sharing > Computer Name):

dns-sd -P iTunesServer _daap._tcp . 3690 ComputerName.local ComputerName.local &

Annd your library will show up in iTunes!

iTunes shows the remote library
Victory!

Playback can be a bit choppy if your server’s upload or laptop’s download is low, but it usually should be okay – MP3 and AAC yields great results around 192 to 320 kbps, equivalent to an average 25 to 40 kB/s transfer rate.

Note that this method also allows you to use a password (set up in iTunes) to protect your shared libraries.

]]>
http://www.uponmyshoulder.com/blog/2010/tunneling-itunes-through-ssh/feed/ 18
Slim down applications: Trimmit http://www.uponmyshoulder.com/blog/2010/slim-down-applications-trimmit/ http://www.uponmyshoulder.com/blog/2010/slim-down-applications-trimmit/#respond Sat, 17 Apr 2010 22:57:53 +0000 http://www.uponmyshoulder.com/blog/?p=166 Applications on Mac OS typically include a lot of stuff most people won’t use daily, especially translations in foreign languages and builds for different architectures (x86, x86_64, PPC and PPC 64 bits).

A handful of utilities exist to trim down applications. I personally use Trimmit, a free-as-in-beer software that gives excellent results, as long as it’s used carefully.

For example, let’s take the latest version of iTunes (v9.1) under Snow Leopard.

This is the default install:
iTunes - Original size

A simple cleanup of foreign languages and compression of TIFF files shrinks iTunes into this more reasonable app:
iTunes - Cleared languages and tiff
Trimmit already cut iTunes size by nearly two thirds (!), and iTunes works flawlessly.

That’s about the best results I’ve had with Trimmit, because things get ugly if you want to squeeze it too far.

Still want to push things more? Okay, let’s keep only the x86 binaries:
iTunes - cleared architecture

That’s another 17MB trimmed… But this manipulation breaks iTunes code signature. Which will -if you run OS X firewall, like everyone should- trigger a message asking you if you want to allow iTunes to connect to Internet each time you run it.

Still seem a bit big? Well let’s check every option in Trimmit and go for it.
iTunes - Complete cleanup

That’s 100kB more freed, but now iTunes crashes at startup. Seems that “useless files” aren’t that useless after all.

It’s important to keep in mind that Trimmit doesn’t work well with any applications.

You should be particularly careful (read: do not try) with Adobe applications and other applications on the blacklist. But overall, the extra space can be a very good thing, especially on small partitions and/or on SSDs.

]]>
http://www.uponmyshoulder.com/blog/2010/slim-down-applications-trimmit/feed/ 0
Changing an application icon under OS X http://www.uponmyshoulder.com/blog/2010/changing-an-application-icon-under-os-x/ http://www.uponmyshoulder.com/blog/2010/changing-an-application-icon-under-os-x/#comments Sun, 11 Apr 2010 22:17:08 +0000 http://www.uponmyshoulder.com/blog/?p=152 I don’t like the default icon for Preview.app in OS X.

The sight of that child, giving me this creepy grin, sitting here in the Dock while I’m browsing pictures, is just weird.

Icon for preview.app
Seriously!

There are two ways to make him go away.

The hard way

I call this method the hard way because it involves having another .icns file (the filetype for OS X icons), and navigating through folders supposed to be hidden. But it doesn’t matter, you’re a hardcore user!

Right-click Preview.app and click “Show Package Contents”.
Navigate to Contents/Resources.
Replace Preview.icns with the icon of your choice.

The easy way

This method allows you to replace an icon with another application’s icon.

Open the information panel of said other application (Cmd+i in the Finder).
Click on the big icon at top-left (not the one in the menu bar).
It will appear highlighted:
The information panel with an highlighted icon

Enter Cmd+c to copy it.

Open Preview.app’s information panel.
Click on the icon:
Preview.app's information panel before changing the icon
Enter Cmd+v.

Voilà!
Preview.app's information panel after changing the icon

Another advantage of this second method is that it also works for disks, folders, and about any file.

You can find a lot of great icons at InterfaceLift (though it doesn’t seem to be updated very often). The icon I used to replace the child is part of the Aqua Neue set.

]]>
http://www.uponmyshoulder.com/blog/2010/changing-an-application-icon-under-os-x/feed/ 1
Seitunes v0.8 http://www.uponmyshoulder.com/blog/2010/seitunes-v0-8/ http://www.uponmyshoulder.com/blog/2010/seitunes-v0-8/#respond Mon, 05 Apr 2010 00:24:08 +0000 http://www.uponmyshoulder.com/blog/?p=139 Version 0.8 of Seitunes is here!

Apart from finishing implementing the core functions, I focused on code clarity and performance enhancements.

Code

All major Applescript interactions with iTunes now go through separate C functions instead of “oh hey just do it here in main()” as before. These functions act as wrappers to access iTunes, and can be re-used for any other project. I could even put them in a separate file and call it a library :)

Performance

Seitunes used to refresh data (artist, song, volume, shuffle, playlist) all the time, with Applescript, to get data directly from iTunes itself. I reduced the number of interactions and reduced the calls to iTunes.
For example, the “+” key used to tell iTunes to increase the volume, then call Applescript to refresh all data, including the new Volume value. Now it just tell iTunes to increase Volume, computes the new Volume value to display and goes along with it.
I kept refresh mandatory for the iTunes status in case anything goes wrong (playlist ended, etc).
Refresh is also forced when changing songs – obviously we don’t want to wait 3 seconds to see the song we just changed.

Of course it creates a problem when a song ends by itself. If we never refresh automatically, we can’t display up-to-date data.
I solved this by adding a “decay” counter that forces a refresh if the data is more than 3 seconds old.

All of this seem to have a positive effect: Seitunes now feels as snappy and reactive as directly using iTunes.

New features

> Quit iTunes
> Toggle shuffle
> Display current playlist
> Less Applescript errors (yay!)

Actually there shouldn’t be any Applescript error left (and yes that’s a feature!).

]]>
http://www.uponmyshoulder.com/blog/2010/seitunes-v0-8/feed/ 0
OS X 10.6.3 broke ncurses http://www.uponmyshoulder.com/blog/2010/os-x-10-6-3-broke-ncurses/ http://www.uponmyshoulder.com/blog/2010/os-x-10-6-3-broke-ncurses/#comments Wed, 31 Mar 2010 17:21:11 +0000 http://www.uponmyshoulder.com/blog/?p=122 As I was working on my Seitunes project, I noticed something strange: the arrows didn’t quite work any more. Instead of their proper action (up & down to change volume, right & left to change song), they all quit the program and printed -respectively- “OA”, “OB”, “OC” and “OD” on the stdout.

I tried to go back to a working state by progressively deleting new features I was implementing, until I had exactly the same code as the (known working!) 0.5 version, but it was still quitting. gdb told me it wasn’t a crash (“Program exited normally”).

After some testing, I noticed Seitunes worked on my laptop, but not on my MacBook Pro. The only difference between them being that my laptop was still in OS X 10.6.2, while my mbp has upgraded to 10.6.3.

After a bit of digging into curses functions, I started to suspect keypad(WINDOW *, BOOL) to not work properly after the update. keypad() is supposed to dictate whether getch() should interpret an arrow as one non-ASCII value (with the boolean argument set to TRUE) or a set of ASCII values beginning by the escape char, a.k.a. 27 (FALSE). I explicitly call keypad(stdscr, TRUE) in Seitunes, but the FALSE state would perfectly explain the quit-then-print-two-chars behaviour I had was having: I use the escape character to quit Seitunes.

I wrote two very simple pieces of code -one for keypad true, one for keypad false- that plainly outputs the value returned by getch(). They look like:

#include <curses.h>

int main( int argc, char** argv )
{
	int key;
	initscr();
	cbreak();
	noecho();
	nonl();
	intrflush(stdscr, FALSE);
	keypad(stdscr, TRUE);
	printw("getch() should produce only one value when hitting an arrow.\n");
	while ( (key = getch() ) != 'q' ) {
		printw("You entered key %d\n", key);	
	}
	endwin();
	return 0;
}

Code and makefile available here (testCurses.zip) if you want to give it a try.

Under both OS X 10.6.2 and Linux Mint 6 “Felicia” (based on Ubuntu 8.10), these programs behave as they’re supposed to: when keypad is TRUE, an arrow is shown as a single value; when FALSE, an arrow becomes a set of values.

Under OS X 10.6.3, these two programs behave the same way. Both output several values for an arrow.

I filed a bug report to Apple (vintage interface by the way!).

While this bug is present, we’ll have to manually parse the ASCII values for the arrows, which are mapped as follows:

Up	27 79 65	('escape' 'O' 'A')
Down	27 79 66	('escape' 'O' 'B')
Right	27 79 67	('escape' 'O' 'C')
Left	27 79 68	('escape' 'O' 'D')

Edit: these values assume OS X 10.6.3 and keypad(stdscr, TRUE), a.k.a. when the bug is present.

If you want to use keypad(stdscr, FALSE) in 10.6.3, the arrows are mapped as:

Up	27 91 65	('escape' '[' 'A')
Down	27 91 66	('escape' '[' 'B')
Right	27 91 67	('escape' '[' 'C')
Left	27 91 68	('escape' '[' 'D')

Update, March 1st: Apple answered to my bugreport (ID #7812788). They told me it was a known issue (duplicate of bug #7812932) currently being investigated by engineering.

]]>
http://www.uponmyshoulder.com/blog/2010/os-x-10-6-3-broke-ncurses/feed/ 12