How To Rename An Xcode Project

# Filed on May 16, 2009 by AnthonyDiSante reply

When you’re using Xcode to develop an iPhone app or a Mac app, you might decide that you need to rename your project.  In many cases this would mean not only renaming the actual executable file that gets produced, but also the names of various source code files, project folders, and the contents of various files within the project.

As far as I can tell, Xcode provides no way to do this.  There are a few settings that seem like they might do part of it, but every time I tried to use one of them within Xcode, it just resulted in errors and my project failing to build.  After many hours and much frustration with Xcode, I decided to try it the Unix way, and it worked.  The solution is a straightforward 3-step process:

1. First I closed Xcode, and made a backup of my project folder.  Then I went into my project folder and renamed every file and folder which contained "OldName" so that it now contained "NewName" instead.  This could be scripted pretty easily but my current project is a small one so I spent the ~5 minutes to manually rename the files and folders.

2. In the project folder, I ran the following command in a terminal, to update the contents of the files in the project:

find . -type f -exec sed -i 's/OldName/NewName/g' "{}" \;

3. I opened the now-renamed project in Xcode and clicked Build -> Clean All Targets.

After that, the project (with the new name) built successfully.

How To Access A VNC Remote Desktop After The Server Reboots

# Filed on Mar 2, 2009 by AnthonyDiSante reply

Ubuntu Linux, and probably other modern versions of Linux, include a built-in VNC server for remote desktop access.  In Ubuntu this is called vino-server and it’s enabled via Main Menu -> System -> Preferences -> Remote Desktop.

But since this runs as the user who’s logged in to Gnome, it only starts after that user is logged in.  So if you’re away from the PC and accessing it remotely, but something happens which causes/requires it to reboot, then when it comes back up, you won’t be able to access the VNC server because the user won’t be logged in.

The solution to this is actually simple as long as you have SSH enabled and you have root access (via "sudo su" for example) to the server.  Just SSH to it using your normal user account.  Then edit the /etc/gdm/gdm.conf file (actually it’s /etc/gdm/gdm.conf-custom on Ubuntu) and add the following lines to the [daemon] section:

AutomaticLoginEnable=true
AutomaticLogin=yourusername

Then either reboot the server by running "sudo shutdown -r now", or just restart gdm by running "sudo killall -HUP gdm".  Once gdm restarts, it will automatically login as the specified user, and your vino-server process will then start, so you can VNC into the system again.  Don’t forget to remove those auto-login lines from your gdm conf file when you’re done.

Count IP Addresses in Access Log File: BASH One-Liner

# Filed on Dec 17, 2008 by AnthonyDiSante 2 replies

Recently my server was nearly overloaded by a web spider that was severely stupid and/or malfunctioning.  It was making multiple requests every second for totally nonsensical URLs.

When I first noticed my server slowing down, I checked my Apache access.log file.  Since encodable.com normally gets about 1000 visitors per day anyway, a visual inspection of the logfile did not make it immediately obvious which IP address was making the most requests.  There are lots of hits from my own IP, for example, but not enough to slow the server down.

One quick way to see which IP addresses are most active is to sort by them:

cat access.log |cut -d ' ' -f 1 |sort

The cut command there simply throws away all the output except for the first field on each line, which is the IP address.  Then we sort them.  We can then scroll up through the terminal window and get a quick-and-dirty visual indication of which IP is most prevalent.

But in my case, I had quite a few IPs with several hundred hits, and that’s not enough to cause a problem.  I needed to see which ones were in the thousands, but scrolling up through the terminal output it’s not especially easy to see the difference between say 500 lines and 1000 lines.

I needed an actual count of the number of times each IP address appeared the access log.  I came up with the following BASH one-liner to do it (split onto multiple lines here only for readability):

FILE=/path/to/access.log;
 for ip in `cat $FILE |cut -d ' ' -f 1 |sort |uniq`;
 do { COUNT=`grep ^$ip $FILE |wc -l`;
 if [[ "$COUNT" -gt "500" ]]; then echo "$COUNT:   $ip";
 fi }; done

First it creates a for-loop based on the output of the uniq command, so each iteration of the loop is for a different unique IP from the log.  It then greps the log for that IP and uses "wc -l" to count the lines in the output.  Finally, if the count is greater than 500, it displays the count and the IP, like so:

6975:   124.115.3.33
5648:   124.115.5.169
1514:   66.219.73.236
1451:   74.204.11.20

As you can see, the stupid spider was coming from the 124.115.* IP range.

UPDATE: even easier: the uniq command has a -c argument that does most of this work automatically.  It counts the occurrences of each unique line.  Then a quick sort -n and a tail shows the big ones.  Also, I tend to use "cut" as above, but one of the Dreamhost guys reminded me that awk may be a little more straightforward:

