Using AppleScript to Automate an iChat Video Chat

I had to write a script to automatically start iChat at login and start a video chat with a specified screenname. I wanted to only start the chat if the user was online and available, and quit iChat on an error or if the chat ended. So here’s the script I have:

using terms from application "iChat"
	tell application "iChat"
		activate
		log in
		delay 5
		set theBuddy to buddy "ScreennameGoesHere" of service "AIM"
		try
			set theStatus to status of theBuddy
		on error errmesg number errno
			set message to display dialog "The user is currently unavailable." buttons {"OK"}
			quit
			return
		end try
		if theStatus is available then
			set theCapabilities to get capabilities of theBuddy
			if (theCapabilities contains multiperson video) then
				send "A user is attempting to contact you." to theBuddy
				delay 2
				tell service "AIM" to make video chat with properties {participants:theBuddy}
				set theChat to result
				delay 30
				try
					set theStatus to the av connection status of theChat
				on error errmesg number errno
					quit
					return
				end try
				repeat while theStatus is not ended
					delay 5
					try
						set theStatus to the av connection status of theChat
					on error errmesg number errno
						quit
						return
					end try
				end repeat
				quit
				return
			else
				set message to display dialog "The user cannot video chat at this time. Please try again later." buttons {"OK"}
				quit
				return
			end if
		else
			set message to display dialog "The user is currently unavailable." buttons {"OK"}
			quit
			return
		end if
	end tell
end using terms from

A couple of gotchas:

  • I tried using video chat instead of multiperson video, but that always returned false. I don’t know why.
  • Once the video chat has ended, you can’t poll its status (hence the try block).

New iPhone Project: uWarranty

So, I created a new iPhone application called uWarranty. It used an unpublished Apple API for warranty status (from selfsolve.apple.com), and so I got this after submitting it:

Thank you for submitting your application to the App Store. Unfortunately, your application, uWarranty, cannot be added to the App Store because it violates section 3.3.7 of the iPhone Developer Program License Agreement:

“Applications may not perform any functions or link to any content or use any robot, spider, site search or other retrieval application or device to scrape, retrieve or index services provided by Apple or its licensors, or to collect, disseminate or use information about users for any unauthorized purpose.”

OK, I get it. That’s fine and is Apple’s prerogative. But now I have this program and all the development time that went into it. So what do I do? I guess the answer is to open-source it, just like AppSales Mobile. Watch this space for more details as I clean up the code and throw it up on a public repository somewhere.

Cocoa Touch Tutorial: Stripping Non-Alphanumeric Characters on Entry in a UITextField

In a previous post, I showed you how to trim non-alphanumeric characters from a string. Here I’ll go more in-depth and show a method that I wrote to restrict text entry in a UITextField to alphanumeric characters. Since I also wanted the characters to be uppercase, I’ll also ensure that only uppercase characters are allowed.

This should all happen in the - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string method of your UITextField’s delegate (which, of course, must implement the UITextFieldDelegate protocol). I’ve implemented it as follows:

- ( BOOL )textField:( UITextField * )textField
shouldChangeCharactersInRange:( NSRange )range
  replacementString:( NSString * )string
{
    /*
     * We only want uppercase letters and numbers in this text field, so if
     * this method is adding something else, we don't want it. But we also
     * want to support copy-and-paste, so it's not always going to be one
     * character added.
     */
    BOOL shouldAllowChange = YES;

The shouldAllowChange variable is set to YES initially because we want to allow this change when possible. The method will test the string to see if it meets criteria for rejection as we move forward.

    NSMutableString *newReplacement =
    [[ NSMutableString alloc ] initWithString:[ string uppercaseString ]];
    
    if ( ! [ string isEqualToString:newReplacement ]) {
        shouldAllowChange = NO;
    }

First, we define newReplacement. It’s an NSMutableString so that if we discover non-alphanumeric characters in it, we can remove them on-the-fly. It also serves as a convenient string against which we can test to see if string is already uppercase.

    NSCharacterSet *desiredCharacters =
    [ NSCharacterSet alphanumericCharacterSet ];
    
    for ( NSUInteger i = 0; i < [ newReplacement length ]; i++ ) {
        unichar currentCharacter = [ newReplacement characterAtIndex:i ];
        
        if ( ! [ desiredCharacters characterIsMember:currentCharacter ]) {
            shouldAllowChange = NO;
            [ newReplacement deleteCharactersInRange:NSMakeRange( i, 1 )];
            i--;
        }
    }

In this section, we define the NSCharacterSet that we want to work with - in this case, the alphanumeric character set. We go through one character by a time and if the current character isn’t alphanumeric, we remove it from the NSMutableString (decrementing i so that we don’t inadvertently skip a character) and set our shouldAllowChange flag accordingly.

    if ( shouldAllowChange ) {
        [ newReplacement release ];
        return YES;
    } else {
        [ textField setText:[[ textField text ]
                             stringByReplacingCharactersInRange:range
                             withString:newReplacement ]];
        [ newReplacement release ];
        return NO;
    }
}

To finish, if shouldAllowChange is still true, we return YES and allow the replacement characters to be added. Otherwise, we return NO, but not before using our replacement replacement string (say that ten times fast) to manually edit the text field’s text. The end result is a text field that will consist only of uppercase letters and numbers.

User Accounts Disabled

To help fight spam, I’ve disabled user account registration on this blog and deleted all subscribers. I apologize if anyone actually used that feature; if RSS isn’t enough for you, e-mail me and I’ll make you an account. Unless you try to sell me Viagra.

Using Apple’s SimplePing on iPhone OS

If you try out of the box to compile Apple’s “SimplePing” code sample on the iPhone OS, you’ll wind up with a lot of errors as some files don’t exist in those SDKs. Specifically, you need these files (you need more than just these files to compile, obviously, but these are the ones that aren’t included):

  • /usr/include/netinet/ip.h
  • /usr/include/netinet/in_systm.h
  • /usr/include/netinet/ip_icmp.h
  • /usr/include/netinet/ip_var.h

So here’s a quick Bash script that links the relevant files to your iPhone OS and iPhone Simulator SDKs:
[sourcecode language=”bash”]for path in /Developer/Platforms/iPhone*/Developer/SDKs/*; do
for file in /usr/include/netinet/ip.h \
/usr/include/netinet/in_systm.h \
/usr/include/netinet/ip_icmp.h \
/usr/include/netinet/ip_var.h; do
if [ ! -f "${path}${file}" ]; then
sudo ln "${file}" "${path}${file}"
fi;
done;
done[/sourcecode]
I’ve spoken to an Apple engineer and confirmed that this is the best way to do it, as well as filed a bug, which I encourage you to do as well if this annoys you.