'Tutorial' Category

Unhide ~/Library in Mac OS X Lion 10.7 [Take Two]

As many people have no doubt noticed by now, Mac OS X 10.7 hides your Library folder by default in the Finder. For power users, developers and other such creatures in regular need of the Library, this means a trip to the Terminal, which seems like a step backwards (to me, at least).

There has been a hack floating around to reenable hidden folders which I detailed here, however it really goes too far, enabling the whole Unix filesystem for view in the Finder.

A more useable alternative uses the ‘chflags’ command, and can be invoked with the following:

$ chflags nohidden ~/Library

DIY Linux Transmission Server

 

One of my favourite things about Linux is its ability to transform a formerly useless elderly computer into something really useful indeed. This is the first in a series of posts covering some great projects to breathe new life into old machines using Linux.

What’s the Project?

BitTorrent is a fantastically useful distribution mechanism – when downloading updated Linux distro ISOs, it regularly exceeds the speeds available through centralised (HTTP/FTP) download repositories, and the sharing of bandwidth and ease of distribution are a great fit for online communities and fan groups (Zappateers, for example). Unfortunately, BitTorrent tends to be a total disk I/O hog, constantly reading and writing tiny chunks of file all over your disk. This causes big slowdowns for all other running programs which need access to your hard disk, not to mention the stress it puts on the drive itself.

We’re going to use an old computer, GNU/Linux and the Transmission BitTorrent client to offload this resource-intensive process to a networked machine, freeing up your main machine to get on with business unfettered. Sacrificing convenience, however, is not an option. Therefore, we’re going to implement the following convenience features:

  • Administering the client through a web browser
  • Sharing content over the local network
  • Creating a zero-setup process for starting new transfers

(more…)

Solving the MBProgressHud _WebTryThreadLock Error

MBProgressHud is a really nice bit of plug in code to add fancy status and loading notifications to your iOS app quickly and easily. It looks great! Unfortunately, there’s an irritating error which cost me some time this morning, and hopefully I can save anyone else in the same position some trouble.

The error:

bool _WebTryThreadLock(bool), 0x7b9b5500: Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now...

This appears when a method which needs to update a UI component is called directly by the MBProgressHUD object.

The most basic usage of MBProgressHUD is thus:

HUD = [[MBProgressHUD alloc] initWithView:self.view];
[self.view addSubview:HUD];
HUD.delegate = self;
[HUD showWhileExecuting:@selector(fetchNewData) onTarget:self withObject:nil animated:YES];

In which ‘fetchNewData’ is the method which is executed while the progress HUD is on display.

Using this technique, fetchNewData will be called on a secondary thread, which causes the crash error we’ve already experienced. UIKit, which handles all the user interface business, should only be running on the main thread, so when the secondary thread makes a move on a particular UI element, its going to throw the ‘web lock from a thread…’ error. (It should be noticed that you can update some UI components using the standard MBProgressHUD setup detailed above, but in most cases you’ll get the error.)

In the case of this example, ‘fetchNewData’ updates part of the UI, so it needs to be called on the main thread. The quick and dirty workaround I used to force it to execute where I wanted it to was to create an intermediary method which can be called by MBProgressHUD as normal but in turn calls ‘fetchNewData’ specifically on the main thread.

You could, for example, call this method ‘performFetchOnMainThread’:

-(void) performFetchOnMainThread    {
[self performSelectorOnMainThread:@selector(fetchNewData) withObject:nil waitUntilDone:YES];
}

Instead of directly calling ‘fetchNewData’ from MBProgressHUD, use it to execute ‘performFetchOnMainThread’, which uses the ‘performSelectorOnMainThread’ method to force ‘fetchNewData’ to be executed on the main thread.

This isn’t the most efficient or beautiful way to accomplish this, but it works, so if you’re getting a ‘web lock from a thread…’ error and you need to make sure your code is executed on the main thread, you can use this technique to get the job done quickly.

PC Keyboards in Mac OS X Lion

One of the benefits of the recent upgrade I made to Mac OS X 10.7 Lion is how consistently simple using a PC keyboard now is. The preference pane for switching the modifier keys around (using alt in place of CMD, Windows key in place of option etc…) was available in Snow Leopard and earlier versions, but compatibility with the range of PC keyboards I’ve tried with my various Macs has long been a bit patchy – even though the changes would save in System Preferences, the keys would still use the default mapping.

If you’re used to CMD being either side of the space bar, it can get pretty annoying when it isn’t there.

In earlier versions of OS X, I’ve used DoubleCommand when there’s been trouble remapping keys, however I’m pleased to report that Lion seems to have addressed some of the earlier issues with the modifier preference pane being ineffective.

I’ve tested the modifiers with a range of keyboards (including ones I’ve experienced issues with in the past) and they all seem to be altering the maps without issue.

To alter the key map:

1. Open System Preferences and select the ‘Keyboard’ preference pane

2. Click the ‘Modifier Keys’ button and switch the mapping. When you’re done, click ‘OK’ and close the preference pane.

Schedule WordPress Backups Using Cron

I was contacted a couple of days back by a reader who wanted to add my super simple WordPress backup program to Cron, the Unix task scheduler, to create automated regular snapshots of his site. Backing up my website using the program is something I normally do by hand as part of my general system maintenance routine, but the program is easily incorporated in to Cron to run on a timer. Completely by coincidence, I suffered a WordPress plugin failure within a couple of days of creating the Cron job, and thanks to my now daily backups I was able to restore from a 10 hour old snapshot with the minimum of fuss. (more…)

Restore Hidden Files to OS X 10.7 Lion Finder

One step Lion takes in the direction of erasing the file system altogether is hiding the Library, System and other OS- and user-crucial directories in the Finder.

