Better Random Password

An even better method of getting a random (ascii) password: $ head /dev/urandom | strings -n 5 | sed 'N;s/$//;s/\n//g;s/\n//g' | sed 'N;s/$//;s/\n//g' | head -n 1 (All on one line, naturally). I had to do the two seds to make it work properly. It would have been nicer if this combined all of the lines, and then I could just trim as many chars as I wanted, but this was tricky. The head -n 1 discards any lines other than the first.

Random Password

I needed a nice random password, something that is fairly strong. Enter /dev/urandom and md5: $ head /dev/urandom | md5 e98afcb4f093bafa0cc5f90f150df8b7 Obviously, that’s not the password I used. Problems with this method: if you don’t write it down, or save it somewhere, you will not be able to get it back. Second, it only uses a small subset of the ascii codeset - 0123456789abcdef. I’ve tried to come up with something that converts this to ascii, but I’m still working on it. You’d also likely want to ignore 8-bit values, as these can be extended characters. If you ever needed to type this in, it’s sometimes hard to do. The big advantage is that this is a totally random method, and you won’t get the same code twice.

MacOS Internet Sharing keeps stopping

I’ve borrowed a laptop from work, and a Wireless Access Point device, so now I can do stuff from my bedroom late at night, like I am this. However, just recently I have come across some sort of strange behaviour. Sometimes, but not all of the time, MacOS’s Internet Sharing feature seems to get turned off. Which means I need to get access to the machine and restart it. Not much fun when I’m all toasty warm in bed, and the Mac is in the cold, cold study. So, I’ve done a little bit of research. I might be able to restart it using: $ sudo /usr/libexec/InternetSharing I’ll try that next time it drops out… but I’d rather have a better fix.

Shell Calc

Whenever I need to do any calculations, I generally drop into python and do them from there. However, I came across a neat tip over on Mac Geekery: When You Need A Calculator. I’ve modified is ever so slightly so it goes into a .profile instead of a .bashrc:

1
2
3
4
    function calc
    {
        awk "BEGIN {print $* ; }"
    }

Sweet.

Getting Real Audio into Podcast