cat /path/to/access.log |awk '{print $1}' |sort
 |uniq -c |sort -n |tail

Workaround for the "Could not find Library" Problem with the iPhone Remote App

# Filed on Aug 16, 2008 by AnthonyDiSante 5 replies

The Remote app for the iPhone is an amazing thing: it turns your iPhone into a remote control for iTunes on your PC -- which is to say, a remote control for your stereo.  But unlike the infrared remote that came with your stereo, this remote works over wifi, so while you’re out on the porch grilling up some steaks, you can take your iPhone out of your pocket and use it to play some music on the stereo inside.

But I’m having a serious problem with Remote that I can’t find much information about online: Remote connects to iTunes and works fine for a while, but then it suddenly fails to connect to iTunes, and cannot connect again until you go to the computer and close & re-open iTunes.  My computer is a Mac mini PPC G4, but from posts in the Apple forums I know that some Windows users are having this problem too.

I had been running OS X Tiger 10.4.11; I upgraded to Leopard -- first 10.5.1 then 10.5.4 -- but that didn’t fix the problem.  I rebooted the iPhone.  I uninstalled the Remote app and reinstalled it.  I tried another iPhone.  I turned off the OS X firewall.  I made sure OS X wasn’t going to sleep.  I upgraded to iPhone OS 2.0.1.  None of those things fixed the problem.

But I discovered that if I disabled & re-enabled my Mac’s network connection, that did fix the problem.  So I immediately set to work creating a way to make this happen from the iPhone, since having to actually go over to the computer defeats the purpose of having a remote.  The result is a 2-tap solution that lets you reset your Mac’s network connection from your iPhone.  Here’s how.


Note: in the following instructions, replace "username" with your own username.

Step 1 of 6: Start Apache & Enable Your Sites Folder

In your Mac’s system prefs, go to Sharing, then enable Web Sharing.  Then run this command:

sudo cp /etc/httpd/users/username.conf /etc/apache2/users/

Go into your /Users/username/Sites/ folder and create a new subfolder called "cgi-bin".  Then edit the file /etc/apache2/users/username.conf and add the following line to the end of it:

ScriptAlias /~username/cgi-bin/ "/Users/username/Sites/cgi-bin/"

Now disable & re-enable Web Sharing, or run the command "sudo apachectl restart" which does the same thing.


Step 2 of 6: Create & Name Your Network Profiles


In your Mac’s system prefs, go to Network, then name your default network location "Online", and then create a second location called "Offline" which has all the adapters disabled.

And if your Mac doesn’t already have a fixed/static IP address, then give it one (in your "Online" location).  If your automatic/dynamic IP was something like 192.168.1.100, then set its fixed IP to 192.168.1.200.


Step 3 of 6: Create CGI Script


Create the file /Users/username/Sites/cgi-bin/fix_itunes_remote.cgi and put the following into it:

#!/usr/bin/perl
exit unless $ENV{REMOTE_ADDR} =~ /^192\.168\./;
my $sentinel = "/Users/username/Public/Drop Box/fix_itunes_remote";
`touch "$sentinel"`;
`chmod a+rw "$sentinel"`;
print "Content-type: text/html\n\n";
print qq`<html>`;
print qq`<body onload="javascript:window.opener='x';window.close();">`;
print qq`</body></html>\n`;

Then run this command to make that file executable:

chmod a+x /Users/username/Sites/cgi-bin/fix_itunes_remote.cgi

Step 4 of 6: Create Shell Script


Create the file /Users/username/togglenet.sh and put the following into it:

#!/bin/bash
SENTINEL="/Users/username/Public/Drop Box/fix_itunes_remote"
for ((i=1; i<=9; i++))
do
  if [ -e "$SENTINEL" ]
  then
    echo "sentinel exists; fixing itunes remote now."
    rm -rf "$SENTINEL"
    /usr/sbin/scselect Offline
    sleep 5
    /usr/sbin/scselect Online
  else
    sleep 5
  fi
done

Then run this command to make that file executable:

chmod a+x /Users/username/togglenet.sh

Step 5 of 6: Schedule Shell Script To Run Regularly


Run the command "crontab -e" and put the following into it:

*/1 * * * * /Users/username/togglenet.sh

Step 6 of 6: Use Your iPhone to Run the Script


On your iPhone, open Safari and visit this URL to reset your Mac’s network connection:

http://192.168.1.200/~username/cgi-bin/fix_itunes_remote.cgi

...where 192.168.1.200 is the IP address of your Mac.  Note that the page will automatically close itself.

Create a bookmark for this page on your iPhone’s home screen, right next to your existing Remote app, and name the bookmark FixRemote.  That way you can just tap FixRemote and then Remote and you’re in business.

That’s It

Visiting that URL causes the CGI script to create the sentinel file; when the shell script sees the sentinel file, it resets your Mac’s network connection.

