Using AppleScript to Automate Data Entry

I was working on an app today and ran into a problem: I had to transfer data from a table in a Microsoft Word document to a dictionary in a dictionary in a dictionary in a property list in Xcode. After copying and pasting several times—I had 243 total entries to copy—I figured there had to be a better way. So, I fired up AppleScript Editor and wrote this quick script to do nine at a time:

repeat 9 times
	tell application "Xcode"
		activate
	end tell
	
	tell application "System Events"
		keystroke tab
		keystroke tab
	end tell
	
	tell application "Pages"
		activate
	end tell
	
	tell application "System Events"
		keystroke "c" using {command down}
		delay 0.5
		keystroke tab
	end tell
	
	tell application "Xcode"
		activate
	end tell
	
	tell application "System Events"
		keystroke "v" using {command down}
	end tell
end repeat

Nothing fancy, but it worked, and I was saved from carpal tunnel syndrome. So if you find yourself needing to do something tedious, repetitive, and (most of all) easily reproduced, you too can turn to AppleScript to get it done. I won’t spend too much time on explaining the code, but just know that you have to enable access for assistive devices in System Preferences before doing it.

Dealing with Special Characters in iPhone 4 Graphics Filenames with Subversion

With the iPhone 4’s high-resolution screen, designers need to create two sets of art; the guidelines are to name the files like so: SomeCoolImage.png and SomeCoolImage@2x.png. Unfortunately, if you try to add these files to an SVN repository, the @ symbol throws them off:

$ svn add Icon\@2x~iphone.png 
svn: warning: 'Icon' not found

The fix, thanks to the subversion_users Google Group, is to add another @ to the end of the filename, like so:

$ svn add ./Icon\@2x~iphone.png@ 
A  (bin)  Icon@2x~iphone.png

If you’d like to do this for all of your high-resolution art in a folder, here’s a tiny Bash command for the task:

for x in `ls *\@*`; do svn add $x\@; done

Cocoa Tutorial: Strip Non-Alphanumeric Characters from an NSString

Let’s say you have an NSString that contains both alphanumeric and non-alphanumeric characters and you want to strip the non-alphanumeric characters out of it. The hard way is to manually go through, character-by-character, and put the character in a new string if it matches certain criteria. But why do it the hard way?

Apple provides a class that we can use for this to great effect: NSCharacterSet. We want alphanumeric characters, so we can create a character set of the characters we want using this method:

NSCharacterSet *alphanumericSet = [ NSCharacterSet alphanumericCharacterSet ];

Now we have a character set like we want. We just need a way to turn our string into a string that contains only those characters. Unfortunately, the closest thing in NSString’s implementation is the -stringByTrimmingCharactersInSet: method. But that seems to do the opposite of what we want. Fortunately NSCharacterSet has our back here. We can use the -invertedSet method. So here is our final code:

NSString *beginningString = @"Some string with non-alphanumeric characters. !@#$%^&*()";
NSCharacterSet *nonalphanumericSet = [[ NSCharacterSet alphanumericCharacterSet ] invertedSet ];
NSString *endingString = [ beginningString stringByTrimmingCharactersInSet:nonalphanumericSet ];

In this example, endingString will be equal to “Somestringwithnonalphanumericcharacters”.

UPDATE: As it turns out, this only works if the non-alphanumeric characters are at the beginning or end of the NSString. Whoops.

Resize Your Windows Automatically for Different Resolutions

I use my MacBook Pro in a cheap nba jerseys few different scenarios: by itself, plugged in to a 21” Apple Cinema Display, or plugged in to a 24” Majnu Dell 2405FPW.  I’m also rather OCD; I prefer my Firefox/Safari, Mail.app, and Vienna windows to be centered, stretch from the menu bar to the top of my Dock, and be a certain width.  I created a small AppleScript to auto-detect my resolution and size the windows accordingly:

tell application "Finder" set screen_resolution to bounds of window of desktop set screen_width to item 3 of screen_resolution set screen_height to item 4 of screen_resolution end tell tell application "System Events" to tell process "Dock" set dock_dimensions to size in list 1 set dock_height to item 2 of dock_dimensions end tell set desired_width to 1400 set side_space to screen_width - desired_width set left_bound to (side_space / 2) set right_bound to left_bound + desired_width set bottom_bound to screen_height - dock_height set top_bound to 22 (* for the menu bar *) try tell application "iTunes" activate set the bounds of the first window to {left_bound, top_bound, right_bound, bottom_bound} end tell end try try tell application "Firefox" activate set the bounds of the first window to {left_bound, top_bound, right_bound, bottom_bound} end tell end try try tell application "Mail" activate set the bounds of the first window to {left_bound, top_bound, right_bound, bottom_bound} end tell end try try tell application "Vienna" activate set the bounds of the first window to {left_bound, top_bound, right_bound, bottom_bound} end tell end try

With that in place, I saved it as an application in ~/Applications, and put it in my Dock. cheap nfl jerseys Now, отличаются whenever I change resolutions, I just click the button and everything is how I like it.

To change the script, you should be able to add any application cheap jerseys with an AppleScript dictionary that supports moving wholesale jerseys China and sizing the window.  The numbers I’ve used make the windows 1,400px wide, and the height that you want will depend on the size of your Dock. The script moves windows to the center, desired_width wide, and from the menubar to the Dock.

Note: I have had some trouble recently; sometimes when I change my resolution the AppleScript doesn’t pick it up.  To wholesale jerseys combat test this, I told the Displays System Preferences pane to keep its icon in the menu bar; Lampertheim when my script uses the incorrect resolution, I change my screen resolution then change it back, which is enough for the script to detect the change.

Update 2008-05-28: Made some usability changes. Details here.