<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>hi, it&#39;s mike</title>
    <link>https://mike.puddingtime.org/tags/iwork/</link>
    <description>Recent content on hi, it&#39;s mike</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <managingEditor>mike@puddingtime.org (mike)</managingEditor>
    <webMaster>mike@puddingtime.org (mike)</webMaster>
    <copyright>© 2026, mike</copyright>
    <lastBuildDate>Thu, 12 Mar 2009 00:53:09 +0000</lastBuildDate>
    <atom:link href="https://mike.puddingtime.org/tags/iwork/index.xml" rel="self" type="application/rss+xml" />
    <item>
      <title>Scripting iWork Numbers &#39;09 With Appscript</title>
      <link>https://mike.puddingtime.org/posts/2009-03-12-scripting-iwork-numbers-09-with-appscript/</link>
      <pubDate>Thu, 12 Mar 2009 00:53:09 +0000</pubDate><author>mike@puddingtime.org (mike)</author>
      <guid>https://mike.puddingtime.org/posts/2009-03-12-scripting-iwork-numbers-09-with-appscript/</guid>
      
      <description>Simple work problem: I need to keep a log of stories published on the sites I cover. Part one of the problem is getting all the existing stories into the log. Part two of the problem is getting new stories into the log as I publish them from now on.
I was pretty excited about taking Numbers ‘09’s …</description>
      <content:encoded><![CDATA[<p>Simple work problem:  I need to keep a log of stories published on the sites I cover. Part one of the problem is getting all the existing stories into the log.  Part two of the problem is getting new stories into the log as I publish them from now on.</p>
<p>I was pretty excited about taking Numbers &lsquo;09&rsquo;s scripting support out for a spin. Unfortunately it&rsquo;s a little anemic, especially compared to full-featured Ruby libraries like <a href="http://roo.rubyforge.org/">roo</a> and <a href="http://spreadsheet.rubyforge.org/">spreadsheet</a>. For instance, it doesn&rsquo;t really have a conception of &ldquo;the last row with data in it,&rdquo; as the other two do.  So you can&rsquo;t just plop new data into the first empty row without doing something like iterating through all rows and finding the first one where a particular cell is empty. I uploaded <a href="http://mph.puddingbowl.org/ruby/Numbers-rb/index.html">the appscript-exported Numbers scripting dictionary</a> if you&rsquo;re curious about what it can do.</p>
<p>There&rsquo;s some interesting room to grow for both scripts.  In addition to tracking stories, we have to keep up with their overall performance.  With the <a href="http://github.com/robmckinnon/rugalytics/tree/master">rugalytics</a> gem it&rsquo;s possible to mine performance data from Google Analytics based on the title of a given article or regular expressions taken from the URL.</p>
<p>Anyhow, this one solves part one.  It&rsquo;s a one-time-only importer that runs through the indexes for each container and grabs everything:</p>
<pre><code>    [sourcecode language=&quot;ruby&quot;]

    !/usr/bin/env ruby

    require 'rubygems'

    require 'appscript'

    include Appscript

    require '/Users/mph/lib/ruby/CDEV.rb'

    require 'hpricot'

    require 'open-uri'

    include Hpricot

    num = app(&quot;Numbers&quot;)

    ss = num.open(&quot;/Users/mph/Desktop/Invoices.numbers&quot;)

    sheet = ss.sheets[&quot;Invoices&quot;]

    table = sheet.tables[&quot;Current Quarter&quot;]

    containers = [&quot;netsysm&quot;, &quot;nethub&quot;, &quot;netsecur&quot;, &quot;linux_unix&quot;, &quot;netos&quot;, &quot;windows&quot;]

    base_url = &quot;http://enterprisenetworkingplanet.com/&quot;

    cdevids = []

    containers.each do |con|

      page = Hpricot(open(&quot;{base_url}{con}&quot;))

      index = page.search(&quot;//div[@class='section_index_list'&quot;)

      articles = index.search(&quot;//div[@class='post']&quot;)

      articles.each do |a|

        link =  a.search(&quot;//h3/a&quot;).first

        url =  link[:href]

        art = CDEV.new(url)

        unless cdevids.include?(art.id)

          cdevids &lt;&lt; art.id

          title = art.title

          url = art.url

          author = art.author

          pubdate = art.pubdate.strftime(&quot;%m/%d/%y&quot;)

          row = table.rows.add_row_above(table.rows[2])

          table.rows[2].cells[1].value.set(author)

          table.rows[2].cells[2].value.set(pubdate)

          table.rows[2].cells[3].value.set(title)

          table.rows[2].cells[6].value.set(url)

        end

      end

    end

    [/sourcecode]
</code></pre>
<p>This one is for part two.  It&rsquo;s just part one stripped down and cast toward working on the foremost window in Safari, so when I&rsquo;m giving an article a look from the front-end to make sure the CMS didn&rsquo;t mangle it, I can run the script and capture the article info in my spreadsheet.</p>
<p>This one points to another oversight in the existing dictionary for Numbers &lsquo;09:  One of the new features in the software is something called &ldquo;Table Categories.&rdquo;  You can pick any column and create a category based on its content.  It&rsquo;s handy for things like quarterly invoicing, because you can take a lump of rows, create a table category out of the publication date and get a nicely sorted table with quarterly subtotals for each month. That poses two problems for my script:</p>
<ol>
<li>
<p>When a spreadsheet has active Table Categories, you can&rsquo;t add a row to it.</p>
</li>
<li>
<p>The Table Categories functionality doesn&rsquo;t exist in the current Numbers scripting dictionary.</p>
</li>
</ol>
<p>So if the script is run while the table is categorized, it fails. It doesn&rsquo;t have any way to query the table and figure out if it&rsquo;s categorized, so I had to fall back on UI scripting to look for whether the &ldquo;Table&rdquo; menu has an item that reads &ldquo;Disable All Categories.&rdquo;  If it does, the script clicks the menu item. Once the script is done adding stuff, it clicks a menu item called &ldquo;Enable All Categories&rdquo; to put the table into a more readable state again.</p>
<p>I hate UI scripting. <a href="http://blog.zenspider.com/2007/07/applescript-is-a-pita-exportin.html">Another blog entry</a> gave me a head start on figuring out what to call the menu items I needed to find, and <a href="http://appscript.sourceforge.net/tools.html">ASTranslate</a> did me the favor of cutting through all the Applescript hyperventilation and rendering things down to something more usable.</p>
<p>So anyhow:</p>
<pre><code>[sourcecode language=&quot;ruby&quot;]



!/usr/bin/env ruby

require 'rubygems'

require 'appscript'

require '/Users/mph/lib/ruby/CDEV.rb'

include Appscript

num = app(&quot;Numbers&quot;)

saf = app(&quot;Safari&quot;)

num_sys = app(&quot;System Events&quot;).application_processes[&quot;Numbers&quot;]

num_cat_menu = num_sys.menu_bars[1].menu_bar_items[&quot;Table&quot;].menus[1]

num_cat_disable = num_cat_menu.menu_items[&quot;Disable All Categories&quot;]

num_cat_enable = num_cat_menu.menu_items[&quot;Enable All Categories&quot;]

ss = num.open(&quot;/Users/mph/Desktop/Invoices.numbers&quot;)

sheet = ss.sheets[&quot;Invoices&quot;]

table = sheet.tables[&quot;Current Quarter&quot;]

num.activate

table.activate

page = saf.documents[0].get

url = page.URL.get

art = CDEV.new(url)

title = art.title

url = art.url

author = art.author

pubdate = art.pubdate.strftime(&quot;%m/%d/%y&quot;)

num_sys.frontmost.set(true)

if num_cat_disable.exists

 num_cat_disable.click

end

row = table.rows.add_row_above(table.rows[2])

table.rows[2].cells[1].value.set(author)

table.rows[2].cells[2].value.set(pubdate)

table.rows[2].cells[3].value.set(title)

table.rows[2].cells[6].value.set(url)

num_cat_enable.click

 [/sourcecode]
</code></pre>
<p>#appscript #iwork #numbers #ruby</p>
<p><em><a href="mailto:mike@puddingtime.org?subject=Re%3A%20Scripting%20iWork%20Numbers%20%2709%20With%20Appscript">Drop a line about this</a>.</em></p>]]></content:encoded>
    </item>
  </channel>
</rss>
