I can't get OmniOutliner to export a text file

Hello there. I’m an newcomer to Applescript (I began my first script 3 days ago), and although I’ve managed to solve most of my coding errors on my own, this one has me really stumped.

The script is supposed to copy all of the checked rows of an existing outline into a new outline and then export that new document as a plain text file (column data separated by tabs). I discovered the codes for specifying the output format from this page:
http://forums.omnigroup.com/showthread.php?t=31396

However, despite trying 3 different approaches, I can’t get OO to export a text file.

Here’s the code:

tell application "OmniOutliner Professional"
    
    -- Initalise variables for the outline that's already open
    set existingDoc to front document of application "OmniOutliner Professional"
    set existing_filename to name of existingDoc
    
    -- Create & initialise a new outline which will receive only the checkboxed items from the original outline
    set new_filename to existing_filename & "Temp"
    set tempDoc to make new document with properties {name:new_filename} at beginning of documents
    set status visible of tempDoc to true
    make new column with properties {title:"2nd column", width:400} at end of columns of tempDoc
    
    -- Copy all of the checked items over from the first outline to the second
    duplicate (every row of existingDoc whose state is checked) to end of children of tempDoc
    
    -- I want to switch off status checkboxes before I export the file
    set status visible of tempDoc to false
    
    -- Export the second outline as a plain text file with data separated by tabs, reverting to the filename of the original outline
    
    -- export tempDoc to POSIX file ("/Users/johnhenderson/Desktop/" & existing_filename & ".txt") as "public.plain-text"
    -- ... gives the error "OmniOutliner Professional got an error: Can’t make file \"Macintosh HD:Users:johnhenderson:Desktop:GiftsForWomen.txt\" into type Unicode text." number -1700 from file "Macintosh HD:Users:johnhenderson:Desktop:GiftsForWomen.txt" to Unicode text
    
    -- export tempDoc to file ((path to desktop as string) & existing_filename & ".txt") as "NSStringPboardType"
    -- ... gives the error "OmniOutliner Professional got an error: Can’t get file \"Macintosh HD:Users:johnhenderson:Desktop:GiftsForWomen.txt\"." number -1728 from file "Macintosh HD:Users:johnhenderson:Desktop:GiftsForWomen.txt"
    
    set tempDocPath to ((path to desktop as string) & existing_filename & ".txt")
    tell front document
        export as "public.plain-text" to file tempDocPath
    end tell
    -- Whether I use "as [format] to [filename]" or "to [filename] as [format]", I get the same error: error "OmniOutliner Professional got an error: Can’t get file \"Macintosh HD:Users:johnhenderson:Desktop:GiftsForWomen.txt\" of document 1." number -1728 from file "Macintosh HD:Users:johnhenderson:Desktop:GiftsForWomen.txt" of document 1
    
    -- Second outline has done its job, close without saving
    close tempDoc
    
end tell

I hope that someone can spot the problem before I tear all of my hair out!
Many thanks, John.

Edit: I forgot to mention that I’m using OmniOutliner ver. 3.10.6

I found an answer to my problem, but it’s more of a work-around than an elegant solution; I’ve had to use “System Events” to simulate the keystrokes that I would manually type to export the file. But this approach has its own problems…

  1. I can’t seem to select the “file type” drop-down menu using the keyboard. However, the export dialog box defaults to the file-type option that was last used, so if that’s already been selected, then this will work.

  2. To save the text file using the same name as the original outline, I’d have to manually remove the extra word “Temp” from the end of the filename that appears in OO’s Export dialog box. But that’s 4 keystrokes, and I’d prefer to only use one. Therefore I’ve changed the code so that the original filename just has a single character added onto the end – “X”. Much easier to delete!

Anyway, enough waffle. Here’s the code…

tell application "OmniOutliner Professional" to activate
tell application "System Events"
    keystroke "e" using {option down, command down} -- Option-Cmd-E brings up the Export dialog box
    key code 124 -- Right-arrow key gets rid of the light-blue highlight on the filename
    key code 51 -- Backspace to remove the "X" at the end of the name
    key code 36 -- Return key is the same as clicking the "Export" button
end tell

It’s not pretty, but it works.

If anybody needs them, the key codes are here:

Here’s hoping that you still have at least some of your hair. I know this is late to the party but there is a better way and I dislike unanswered questions that are pretty core (like exporting a doc).

I’ve included three of the export document types.

Hope this helps,
Ken

tell application "OmniOutliner"
	
	--Export to the desktop, using the current filename (sans extension)
	set deskPath to path to desktop folder as string
	set fileN to name of document 1 as string
	
	--Combine path and filename
	set DeskSave to deskPath & fileN
	
	--As export only affects visible text, expand all so that entire doc is exported
	expandAll
	
	--Export as three different doc types and append the appropriate extension
	export document 1 to (DeskSave & ".opml") as "org.opml.opml"
	export document 1 to (DeskSave & ".txt") as "public.plain-text"
	export document 1 to (DeskSave & ".rtf") as "public.rtf"
	
end tell

and, FWIW, a JavaScript for Automation version, with a folder choice dialog, and a notification of results.

function run() {
    var oo = Application('OmniOutliner'),
        oosa = (oo.includeStandardAdditions = true, oo),

        // Active document ?
        ds = oo.documents,
        d = ds.length ? ds.at(0) : undefined;

    if (d) {

        // Saved ?
        var strName = d.file() ? d.name() : '',
            pathFolder = strName ? chosenFolder() : undefined;

        if (!pathFolder) {
            oosa.activate();
            return oosa.displayAlert("First save the OO document ...");
        }

        var strStem = pathFolder.toString() + '/' + strName,
            lstExported = [];

        // Export UTIs and extensions ?
        [{
            uti: 'org.opml.opml',
            extn: 'opml'
        }, {
            uti: 'public.plain-text',
            extn: 'txt'
        }, {
            uti: 'public.rtf',
            extn: 'rtf'
        }]
        .forEach(function (type) {
            oo.export(d, {
                to: strStem + '.' + type.extn,
                as: type.uti
            });
            lstExported.push(type.extn);
        });


        // Result ?

        var a = Application.currentApplication(),
            sa = (a.includeStandardAdditions = true, a);

        sa.activate();
        sa.displayNotification('as: ' + lstExported.join('\n'), {
            withTitle: 'Exported from ' + oo.id(),
            subtitle: strName,
            soundName: 'Glass.aiff'
        });
    }
}

// chosenFolder :: () -> maybe Path
function chosenFolder() {
    var a = Application.currentApplication(),
        sa = (a.includeStandardAdditions = true, a);

    sa.activate();

    return sa.chooseFolder({
        withPrompt: 'Choose folder',
        defaultLocation: sa.pathTo('desktop')
    });
}