Convert Windows Line endings to Unix / OS X

After reading a Windows user’s nightmare of having to convert hundreds of files, I thought I’d post this solution. If you need to convert Windows line endings to Mac / Unix ones, you need to execute this command in Terminal. (This solution doesn’t add extra line-endings that some do)


$ cd /directory/with/your/files
$ find . -type f -exec perl -pi -e 's/\r\n?/\n/g' {} \;

The above works recursively. If you are a bit afraid of going recursive this is better:


$ cd /directory/with/your/files
$ perl -pi -e 's/\r\n?/\n/g' *

This above may through up some errors if it tries to convert directories, so you can be more specific if the file names have a pattern like : a.txt ; b.txt; c.txt; etc.


$ cd /directory/with/your/files
$ perl -pi -e 's/\r\n?/\n/g' *.txt

A good point to note that a command - period will interrupt a command if you go wrong. This is definitely one to put in your .profile file:


alias fle="perl -pi -e 's/\r\n?/\n/g'"
alias fler="find . -type f -exec perl -pi -e 's/\r\n?/\n/g' {} \;"

Then you can just do

$ fle *
$ fler

[Fle = fix line endings ]

Digg!

Remove the iTunes DRM with Roxio Toast Titanium

Apple allows you to burn a particular playlist to a CD a maximum of 7 times. You can use this method to remove the DRM and so play music that you yourself purchased on a player of your choice. However, if you don’t want to ‘waste’ a CD in the process, and you still own Roxio Toast 6.0, you can do this instead:
1. Selecting the Audio Tab
2. Adding your songs
3. From the File Menu, choose “Save as Disk Image…”
4. Wait for the disk image to be created

Finally, you need to mount the disk image, but here’s the weird part. If in Toast you go to Utilities > Mount Disk Image, the disk image that you just created is not selectable! But, if you change the file extension to .dmg, it will be, thus:

5. In the Finder, change the diskimage to have the .dmg extension.
6. In Toast, go to Utilities > Mount Disk Image, and mount the disk image.
7. It should now appear in iTunes and you can import the CD at an encoding of your choice (recommend at least, 192kbps VBR as noted here)

Windows users can probably do much the same with an earlier version of Nero. This works with Toast 6.0.1, but not 7 and not perhaps with later versions of Toast 6. This should be legal as you are doing exactly what you can with iTunes anyway, but if you do more than 7 times you may be breaking the license agreement. I’m no lawyer because that line in Shakespeare scared me shitless as a kid, and I decided to become an astronaut instead.

Set up multiple Apache virtual hosts in OS X

When you are developing websites in OS X, you’ll often want to be able to have a unique test domain for site. For example, my testing domain for this site on localhost is
madross.lh [the lh for localhost, use anything you like]
For j-cafe.com , I use j-cafe.lh

To do do this is fairly straightforward. You just need to make sure the ‘dummy names’ are added to the hosts file in /etc


127.0.0.1 j-cafe.lh
127.0.0.1 madross.lh

Then in your Apache config file, have this:


NameVirtualHost 127.0.0.1

<VirtualHost 127.0.0.1>
ServerName madross.lh
### add the other stuff obviously!
</VirtualHost>

<VirtualHost 127.0.0.1>
ServerName j-cafe.lh
### add the other stuff obviously!
</VirtualHost>

Then just restart the server:

$ sudo apachectl graceful

If you are having trouble with host names being cached you might try this:

$ sudo lookupd -flushcache

I use madross.lh for my testing server, and madross.com is obviously the real site. I keep the number of dots the same to prevent cookie problems. However, you may prefer to have your test server and the real server to have the same name. Obviously, this means you are not going to be able to access the real site. But that’s not really a problem. Just comment the names in /etc/hosts when you want to visit the real site (and vice-versa :-) )


# 127.0.0.1 j-cafe.com
# 127.0.0.1 madross.com

And remember to do

$ sudo lookupd -flushcache

(Adding an alias in .profile is helpful for this)

Fundamentally, this should work on Windows, Unix, Linux etc. On Windows, the hosts file is probably somewhere else.

BBCode Spam Plugin

If you get a lot of spam with multiple BBCode links this will stop them. If it finds [/url] anywhere in the text it’ll give the poster a chance to modify the post and repost it. It’s unlikely that [/url] is required by a legitimate poster so it won’t impact your users too much.

