🕷 zenspider.com

by ryan davis

Looking for the Ruby Quickref?


Published 2006-02-21 @ 20:50

Tagged toys, applescript

I’m posting this here mainly because it is perverse.

It is probably the most complex applescript I’ve ever had the “honor” of creating. This script generates a new applescript that will recreate the frontmost OmniOutliner document as best it can. It supports multiple columns and column datatypes. I used it while working at Omni to test the applescript support and help automate some of our testing. It should be of use to anyone who wants to automate some stuff into a clean editable format just shy of a full fledged spreadsheet (If you haven’t looked at OmniOutliner’s column support, you really should).


    Make Script v. 1.0.4
        generates an applescript from your frontmost Outliner document that when run recreates that document as close as possible
    + no styles
    + probably a lot more
    1.0.4 - added version history, fixed support for missing values.


global scriptText
set scriptText to ""

on addText(aLine)
    set scriptText to scriptText & aLine & return
end addText

on addComment(aLine)
    my addText("-- " & aLine)
end addComment

tell application "OmniOutliner Professional 3.5"
    tell front document
        set columnCount to count of columns
        -- preface
        my addText("tell application \"OmniOutliner Professional\"")
        my addText("tell make new document")
        my addText("set sorting postponed to true")
        -- styles TODO
        my addComment("columns")
        repeat with columnIndex from 1 to columnCount
            set aColumn to column columnIndex
            if columnIndex > 2 then
                my addText("tell make new column")
                my addText("tell column " & columnIndex)
            end if
            if name of aColumn is not "" then
                my addText("set name to \"" & name of aColumn & "\"")
            end if
            if columnIndex > 1 and type of aColumn is not rich text then
                -- can't set the note column type
                my addText("set type to " & type of aColumn)
            end if
            my addText("set width to " & (width of aColumn as integer))
            if sort order of aColumn is not none then
                my addText("set sort order to " & sort order of aColumn)
            end if
            -- TODO: durations have other format values
            if type of aColumn is rich text then
                -- do nothing
            else if type of aColumn is popup then
                repeat with anEnum in every enumeration in aColumn
                    my addText("set name of (make new enumeration) to \"" & name of anEnum & "\"")
                end repeat
                set aFormat to format of aColumn
                if |format| of aFormat is not "" then
                    my addText("set format string  to \"" & |format| of aFormat & "\"")
                end if
            end if
            if alignment of aColumn is not natural then
                my addText("set alignment to " & alignment of aColumn)
            end if
            if summary type of aColumn is not none then
                my addText("set summary type to " & summary type of aColumn)
            end if
            (* BUG:17121
                my addText("set background color to {51381, 51609, 65535}")

            (* TODO
            column style  style  -- The style of the column.  This is used as the default style for values in the column (but is overriden by any style attributes defined on rows)

            my addText("end -- column")
        end repeat
        my addComment("rows")
        set previousLevel to -1
        repeat with rowIndex from 1 to count of rows
            set aRow to row rowIndex
            set rowCount to rowIndex
            set currentLevel to level of aRow
            if currentLevel ≤ previousLevel then
                repeat (previousLevel - currentLevel + 1) times
                    my addText("end -- row")
                end repeat
            end if
            set previousLevel to currentLevel
            my addText("tell make new row -- row #" & rowIndex)
            repeat with colIndex from 1 to columnCount
                if value of cell colIndex of aRow is not "" then
                    set aValue to value of cell colIndex of aRow
                    if aValue is not missing value then
                        -- TODO: support other types?--they might all be handled correctly by value now
                        my addText("set value of cell " & colIndex & " to \"" & value of cell colIndex of aRow & "\"")
                        my addText("set value of cell " & colIndex & " to missing value")
                    end if
                end if
            end repeat
            if state of aRow is not unchecked then
                my addText("set state to " & state of aRow)
            end if
            if has subtopics of aRow then
                my addText("set expanded to " & expanded of aRow)
            end if
        end repeat
        repeat previousLevel times
            my addText("end -- row")
        end repeat
        -- close
        my addText("set sorting postponed to false")
        my addText("end -- document")
        my addText("end -- application")
    end tell -- document
end tell -- application

tell application "Script Editor"
    set myScript to make new document with properties {text:scriptText}
end tell -- application