Obviously as power users we’re fully used to delving into the (already abstracted) file system of Mac OS X, and we want those folders back! Via this site, there is a Terminal command to restore access to these directories, but be warned that it comes with a caveat: by enabling access to these folders, you open up the whole filesystem including UNIX folders (etc, var…) and DS_Store files for view in the Finder.

In short, it’s way annoying, especially when browsing Linux volumes.

Irritating as having bits of the filesystem whipped away from prying eyes, I think I’m going to stick with Terminal for accessing those folders. TextWrangler can still browse them, and that’s enough for me. For those who want to see everything, and I mean everything:

$ defaults write com.apple.Finder AppleShowAllFiles YES

iPhone App Preview – colorshare

Coming soon to the web and the iPhone, a very quick and very easy way to prototype color schemes and share them online. ‘colorshare’ is currently in testing, but it should soon be available on the iOS App Store as a free download.

Meanwhile, the future home of the application on the web is online already, with features (beyond a placeholder) coming soon.

Super Simple Linux RAM Disk Manager – rdmanage

I’m currently developing a piece of server software which needs to be able to create, modify, serve up and dispose of many temporary files many times every second. Because a great number of I/O operations will be occurring, the program requires very rapid disk access – ideally, it would run on a RAID array or a solid-state disk. Because the server I’m using doesn’t have either, I’m using RAM disks as a substitute.

RAM disks take a portion of your system RAM memory and make it available for storage by mounting it in your file system. Using a RAM disk, you gain a working folder with speed way far in excess of any hard disk or even solid state drive. The usual rules of RAM apply – this is volatile memory, so anything stored on a RAM disk will be erased when the power is cut – but it’s perfect for temporary file storage.

I’ve written a very simple program for Linux called ‘rdmanage’ to help create and manage RAM disks easily at the command line.

Operation:

There are three modes of operation for this program – create a new RAM disk, create a RAM disk from an existing folder, and remove existing RAM disk. As promised, the program is very simple to operate:

To create a RAM Disk:

$ ./rdmanage create [size in mb]

For example, to create a 100MB RAM disk:

$ ./rdmanage create 100

To create RAM Disk from an existing folder:

$ ./rdmanage from /path/to/folder

For example, to create a RAM disk from the folder ‘/home/randymarsh/sim’:

$ ./rdmanage from /home/randymarsh/sim

To remove an existing RAM Disk:

$ ./rdmanage remove

Notes:

– When using the ‘from folder’ mode, the maximum size of the folder is 512MB

– You will need root permissions to mount the RAM drive

– The ‘remove’ mode will commit the state of the RAM drive to disk in a folder called ‘ramdisk.contents’

– All operations will occur within the current working directory (i.e. the folder you’re presently in). To check which folder you’re in, type ‘pwd’ at the command prompt.

– Depending on how you download the program, you might have to set the file as executable:

$ chmod +x rdmanage

– If you want to check the status of your drive, it will be listed when you use the ‘df’ command

– For program help, type:

$ ./rdmanage help

Download

You can download the program by right-clicking here and selecting ‘Save As’/’Save Target As’, or if you would prefer to download straight to your Linux machine from the command line, use wget:

$ wget http://zebpedersen.co.uk/python/rdmanage

Hopefully this program can save you a bit of time creating and managing your RAM disks. Enjoy!

Super Simple WordPress Backup Program

Whatever you do with your computer, it’s absolutely vital to back up your data. Never is this more true than when you’re administering a website, where creating backups should be a well organised and regular procedure.

This site is no exception to that rule, so to help me out with my daily backups I’ve written a small program in the Python programming language which automates the process of archiving daily snapshots of my site. For peace of mind, I still prefer to launch and organise backups myself rather than use completely automatic solutions; this program helps make that process much quicker.

Download

You can download the program by right-clicking here and pressing ‘Save As’/’Save Target As’.

If you want to download straight to your Linux machine from the command line, use wget:

$ wget http://zebpedersen.co.uk/python/wordpressbackup

The program is designed to run under Linux, but theoretically it will work on any Unix system (Mac OS X, Solaris etc…) with Python installed.

Operation

All you need to feed the program is the path to your WordPress installation (the directory containing your wp-****.php files). The database login credentials are borrowed from WordPress, and the backup is placed in a date-stamped folder for easy archiving.

Each backup contains a dump of your MySQL database and a tarball of your installation directory – everything you need to restore your site in case of a disaster. The backup will be created in the folder from which you start the program.

To launch the program:

$ ./wordpressbackup /path/to/wordpress/installation

For example, if your site is located in the directory ‘/home/randymarsh/web’, the command would be:

$ ./wordpressbackup /home/randymarsh/web

If you need help or more information, use the help & about functions:

$ ./wordpressbackup help

$ ./wordpressbackup about

Hopefully this little script can help out anyone else like me who only trusts a backup they’ve done by hand. Enjoy!

 

Create VHD Images Using VirtualBox

Yesterday, I wrote a post offering some VHD image files of various sizes for download. However, if you’re a bit more intrepid, you might want to create some of your own.

Despite the fact that (as far as I’m aware) you can’t create VHD image files using the GUI version of Oracle’s VirtualBox software, it is possible to create them using the command line tool ‘VBoxManage’.

Here’s how:

$ VBoxManage createhd --filename 40GB_VHD.vhd --size 40000 --format VHD --variant Standard

This command is fairly self-explanatory. Options as follows:

  • createhd (create a virtual disk image)
  • –filename [filename.vhd]
  • –size [size in MB]
  • –format [specify the new file as a VHD image]
  • –variant [‘Standard’ equates to self-expanding]

VBoxManage and other VBox command line tools are installed when you load the full version of VirtualBox (available here), so there’s nothing extra you need to load on. This command works on PC, Mac and Linux. Enjoy!