All you need to do is download either the zipped file or the gzipped file and pop it into the plugin folder, then activate the plugin.

Miscellaneous Terminal Basics

This is a random list of useful stuff for Terminal beginners. I’ll just add things as they come to mind.

TextWrangler

Rather than trying to faff around in Pico or vi, you can use the excellent and free Textwrangler from Barebones.

Want to edit an invisible file like .profile? Just drag a directory onto TextWrangler in the dock, and you can edit it from there. Alternatively, just type


$ edit foo.txt

and foo.txt will open.

The .profile file

In your home directory, there is an invisible file called .profile . You can edit this file to add shortcuts for common commands and various other customizations. So open up a new terminal window:


$ cd
$ cp .profile .profile.saved
$ edit .profile

We can now edit the file in TextWrangler, and we’ve also created a backup (which we can restore with: mv .profile .profile.broken && mv .profile.saved .profile)

Add these 2 lines to the .profile file and save it:

alias h='history 10'
alias reload="source /Users/YOU/.profile"

[ replace /Users/YOU with the path to your home directory . If you don’t know this, then in terminal type cd followed by pwd and it’ll appear]

Before you can use these changes you have to reload the .profile file by typing:

$ source ~/.profile
$ h

Do that now. But notice that we added that line as an alias, so from now we can just type reload instead.

Now type

$ h

You’ll get the history of the last 10 things you did numbered. You can repeat a previous action by just doing !527 (or whatever the appropriate number is). !! repeats the last command.

Command Path

When you run a command, how does the Terminal (more properly the shell) know where to look to execute it. Basically, there is a variable called PATH which tells the shell where to look? Try

$ which pico

It should output

/usr/bin/pico

which tells the computer to execute pico which is inside /usr/bin

But why /usr/bin? Type

$ echo $PATH

and you should see something like

/usr/bin:/usr/local/bin

which tell the terminal where to look. (Never put stuff in /usr/bin by the way. That’s for the system, whereas /usr/local/bin is stuff that can added by admins for users to use.)

What happens if there is a pico inside /usr/bin and /usr/local/bin? Well, /usr/bin appears before /usr/local/bin so it will execute /usr/bin/pico. If you want to execute /usr/local/bin/pico then you’ll need to type out the whole path.

Now it’s often handy to be able to add executables to your own bin directory, and not have to type out the full path: /Users/YOU/bin/foobarit, but just foobarit instead. To do that add a directory in your home called bin, and add or modify your .profile file so it has a line like this:


PATH=$PATH:.:/Users/YOU/bin

Now when you type in a command, the shell will look in this order:


/usr/bin /usr/local/bin .(the present directory) /Users/YOU/bin

You want your bin to be last to prevent some nasty person dropping an cd (or something) file into your own bin modified to send private information about you. (Imagine typing cd and rather than executing /usr/bin/cd instead a modified ~/bin/cd got executed instead)

Digg!

Speed up Azureus in OS X

This is about as basic as it gets. The BitTorrent client, Azureus, can be a memory hog if you leave it visible. Simply do Command-H whenever you want to switch out of it. I find the CPU dropping from 20% to 3%. Your mileage may vary.

Remove the iTunes DRM

A look at the possible methods of removing the DRM in iTunes, advantages, disadvantages, and their legal implications
Part 1: the ‘Apple’ method and hints to avoid major loss of quality,
Part 2: removing the DRM without loss of quality
Continue reading ‘Remove the iTunes DRM’

Removing the iTunes DRM (part 2)

In part 1, we explained how to remove the iTunes DRM using a method that is OK by Apple, and is 100% legal. This method has disadvantages in that there is a reduction of quality and it’s time consuming. If that doesn’t bother you then that’s probably the way to go.
Continue reading ‘Removing the iTunes DRM (part 2)’

Digg!

How to size text with CSS

For those in a hurry!

After years of trying we can now use pure CSS to let users read text the size they want it. All, it takes is
body {
font-size: 76%; /*sets a standard base of 12pixel*/
}

and from then on use ems like this:

h1{
font-size: 1.34em /*set base header 1 to 16px*/

}

Continue reading ‘How to size text with CSS’

Digg!