The cronjob runs the shell script once per minute, all day every day.  And the shell script sleeps for a few seconds then loops and runs again, so the frequency of checking is actually every few seconds, not just once per minute.  If it finds the sentinel file, it turns off the network and then turns it back on again, by using the scselect command to temporarily switch your network to its "Offline" profile.

How to Protect Yourself from Viruses and Spyware

# Filed on Jun 23, 2008 by AnthonyDiSante reply

The number one best way to protect yourself against viruses, spyware, and other malware is to throw away your Windows PC and buy a Mac.  Malware is virtually unheard-of on Mac computers, partly because there are fewer Macs than Windows PCs, but also because the Mac is built on an operating system where security is a primary concern, as opposed to Windows where security has been an afterthought.  And nowadays you can have a Mac and also run Windows on it, either by dual-booting or via virtualization, so you can still use Windows whenever you need to.

The second best way to protect yourself from malware is to stop using Internet Explorer, and use Mozilla Firefox instead.  That’s because today, a huge percentage of malware infections occur through bugs in Internet Explorer.  Microsoft eventually patches these holes, but inevitably there’s a delay between the discovery of the bug and the distribution of the fix, which means there’s a period of time where you’re exposed.  The safest thing to do is to avoid IE entirely and just use Firefox.

What if you’re already infected with some nasty virus/spyware/malware?  First, if you don’t already have an antivirus program installed, then install one ASAP, such an Norton or McAfee.  Then make sure it’s fully up to date -- it will download new virus signatures daily, because new viruses are released daily.  Next install Windows Defender.  An AV program and Windows Defender will catch and remove a lot of stuff, but some things will slip through.  For those, you can try installing Spybot Search & Destroy, or, another program that I discovered today while disinfecting a family member’s PC, SuperAntiSpyware.

But even with all of those programs installed, it’s still possible to get infected.  Sometimes running a full system scan with each program will fix the problem, but some malware gets so deep into the system that the only solution is to format your hard drive and reinstall Windows from scratch.

Indeed, even if you can successfully remove the malware from your system, you can never be fully certain that part of it isn’t left hiding somewhere, just waiting to come to life at a later date -- or still running, silently & invisibly, and sending your personal information out to some bad guys somewhere on the net.  So if you really want to be safe, you might consider formatting & reinstalling anytime you get a malware infection.  Yes that’s a hassle, but would you really feel safe doing things like online banking or shopping on a system that had been compromised in the past?  I know I wouldn’t.

Upgrade to 64-bit Linux, Get Half a Gig of RAM for Free

# Filed on May 14, 2008 by AnthonyDiSante reply

About 18 months ago I upgraded my workstation from a Pentium III 850 MHz system to an Intel Core 2 Duo (2.4 GHz) system.  In addition to the giant CPU upgrade (triple the MHz and dual-core) I also doubled the RAM from 2 GB to 4 GB.

With 2 GB, my system was swapping quite often, because Firefox and Thunderbird are memory hogs, because I run Firefox with 30+ tabs constantly, and because I always have many, many xterms, gFTPs, and text editors (Adie) running.  With the upgrade to 4 GB of RAM, my system would run swap-free for a few days, which was a beautiful thing.

But inevitably FF and TBird’s memory usage would creep up, and the number of xterms/gFTPs/editors I was running would steadily increase as my system was up for weeks and months on end.  So after the first few days of uptime, the swap would slowly start being utilized again.

Well a couple weeks ago I finally got around to switching my system from the 32-bit version of Ubuntu to the 64-bit version.  I used the release of Hardy Heron as an excuse to wipe the system clean (which had been upgraded from Dapper to Edgy to Feisty to Gutsy) and do a new install using the amd64 ISO image instead of the i386 image.

Because of limitations inherent in running a 32-bit operating system, the maximum addressable RAM is something like 3.3 GB, so even though I’d had 4 GB installed, I could only use 3281 MB of it (as shown by the "free -m" command).  After installing 64-bit Hardy Heron, though, the available RAM jumped to 3887, over 600 MB more!  Between that and the fact that Hardy includes Firefox 3 (beta), which has a smaller memory footprint than Firefox 2, my system is now swap-free all the time.

Happy Half a Million!

# Filed on Mar 30, 2008 by AnthonyDiSante 5 replies

It’s not exactly a birthday, but yesterday, encodable.com hit half a million visitors since we started logging back in 2005 -- with VisitorLog of course.

That’s not half a million "hits" or "page views" but actual unique visitors.

In 2005 we received about 21,000 visitors; in 2006 it was 142,000; and in 2007 we had 259,000 visitors.  Can’t complain about that kind of growth, and thanks to great customers like you, we’re looking forward to the next half a million in the next year or two!

search posts:

home | archives ]