<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>updates @ m.blog &#187; Code</title>
	<atom:link href="http://mroth.info/blog/category/nerd/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://mroth.info/blog</link>
	<description>Infrequent updates from a social technologist.</description>
	<lastBuildDate>Sat, 19 Dec 2009 22:11:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Palm Pre Mojo SDK experiments</title>
		<link>http://mroth.info/blog/2009/07/08/palm-pre-mojo-sdk-experiments/</link>
		<comments>http://mroth.info/blog/2009/07/08/palm-pre-mojo-sdk-experiments/#comments</comments>
		<pubDate>Thu, 09 Jul 2009 05:19:48 +0000</pubDate>
		<dc:creator>mroth</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[Nerd]]></category>

		<guid isPermaLink="false">http://mroth.info/blog/?p=162</guid>
		<description><![CDATA[As a new Pre owner, I was curious to learn to code for the Palm WebOS SDK (Mojo).  Whenever I try to learn a new technology, I try to build a functional test project.  So&#8230; what to build for WebOS?  Since the Pre&#8217;s web browser doesn&#8217;t currently support the W3C Geolocation spec [...]]]></description>
			<content:encoded><![CDATA[<p>As a new Pre owner, I was curious to learn to code for the Palm WebOS SDK (Mojo).  Whenever I try to learn a new technology, I try to build a functional test project.  So&#8230; what to build for WebOS?  Since the Pre&#8217;s web browser doesn&#8217;t currently support the <a href="http://dev.w3.org/geo/api/spec-source.html">W3C Geolocation spec</a> used by <a href="http://blog.flickr.net/en/2009/06/18/nearby-on-your-phone/">Flickr Nearby</a>, I figured a good &#8220;Hello World&#8221; might be an application to get the GPS coordinates from the Pre, and then load the appropriate Flickr page for that location.</p>
<p><a href="http://www.flickr.com/photos/mroth/3621425624/" title="location services call by mroth, on Flickr"><img src="http://farm4.static.flickr.com/3373/3621425624_7d07c8fbd1.jpg" width="500" height="375" alt="location services call" /></a></p>
<p>CAVEAT: This is a trivial hack, which was never designed for a production release.  I&#8217;m writing up this blog post on how it works because (much to my surprise!) some screenshots of my little hack <a href="http://www.prethinking.com/home/2009/7/8/an-early-peek-at-flickr-nearby-app-for-the-palm-pre.html">generated a bit of interest</a>.  So, I&#8217;ll provide some background, which hopefully will be either helpful or interesting to someone.  Also note, that while I&#8217;m a bit of a minor nerd, I&#8217;m not a software engineer by any stretch of the imagination (I primarily do business / strategy / management-y type stuff), AND this was pretty much my first foray in Javascript, so I&#8217;m sure there are horrible things in my code that will make real developers cringe.  Sorry!</p>
<p>To begin, follow  one of the <a href="http://www.youtube.com/watch?v=YXS3SQauwPE">existing tutorials out there</a> for setting up your environment, learning the basic concepts, generating the project and first scene, etc. etc.  I&#8217;m just going to show here what I did from when I actually started coding.</p>
<p><span id="more-162"></span></p>
<p>First, we&#8217;ll need some minor code to push our main &#8220;scene&#8221; to the stage.</p>
<pre class="brush: javascript">
/* in /app/assistants/state-assistant.js */
StageAssistant.prototype.setup = function() {
    this.controller.pushScene('main');
}
</pre>
<p>Let&#8217;s give the scene some content:</p>
<pre class="brush: html">
<!-- in /app/views/main/main-scene.html -->
<div id="main">
<h1><img src='flickr_logo.gif' />(nearby)</h1>
<div id="disclaimer" class="palm-body-text">
		This is a quick hack to enable "nearby" functionality on Flickr Mobile.
		When Palm supports the W3C Geolocation API natively in the web browser
		we won't need this application. :-)
	</div>
<div x-mojo-element="Spinner" id='gpsSpinner' name='gpsSpinner'></div>
<div id="status_zzz" class="palm-body-text"><em>Determining location...</em></div>
</div>
</pre>
<p>Note I&#8217;m not really bothering with too much of the special Mojo chrome here, but that&#8217;s fine, right?  Yay for web standards! Let&#8217;s perhaps add some CSS to prettify stuff:</p>
<pre class="brush: css">
/* in /stylesheets/main.css */
body {
  background: #fff;
}
h1 {
  font-size: 18px;
}
#gpsSpinner {
  float: right;
  margin-right: 10px;
  margin-top: 5px;
}
</pre>
<p>Here&#8217;s what that scene will look like when it&#8217;s first loaded:<br />
<img src="http://img.skitch.com/20090709-fn7pf4bitxp7e5aqptn8uyfsxf.png" /></p>
<p>Now we get to the meat of the application, configuring the &#8220;assistant&#8221; for the scene.  Which will be in <code>/app/assistants/main-assistant.js</code> for those following along at home.</p>
<p>First, in the setup method, I&#8217;m going to initialize the mojo widget for a spinner (mojo widgets are basically chrome that have special properties and methods built in.)  Next, I&#8217;ll fire off a services request to the location manager to get info from the GPS, and bind it to callback handlers.</p>
<pre class="brush: javascript">
MainAssistant.prototype.setup = function() {
/* this function is for setup tasks that have to happen when the scene is first created */

  // setup spinner widget
  this.gpsSpinnerModel = {
       spinning: true
  }
  this.controller.setupWidget('gpsSpinner',
       this.attributes = { spinnerSize: 'small' },
       this.gpsSpinnerModel
  );

  // get location from GPS
  this.controller.serviceRequest('palm://com.palm.location', {
       method:"getCurrentPosition",
       parameters:{},
       onSuccess: this.locationSuccess.bind(this),
       onFailure: this.locationFailure.bind(this)
  });
}
</pre>
<p>The services request is an asynchronous call, so I have to create a function to catch the response when it comes back.  Here I define that method, which reads the response and updates my interface accordingly.</p>
<pre class="brush: javascript">
MainAssistant.prototype.locationSuccess = function(response) {
/* this is the callback method for when we successfully get the location back */

    if (response.errorCode != 0) {
        $('status_zzz').innerHTML = "BONK. Something went wrong! error code: " + response.errorCode;
    } else {
        var gLat = response.latitude;
        var gLong = response.longitude;
        var coords = gLat + "," + gLong;

        $('status_zzz').innerHTML = "Sweet! Your location is: <small>" + coords + "</small>";
        $('gpsSpinner').mojo.stop(); //spinnaz no mo!

        this.handleGo(coords);
    }
}
</pre>
<p>And finally, here&#8217;s my method (separated out for code simplicity) that does something with the final coordinates.  If I was being fancy, I would make some calls to the Flickr API and retrieve information about photos and build an interface, but it just so happens Flickr already has a nice webkit optimized webpage to display that information, so we&#8217;ll just use the browser:</p>
<pre class="brush: javascript">
MainAssistant.prototype.handleGo = function(coords) {
    var url_target = 'http://m.flickr.com/nearby/' + coords;

    /* In the future we could use an embedded webview to render this page,
  	But for now, opening a web page in the browser works fine and dandy! */
    this.controller.serviceRequest('palm://com.palm.applicationManager', {
           method: 'open',
           parameters: {
               id: 'com.palm.app.browser',
               params: { target: url_target }
           }
    });
}
</pre>
<p>(Note, we could have also done this in an embedded webview, but I figured it was more polite to open a &#8220;real&#8221; browser window so the user could use their bookmarks etc.)</p>
<p>Done and done!<br />
<a href="http://www.flickr.com/photos/mroth/3621425804/" title="mobile nearby page by mroth, on Flickr"><img src="http://farm3.static.flickr.com/2460/3621425804_bf94744ae7.jpg" width="500" height="375" alt="mobile nearby page" /></a></p>
<p>I was pretty impressed with how easy it was to get running.  Since this was a &#8220;trivially simple&#8221; example, I didn&#8217;t really get into the guts of what Mojo can do.  There&#8217;s lots of neat stuff with system notifications, background applications, cloud services and other things that if you&#8217;re serious about I&#8217;d suggest checking out something like <a href="http://oreilly.com/catalog/9780596801816/">Mitch Allen&#8217;s book</a>.</p>
<p>For existing Mojo developers looking for something to build an application <em>about</em>, I&#8217;d highly suggest looking into the <a href="http://flickr.com/services/api">Flickr API</a>.  It has options to return results in <a href="http://www.flickr.com/services/api/response.json.html">JSON</a> which is super handy to use in Javascript, and there are plenty of <a href="http://github.com/straup/js-flickr-api/tree/master">utility libraries</a> out there.  (You can find all sorts of stuff from our sexy-smart enginerds on <a href="http://code.flickr.com/blog/">Flickr&#8217;s devblog</a>.)</p>
<p>UPDATE: You can now <a href="http://github.com/mroth/flickr-nearby-webos">get the source for this on github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mroth.info/blog/2009/07/08/palm-pre-mojo-sdk-experiments/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Source Highlighting for WordPress</title>
		<link>http://mroth.info/blog/2004/12/20/syntax-highlighting-in-wordpress/</link>
		<comments>http://mroth.info/blog/2004/12/20/syntax-highlighting-in-wordpress/#comments</comments>
		<pubDate>Tue, 21 Dec 2004 03:46:15 +0000</pubDate>
		<dc:creator>mroth</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">/?p=75</guid>
		<description><![CDATA[I wanted to have a source code highlighting plugin for WordPress, and the best existing one seems to be Amit Gupta&#8217;s iG:Syntax Hiliter.  However, a number of things about that plugin annoyed me&#8230; so I changed them.  Three cheers for open source!

Added SCROLL_BOX and SHOW_LANG style variables to definitions, to make it easier [...]]]></description>
			<content:encoded><![CDATA[<p>I wanted to have a source code highlighting plugin for WordPress, and the best existing one seems to be Amit Gupta&#8217;s <a href="http://blog.igeek.info/still-fresh/2004/11/22/igsyntax-hiliter-2-final/">iG:Syntax Hiliter</a>.  However, a number of things about that plugin annoyed me&#8230; so I changed them.  Three cheers for open source!</p>
<ul>
<li>Added <code>SCROLL_BOX</code> and <code>SHOW_LANG</code> style variables to definitions, to make it easier for people to decide whether they want the language prefix header and/or fancy scroll box options (I didn&#8217;t).</li>
<li>Cut out about 500 lines to redundant code, reducing the size of the program source by about two thirds(!).</li>
<li>Added language support for Apache log files, Bash, C#, Lisp, Objective-C, and VB.NET.  This depends on you installing a more recent version of <a href="http://qbnz.com/highlighter/">GeSHi</a> with all of the language support files (the author of iG:SyntaxHiliter bundles a version without the files for languages he doesnt support). </li>
<li>In addition, the new structure should make it trivial to implement any other language supported in <a href="http://qbnz.com/highlighter/">GeSHi</a> with two lines of code.  I didn&#8217;t bother to add any others myself, since I&#8217;m lazy.  If you look at the source you&#8217;ll be able to figure out how to do it.</li>
</ul>
<p>Not bad for an hours worth of work in a language I&#8217;m not familiar with.<br />
<span id="more-75"></span><br />
You can download a copy here: <code><a href="http://faktory.org/m/software/syntax/syntax_mhr.bz2">syntax_mhr.bz2</a></code> </p>
<p>It&#8217;s not polished, and doesn&#8217;t come with instructions, so you may wish to refer to <a href="http://blog.igeek.info/still-fresh/2004/11/22/igsyntax-hiliter-2-final/">the original version</a> if you need handholding, and then just substitute in the replacement files.  Remember, you <strong>must</strong> install a copy of <a href="http://qbnz.com/highlighter/">GeSHi</a> for this to work.</p>
<p>As per the licensing on the original, this is released under a <a href="http://creativecommons.org/licenses/by-sa/2.0/">Creative Commons Attribution-ShareAlike-2.0</a> license.</p>
<p>This should be considered a &#8220;hack&#8221;.  I may (or may not) clean up the code further and add more features in the future, but I do not plan on making this an official fork of the plugin, nor offering customer support.  Amit is more than welcome to backport these changes into the official plugin under the terms of the license, and I encourage him to do so.</p>
]]></content:encoded>
			<wfw:commentRss>http://mroth.info/blog/2004/12/20/syntax-highlighting-in-wordpress/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Random Grouphug</title>
		<link>http://mroth.info/blog/2004/12/18/random-grouphug/</link>
		<comments>http://mroth.info/blog/2004/12/18/random-grouphug/#comments</comments>
		<pubDate>Sat, 18 Dec 2004 06:56:48 +0000</pubDate>
		<dc:creator>mroth</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/?p=70</guid>
		<description><![CDATA[Need a grouphug?
#!/usr/bin/perl
use LWP::Simple;
$_ = get('http://grouphug.us/random/');
m&#124;
(.*?)
&#124;ism;
$_ = $1;
s/< ([^>]+)>//ig;
s/^s*//;
s/s*$//;
print "$_n";
Warning: reading people&#8217;s confessions is addictive, and occasionally NWS.
]]></description>
			<content:encoded><![CDATA[<p>Need a <a href="http://grouphug.us">grouphug</a>?</p>
<blockquote><p><code>#!/usr/bin/perl<br />
use LWP::Simple;<br />
$_ = get('http://grouphug.us/random/');<br />
m|
<td class="conf-text">(.*?)</td>
<p>|ism;<br />
$_ = $1;<br />
s/< ([^>]+)>//ig;<br />
s/^s*//;<br />
s/s*$//;<br />
print "$_n";</code></p></blockquote>
<p>Warning: reading people&#8217;s confessions is addictive, and occasionally <acronym title="Not Work Safe">NWS</acronym>.</p>
]]></content:encoded>
			<wfw:commentRss>http://mroth.info/blog/2004/12/18/random-grouphug/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>NmapFE Updated</title>
		<link>http://mroth.info/blog/2004/09/06/nmapfe-updated/</link>
		<comments>http://mroth.info/blog/2004/09/06/nmapfe-updated/#comments</comments>
		<pubDate>Mon, 06 Sep 2004 19:08:50 +0000</pubDate>
		<dc:creator>mroth</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/?p=62</guid>
		<description><![CDATA[I managed to dig out the Powerbook this weekend to update NmapFE for OSX.  Pretty much just an update to nmap 3.70 engine, which provides much faster scanning in some cases.
It&#8217;s a bit strange being the sole developer for a project I don&#8217;t even use myself, that runs on an operating system I no [...]]]></description>
			<content:encoded><![CDATA[<p>I managed to dig out the Powerbook this weekend to update <a title="NmapFE for OSX" href="http://faktory.org/m/software/nmap/">NmapFE for OSX</a>.  Pretty much just an update to nmap 3.70 engine, which provides much faster scanning in some cases.</p>
<p>It&#8217;s a bit strange being the sole developer for a project I don&#8217;t even use myself, that runs on an operating system I no longer use myself.  However, quite a few other people do seem to use it, based on the emails I receive, so I try to keep it updated when I can.</p>
]]></content:encoded>
			<wfw:commentRss>http://mroth.info/blog/2004/09/06/nmapfe-updated/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Google Calculator from Command Line</title>
		<link>http://mroth.info/blog/2004/05/20/google-calculator-from-command-line/</link>
		<comments>http://mroth.info/blog/2004/05/20/google-calculator-from-command-line/#comments</comments>
		<pubDate>Thu, 20 May 2004 17:23:07 +0000</pubDate>
		<dc:creator>mroth</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/?p=59</guid>
		<description><![CDATA[I used to frequently use bc or units from the command line to handle any quick calculation needs, but the handiness of google calculator (as documented in many other places) has largely displaced them. 
Who wants to bother to open a web browser for results though? So I made a quick perl hack to grab [...]]]></description>
			<content:encoded><![CDATA[<p>I used to frequently use <code>bc</code> or <code>units</code> from the command line to handle any quick calculation needs, but the handiness of google calculator (as documented in <a href="http://www.google.com/search?q=google%2bcalculator">many other places</a>) has largely displaced them. </p>
<p>Who wants to bother to open a web browser for results though? So I made a quick perl hack to grab it from command line, example usage:</p>
<blockquote><p><code><strong>$ gcalc 2+4/6*8</strong><br />
  2 + ((4 / 6) * 8) = 7.33333333<br />
  <strong>$ gcalc &quot;12 * 12oz. in gallons&quot;</strong><br />
  12 * 12 US fluid ounces = 1.12500 US gallons<br />
  <strong>$ gcalc the speed of light in km/fortnight</strong><br />
  the speed of light = 3.62628957 x 10^(11) kilometers / fortnight<br />
  <strong>$ gcalc the answer to life, the universe, and everything</strong><br />
  the answer to life, the universe, and everything = 42</code></p></blockquote>
<p>Download <a href="http://faktory.org/m/software/gcalc.pl">the perl script (916 bytes)</a>, share and enjoy. I find it handy to have access to this in IRC, so there is also a version as a <a href="http://faktory.org/m/software/xchat/xgooglecalc.pl">xchat plugin</a>.</p>
<p>Yes, I know that you&#8217;re supposed to use the Google XML-API rather than screen-scraping, unfortunately the API does not return any Calculator results, so scraping appears to be the only way for now. </p>
]]></content:encoded>
			<wfw:commentRss>http://mroth.info/blog/2004/05/20/google-calculator-from-command-line/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Renaming Files with RegEx</title>
		<link>http://mroth.info/blog/2003/01/20/renaming-files-with-regex/</link>
		<comments>http://mroth.info/blog/2003/01/20/renaming-files-with-regex/#comments</comments>
		<pubDate>Mon, 20 Jan 2003 21:17:50 +0000</pubDate>
		<dc:creator>mroth</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/?p=38</guid>
		<description><![CDATA[I finally found a good command-line solution for batch renaming files, and would always end up writing some damn overly complex tcsh foreach loop.  This short perl script basically just wraps Perl pattern matching into a file renamer shell script, but its quick and handy for those familiar with that type of syntax.
[kaffe:~/screenshots] matthew% [...]]]></description>
			<content:encoded><![CDATA[<p>I finally found a good command-line solution for batch renaming files, and would always end up writing some damn overly complex tcsh foreach loop.  <a href="http://www.evolt.org/article/Renaming_Files_with_Perl/17/351/">This short perl script</a> basically just wraps Perl pattern matching into a file renamer shell script, but its quick and handy for those familiar with that type of syntax.</p>
<blockquote><p><code><b>[kaffe:~/screenshots] matthew%</b> ls<br />
Explorer-001.png  Explorer-003.png  Explorer-005.png<br />
Explorer-002.png  Explorer-004.png  Explorer-006.png<br />
<b>[kaffe:~/screenshots] matthew%</b> regexrename 's/Explorer-00(.)/AnnotateDemo_1/' *.png<br />
<b>[kaffe:~/screenshots] matthew%</b> ls<br />
AnnotateDemo_1.png  AnnotateDemo_3.png  AnnotateDemo_5.png<br />
AnnotateDemo_2.png  AnnotateDemo_4.png  AnnotateDemo_6.png</code></p></blockquote>
<p>(For those of you that are afraid of the command line, <a href="http://www.versiontracker.com/moreinfo.fcgi?id=16588&#038;db=mac">this GUI program</a> for OSX seems to have no shortage of good reviews.)</p>
]]></content:encoded>
			<wfw:commentRss>http://mroth.info/blog/2003/01/20/renaming-files-with-regex/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>New Software: CommentFinder</title>
		<link>http://mroth.info/blog/2002/10/07/new-software-commentfinder/</link>
		<comments>http://mroth.info/blog/2002/10/07/new-software-commentfinder/#comments</comments>
		<pubDate>Tue, 08 Oct 2002 02:19:55 +0000</pubDate>
		<dc:creator>mroth</dc:creator>
				<category><![CDATA[Code]]></category>

		<guid isPermaLink="false">/?p=29</guid>
		<description><![CDATA[Readers of my blog get to be the first to play with a new software hack I wrote, CommentFinder.
CommentFinder will take a website and reveal with a visible graphic the inline comments the author left in the code, much as if it would look if you were editing the page in Dreamweaver. If you mouseover [...]]]></description>
			<content:encoded><![CDATA[<p>Readers of my blog get to be the first to play with a new software hack I wrote, <a href="http://faktory.org/m/software/commentfinder/">CommentFinder</a>.</p>
<blockquote><p><tt><em>CommentFinder</em> will take a website and reveal with a visible graphic the inline comments the author left in the code, much as if it would look if you were editing the page in Dreamweaver. If you mouseover the icon, you'll get a tooltip telling you what the text of the comment was. You can find all sorts of interesting things this way (people tend to leave themselves little notes<!-- Like this one!-->) , without the hassle of browsing source code.</></tt></p></blockquote>
<p><a href="http://faktory.org/m/software/commentfinder/">check it out</a> and let me know what you think by posting a comment here.</p>
]]></content:encoded>
			<wfw:commentRss>http://mroth.info/blog/2002/10/07/new-software-commentfinder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
