having fun on the network

Itunes has a shared library feature but does not show you who and what is it sharing to.So lets start terminal and type :

lsof |grep iTunes |grep TCP

You can also find out what they are listening to by doing

lsof | grep mp3

Now how about for a little fun , say your colleague is listening from a windows machine

nmblookup -A

gives you their netbios name , next on use that name with :

echo “Ejoying songname John ?” | smbclient -M THENAMEHERE

shows a dialog like this smack middle in their windows screen
Note that you can send messages like this to any computer you see in finder’s SHARED sidebar , just don’t abuse it , it will stop being funny.

deleting junk and cache

For every folder you open in Finder it saves a .DS_Store file in that folder with the window settings , here is a easy way to get rid of them (texts in bold are shell commands to be put into Terminal ) the command recursively traverses all the directories , you can replace all the path with / for a system wide cleanup

find /Volumes/myvolume/mydir -name “.DS_Store” -print0 | xargs -0 rm -v

Like all operating systems there are some caches on osx that you might sometimes want to get rid off , to do so :
rm -rfv ~/Library/Caches/
removes all caches for the logged in user
sudo rm -rfv /System/Library/Caches/ /Library/Caches/
removes some system wide caches
sudo rm -rfv /private/var/folders/
removes Leopard specific cache folders
The osx kernel is a modular one meaning extensions are loaded into it as needed , to speed things up especially at boot caches are made that from time to time you might want to clear
sudo rm /System/Library/Extensions.kextcache /System/Library/Extensions.mkext /System/Library/Extensions/Caches/com.apple.kext.info

dual boot , one itunes library

Since i started dual booting either Tiger or Leopard one thing that really annoyed me was that i had to deal with 2 itunes libraries , not being able to have the same thing in itunes no matter what i boot into.

Well there is a very elegant and easy fix for that that i am going to detail in this post and it only requires you to have a extra partition besides the ones that you are booting from.
So let’s get to it (texts in bold are shell commands and should be entered via Terminal):

1 – move your itunes library to a partition you do not boot from (i put mine on the Storage partition)

mv ~/music/iTunes /Volumes/IStorage/

2 – boot into each of the os’es and delete ~/music/iTunes if existing and make a symbolic link to the new location in its place

ln -s /Volumes/IStorage/Itunes/ ~/music/iTunes

This works beautifully and it works at some extent for things like Photo Booth too , also tho it might work i do not advise to make the links with Finder , just stick to console.

avi movies in front row and itunes

Everyone has those divx movies , you can play them in quicktime given you have the proper codecs like Perian , but the pesky itunes will not add the files to its library.

What we need to do is basically change metadata of files by adding type “MooV” to them , the simplest way to do it is download this applescript droplet i wrote and just drag the files you want fixed on its icon.

A more involved way to do it that needs devtools (found on the install dvd) installed is by entering the bold text below in Terminal , after you modified the path and extension to suit your needs.

find /path/toyour/movies/directory -name “*.avi” -print0 | xargs -0 /developer/tools/SetFile -t “MooV”

Now that you can play those files with itunes how about having them show and play in Front Row from the confort of your remote ??!! , without even adding them to the itunes library , it is just a matter of making symbolic links in your ~/Movies folder to the directories containing your movies , you can use the shell or just alt+cmd drag them to ~/Movies to make the links.

osx and movies

OSX is nice , and not because it has no shortcomings , but because you can fix them as opposed to windows.This has a lot to do with the power of osx that stems from it having applescript and a posix compliant shell.

Here is how to change metadata of files so you can play divx movies with itunes for example , with terminal and devtools

find /path/toyour/movies/directory -name “*.avi” -print0 | xargs -0 /developer/tools/SetFile -t “MooV”

, with applescript http://forums.ilounge.com/showthread.php?t=214705

Now that you can play those files with itunes how about having them show and play in Front Row from the confort of your remote , without even adding them to the itunes library , it is just a matter of making symbolic links in your ~/Movies folder to the directories containing your movies , you can use the shell or just alt+cmd drag them to ~/Movies to make the links.

If yo do not know by now you can download movies from youtube just by selecting the file in Safari > Activity > Copy > Paste into downloads window.

How about playing every movie format there is including the flash movies you just downloaded from youtube in anything you like , quicktime , itunes , frontrow etc , well then just download http://perian.org/ and never care about formats of movies ever again.