Occasionally, the ABC have items on their programs to which they don’t have the copyright for, and as such they cannot post an MP3 of the program, but will do a Real Audio stream. Meaning you have to use Real Audio to listen to it, and you cannot archive it. Or so it seems. Now, I refuse to use Real Audio: it’s nothing to do with commercial software or anything like that (there is after all a free version), but I just don’t like streaming. I find that on dialup it just doesn’t work well. And, the time I spend at the computer I am generally reading, so listening to anything other than music just doesn’t work well. I much rather listen to Podcasts while commuting. So, how do you get an item into your iTunes Podcast library? You’ll need mplayer. You can find it at the MPlayerOSX Website. Install it, and then add the following to your ~/.profile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
    function realdump
    {
        if test -f $1 ; then
            realdump `cat $1` 
        else
            export temp=${1/#*\//}
            export filename=${temp/.*/.wav}
            ~/bin/mplayer -cache 128 -vc dummy -vo null -ao pcm -aofile $filename $1
        fi
        if test $# -gt 1 ; then
        shift
        realdump $*
        fi
    }

You’ll also need something to encode in either MP3, or AAC format. faac is great for the latter, LAME for the former. I use faac, and have the following in my ~/.profile:

1
2
3
4
    function faac_podcast
    {
        faac -b 64 -w $1
    }

You’ll want something similar if you use LAME. The ABC has a placeholder MP3 file, that is usually just a short blurb saying they don’t have the ability to put the program up as an MP3. Download this, and then go to the website of the program in question. Find the Real Audio stream, and download the .ram file. Open this in a text editor, and use the Terminal to download the stream using: $ realdump rtsp://www.server.net/path/to/file.ra Then use the following command to encode the file: $ faac_podcast filename.wav This may take some time - although on my computer it was a bit faster than the previous step. In iTunes, ask the Finder to reveal the placeholder file (select it, then use ⌘R, or File - Show Song File). Give the replacement file exactly the same name, except keep the right extension, and copy this file to the same location. If they are the same file format, it will overwrite the placeholder, and you are finished. If not, you need to delete the placeholder file, but not the library entry. Moving the file is not enough, you must delete it! Then, to connect the entry to the replacement file, use ⌘I (Get Info…) and choose to select the file. Choose the replacement file. You can use iTunes to import and convert the file, but for me this was much slower. It requires copying the full WAV file to the library (slow, since mine is on a server), and then converting it to MP3/AAC (again, slow, as it needs to get all of the data back from the server, and then send the new stuff there).

realdump: dump RealPlayer stream to file

The following function/script will dump the data from a RealPlayer stream to disk as a WAV file. Put it into your ~/.profile, or make a shell script out of it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
    function realdump
    {
        if test -f $1 ; then
            realdump `cat $1`
        else
            export temp=${1/#*\//}
            export filename=${temp/.*/.wav}
            ~/bin/mplayer -cache 128 -vc dummy -vo null -ao pcm -aofile $filename $1
        fi
        if test $_# -gt 1 ; then
            shift
            realdump $*
        fi
        
    }

It creates a file with the same basename (the filename minus the extension), with an extension of .wav; there is no checking to see if the file exists already, and old file will be overwritten. The argument to realdump can be either the URLs of the streams, or the name of a file with the URLs of the streams in it. It should handle files with multiple entries, so you can concatenate a series of .ram files, and it should download each of them in turn. Ensure they have different filenames, not just different directories!

Packages are Cool!

I really like the way Apple decided to ‘package’ applications, plugins and the like with MacOS X. It actually makes it easy to see how stuff works. Take basic applications, and most of them are just an executable, but it’s easy to add modularity, and replace GUI elements and so forth.

I’ve grown tired of Toast: it seems to fail more than half of the time when burning DVDs, and I know the media is good, because Disk Utility has no problems burning! I intended to make an AppleScript that burned the selected disk image to a DVD/CD, but found that Disk Utility was not scriptable.

But there is a CLI command called hdiutil that can do most of the stuff Disk Utility can (at least with disk images). The command to burn an image to a Recordable Drive is:

hdiutil burn <imagename>

All I need to be able to do is get this to happen as a contextual menu. As far as I can tell, it should be possible to do this from AppleScript, or python, or bash.

Even better, I should be able to automatically create the <Name>.plugin/* directory structure, and what the contents need to look like, as soon as I can find out exactly what needs to be where!

Editing files as super-user

Since one of the security updates, Apple ‘fixed’ open so sudo open wouldn’t run the app as root, but I liked the ability to do this. Instead, one needs to actually run the application (located in ApplicationName.app/Contents/MacOS/ApplicationName) with a sudo. I often need to edit text files, and I use SubEthaEdit. So I put the following lines in my .profile:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
function edit
{
    if test `echo $TERM_PROGRAM` == 'Apple_Terminal' ; then 
      open -a 'SubEthaEdit.app' "$*" &
    else
      pico "$*"
    fi
}

function sedit
{
    if test `echo $TERM_PROGRAM` == 'Apple_Terminal' ; then
      sudo ~/Applications/SubEthaEdit.app/¬
                  Contents/MacOS/SubEthaEdit "$*" &
    else
      sudo pico "$*"
    fi
}

If you don’t mind the prompt not returning until you finish, you can remove the & from the relevant lines. Why the pico lines? (And the test…) Because I sometimes ssh or telnet into my machine, and I like to be able to just type in (s)edit rather than have to remember another command. Other tips I use - change the background colour of SEE to blue for normal use, and another colour, like red/orange for sudo use. That way, you will know when you are editing a file as a superuser. You will need to sedit first to go through the ‘initial use agreement’. You could also use this trick with another editor…like pico…:-)

More on Sharing iTunes Songs

After all of the mucking around with getting an AppleScript to work, and then using python anyway, I discovered a far faster method of adding tracks to iTunes: just drag the folder they all live in, and iTunes will only add the songs that aren’t in the Library yet! Probably well documented, but I didn’t find it! What you may need to do, particularly if multiple people need to be able to change MP3 Tags or artwork, is do the following:

  • Change the owner/group of the files to something that all people can access - I use shared/admin, since all non-admin users are scum, and do not need to be able to change stuff.
  • Change the protection bits of the files to allow owner R+W, group R+W, all R.

Code to do this should be easy: find ~shared/Music -name *.m* -not -perm 664 -exec chmod 664 {}; find ~shared/Music -name *.m* -not -owner shared -exec chown shared:admin {}; But bash interprets the ; itself, and gives the following error: find: -exec: no terminating ";" Bitch. Can’t be bothered figuring out how to use another shell, so knock up a script in python: UpdateShared.py This script also updates the owner/group and permissions for files in ~shared/Movies and ~shared/iPhotoLibrary. Update: the ; must be \; for the bash commands to work.