iOS and WP7 Programming Tidbits: Ruby, AppleScript, accessing the clipboard from JavaScript, Stanford

Some of my recent tips you, assuming you're interested in programming mobile devices, may be interested in.


HERE, I've answered a question on whether it's possible to run Ruby (a dynamic, reflective, general-purpose object-oriented programming language that combines syntax inspired by Perl with Smalltalk-like features) scripts under iOS. It is.

Unfortunately, you'll need to jailbreak your phone / tablet, should you want to do this. In Cydia, just search for “Ruby”. Install the package and, after that Ruby becomes available from both MobileTerminal (ruby <scriptname> or directly) and your native iOS apps. For the latter, just use the system() call (more on it HERE) just like in the following: “system("ruby scriptpath");

BTW, if you plan to run Ruby (or anything) from MobileTerminal, make sure you check out THIS for my article on acquiring the current version of MobileTerminal. Unfortunately, the default repositories in Cydia still only have the old (and non-working) version of the app.

Copying the highlighted text in UIWebView to the iOS clipboard

If you know what AppleScript is (it's certainly worth learning as there are a lot of ways it can make your OS X life significantly easier and work faster!), you may also have run into the excellent script HERE). Basically, the script allows you to select some text in a web page, then invoke the script on the selection (preferably via Automator and a hotkey to make the process as quick as possible). If you also incorporate the invocation of BBEdit, a great third-party OS X editor (as is done in the lowermost post by bluedogranch on Feb 18, '08 10:33:41AM), in addition to the selection, also the page URL and the current date will be copied to a new BBEdit document. Then, you will be able to easily save the selection.

This got me thinking. On my iPads, I often collect information from the Web. Up until now, I've done this the following way: first, I copied the section I wanted to save to the clipboard, switched tasks to Notes, pasted the clipboard contents to a new page. Then, switched back to the browser and did the same with the URL so that I don't have to search for the given page again. This all means four task switches and a lot of manual work.

If you frequently save stuff this way, the work involved can become really annoying. No wonder I embarked on discovering how the above-linked AppleScript script can be re-implemented on iOS.

Unfortunately, without jailbreaking, it seems all this impossible.

From JavaScript (run via a scriptlet put into your Favorites list to provide the fastest access), it's, unfortunately, impossible. While for some (but not all!) desktop browsers it's certainly possible (for example, for Internet Explorer, the script that does all this is as follows: “javascript:CopiedTxt=document.selection.createRange();CopiedTxt.execCommand('Copy');”), that's not the case with iOS and its highly secure system, where, for obvious safety reasons, JavaScript, while it does have access to both the current selection and the URL of the current page, has no way of accessing the clipboard.

BTW, with jailbreaking, all this might be possible. I (currently) don't know how it needs to be done on the system level (I'll try to look into the problem) and there's no direct Cydia application for this. The closest equivalent I could find was Action Menu Plus Pack (dedicated article HERE), which has, among other things, a clipboard history list. This may mean reducing the number of required task switches (two instead of four): you copy the selected Web page content and the URL to the clipboard without switching to Notes in between. Only after doing this should you switch to Notes (one task switch) and copy the two items from the clipboard (directly from there and, then, the last-but-one item from the history). However, it still requires work.

Currently, the only way of doing all this is creating a UIWebView-based Web browser yourself and invoking some Javascript code (window.getSelection().toString()) to get the current selection and, then, along with the current URL, copy it to the clipboard (UIPasteboard *pasteboard = [UIPasteboard generalPasteboard]; pasteboard.string = <the new, compound string containing both the URL and the selection>).

I've even created a demo application doing exactly this. The source code can be downloaded HERE. It has a very simple GUI consisting of two buttons (Go / Copy), a textfield to enter the URL and, of course, a UIWebView. The latter two have outlets (only); the former two event handlers (only). The Go button's event handler (in URLAndSelectionCopyDemoViewController.m) is as follows:

NSURLRequest* req = [NSURLRequest requestWithURL:[NSURL URLWithString:[tf text]]];

[web loadRequest:req];

where tf is the outlet of the text field and web is that of UIWebView. As you can see, the code simply loads the Web page whose address is in the text field.

The Copy event handler gets the current(!) URL via Javascript. Not through the text field outlet – don't forget we don't dynamically update the textfield. Should we want to do it, it's possible if we override - (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType and, before returning YES, we set the new URL in the text field to avoid polling (periodic calls to Javascript document.URL to notice when the URL has changed after the users' tapping a link). Then, it inserts a line feed, some hyphens and another line feed, and, after that, the body of the selection:

UIPasteboard *pasteboard = [UIPasteboard generalPasteboard];

NSString* s = [web stringByEvaluatingJavaScriptFromString:@"document.URL"];

s = [s stringByAppendingString:@" :\r----\r"];

s = [s stringByAppendingString:[web stringByEvaluatingJavaScriptFromString:@"window.getSelection().toString()"]];

pasteboard.string = s;

Finally, here's the GUI:

That's all!

I'll ask the developer of iCab Mobile, probably the best third-party Web browser, whether he wants to implement this in his browser.

Customizing UIButtons

I've posted a quick note on where you can get more information on “graded”, customized UIButtons HERE. Let me cite myself:

Probably the best online custom button tutorial I've run into is at It explains how image stretching works, what the “caps” parameters are pretty well and in plain English. (Offline, among other things, I recommend the “Spiffing Up the Button” section (pg. 92) of Beginning iPhone 3 Development by Dave Mark and Jeff LaMarche – if you have the book, of course.) I haven't found the other online tutorials verbose enough – generally, they only present some code, but no image stretching explanation. Before running into the tutorial I've linked to, it took me quite a bit of experimenting to find out how images should be presented.

Some additional example code explaining the four different button states are at, among others, Set a button background image iPhone programmatically.

Stanford University iOS course

While the first series (Winter 2010) of Stanford University's iOS programming course wasn't particularly anything to write home about, the second one (Fall 2010), already led by Paul Hegarty, has already been a very good one. Now, the current, third one, updated to cover iOS5, has started to appear at iTunes University. Highly recommended! I plan to comment on the series in my future articles, explaining what, as an iOS lecturer myself, I would have elaborated on further or would have stated differently.

Windows Phone 7 programming: No second edition of Petzold's classic

I've asked Charles Petzold, the author of, among other things, the free ebook Programming Windows Phone 7 for the publishing date of the second edition of the book, incorporating the (huge) changes in the WP7 programming model (for example, the ability to unite XNA and SilverLight development, which is REALLY a good thing – a decent, dedicated article is HERE) and API's. Unfortunately, it seems there won't be.

Master your iPhone in one minute a day: Sign up here to get our FREE Tip of the Day delivered right to your inbox.

<p>Werner Ruotsalainen is an iOS and Java programming lecturer who is well-versed in programming, hacking, operating systems, and programming languages. Werner tries to generate unique articles on subjects not widely discussed. Some of his articles are highly technical and are intended for other programmers and coders.</p>
<p>Werner also is interested in photography and videography. He is a frequent contributor to not only mobile and computing publications, but also photo and video forums. He loves swimming, skiing, going to the gym, and using his iPads. English is one of several languages he speaks.</p>