To top it off i have a script for those times when you do want to know what are your movie files encoded with , this is a extremely fast shell script , with basic info , you can get it packaged as a osx app to drag folders to or directly as a cross-platform script.


commands for terminal lovers

There are some things the interface will just not tell you , or you just need a textual representation for , so here we go (texts in bold are shell commands to be put into Terminal)

Apple command , opens a file , just as if you had double-clicked the file’s icon , a wrapper to the os extensions useful in terminal to open a file with the app associated with it.


17:35 up 31 mins, 2 users, load averages: 0.03 0.10 0.07
Standard unix command showing load and time since boot.
uname -a
Darwin leopard 9.5.0 Darwin Kernel Version 9.5.0: Wed Sep 3 11:29:43 PDT 2008; root:xnu-1228.7.58~1/RELEASE_I386 i386
Apple only command showing kernel and os info.
ProductName: Mac OS X
ProductVersion: 10.5.5
BuildVersion: 9F33
Apple only command showing os name version and build number.
SystemUIS 119 valexa 9u IPv4 0x2e700f8 0t0 UDP *:*
Dock 107 valexa cwd DIR 14,3 1326 2 /
Unix command showing list of open files/sockets/connections by running processes
EX lsof -c Dock shows all files open by Dock and sudo lsof -i | grep LISTEN shows all listening server applications running
Virtual Memory Map of process 107 (Dock)
Output report format: 2.2 — 32-bit process
==== Non-writable regions for process 107
__PAGEZERO 00000000-00001000 [ 4K] —/— SM=NUL /System/Library/CoreServices/Dock.app/Contents/MacOS/Dock
Apple command showing virtual memory of processes , used vmmap Dock for the example

18:05:55.034 lstat /Users/valexa/Library/Preferences 0.000053 fseventsd
18:05:55.036 pwrite F=23 B=0x4000 O=0x00659000 0.000024 mds
18:05:55.434 lstat /valexa/Library/Preferences/com.apple.Terminal.plist 0.000069 mdworker
Apple command showing real time information about all file/network accesses going on (needs to be ran as root)

On Perl and data manipulation

It is common belief that Perl is the language of choice for the purpose of data processing and manipulation , and i couldn’t agree more , having been turning to it every time i needed some serious data processing , only to get the best results at each undertaking.

However back then in 99 when looking for a method of automating the calculation of my monthly online time from my dialup provider’s access logs , a task for which ASM just wouldn’t cut it , i had no other choice but to assume this thing about perl and data manipulation as the truth and have a go at the language without knowing for certain.

Today after quite a while and many data manipulation scripts , and quite coincidentally for the purpose of calculating the total downtime of my isp from my server’s access logs , i can not feel but pleased with the power you have with perl and it’s data manipulation drive , so i felt like evoking this wonderful side of perl myself too.

Now as to not make this article a dry reading , and because probably nobody likes staring at a program execution with no progress display for hours on end , or not even minutes , i am going to explain 2 simple progress display trick for your perl scripts.

Roughly the trick consists in using r (carriage return) to write over the same line over and over, while also disabling output buffering where it is the case

The first example below is the simpler one , but memory buffers are sacrificed for this and do not use this example for files bigger than some dozens of megabytes or loading times will be drastic and memory usage intensive , that aside this should not present any other speed decreases given you write your script in a speed conscious manner.

use POSIX;
#disable output buffering
$| = 1;

open (INFILE,”< $infile”) or die “$infile file not found”;
foreach $a (@data){
$proc = floor((($#data – $.) / $#data) * 100);
print “$. more lines to process ($proc% processed)r”;

In this second script we are not buffering the whole file into memory , so loading speeds will be great even tho’ we have to use a function to count the total line number of the file with clines() before starting to process it

use POSIX;
#disable output buffering
$| = 1;

#reading line length
print “Reading $infile….”;
$lines = clines($infile);
print “done ($lines lines)n”;

#processing data
open (INFILE,”< $infile”) or die “$infile file not found”;
$out = 0;
while(<INFILE>) {
$proc = floor(($out / $lines) * 100);
print $lines-$out.” more lines to process ($proc% processed)r”;
sub clines {
my ($filename) = @_;
$lines = 0;
open(FILE, $filename) or die “Can’t open `$filename’: $!”;
while (sysread FILE, $buffer, 4096) {
$lines += ($buffer =~ tr/n//);
close FILE;
return $lines