Script to convert oo3 files to OmniOutliner 5 format


#1

The script below will find all OmniOutliner 3 files on your macOS startup volume and connected external volumes, and then open each of them in turn and convert them to OmniOutliner 5 format. The original OmniOutliner 3 files will be moved to the trash. Be patient – the script will alert you when it is finished.

The script uses GUI Scripting. It will prompt you to enable access for the script runner, such as Script Editor, if necessary.

The script is sensitive to the speed of your computer. If it does not work, increase the delaySeconds global variable by, say, 0.5 seconds and try again.

The script appears below. Copy and paste into your favorite script editor, then compile and run it.

-- Convert OmniOutliner 3 Files
-- Bill Cheeseman
-- Version 1.0.6 2017-04-08

-- This script automatically converts OmniOutliner 3 files to OmniOutliner 5 format. It converts all OmniOutliner 3 files on the startup volume, and those on any connected external volumes if the "When performing a search" pop-up menu in Finder Advanced Preferences is set to Search This Mac. The original OmniOutliner 3 files are moved to the Trash.

if not enabledGUIScripting(true) then return

-- If the script fails, try increasing the delaySeconds value
-- by a tenth of a second or more.
set delaySeconds to 1.0

-- Open a Finder window listing files whose names or
-- contents contain the given name extension.
set nameExtension to "oo3" -- OmniOutliner 3
activate application "Finder"
tell application "System Events"
	tell process "Finder"
		-- This GUI Scripting Finder search is almost instantaneous.
		keystroke "n" using {command down} -- open new window
		keystroke "f" using {command down} -- make it a search window
		delay delaySeconds
		keystroke "." & nameExtension & return
		delay delaySeconds
	end tell
end tell

-- Obtain user permission to convert OmniOutliner 3 files.
tell application "Finder"
	set fileCount to count every file in front window
	set ooCount to count (every file in front window whose kind is "OmniOutliner 3")
	if ooCount = 0 then
		display alert "No OmniOutliner 3 files found" message "No OmniOutliner 3 files were found on the startup volume or on any connected external volumes."
		return
	else
		if ooCount = 1 then
			set prompt to "Convert 1 OmniOutliner 3 file?"
		else
			set prompt to "Convert " & ooCount & " OmniOutliner 3 files?"
		end if
		display alert prompt message "OmniOutliner 3 files will be converted to OmniOutliner 5 format. Each file will be opened in OmniOutliner 5 and converted in turn. You will be notified when all files have been converted.

All OmniOutliner 3 files on the startup volume will be converted, as well as files on any connected external volumes if the \"When performing a search\" pop-up menu in Finder Advanced Preferences is set to Search This Mac. The original OmniOutliner 3 files will be moved to the Trash." buttons {"Cancel", "OK"}
		if button returned of result is "Cancel" then return
	end if
	
	
	-- Convert the files to OmniOutliner 5 format.
	activate application id "com.omnigroup.OmniOutliner5"
	set fileIndex to 1
	set convertedCount to 0
	repeat
		get a reference to item fileIndex in front window -- work around Finder scripting issue
		set fileAlias to original item of result as alias
		if kind of fileAlias is "OmniOutliner 3" then
			tell application id "com.omnigroup.OmniOutliner5" to open fileAlias
			tell application "System Events"
				tell process "OmniOutliner"
					-- insert GUI Scripting statements here:
					try
						click button "Convert…" of group 1 of splitter group 1 of window 1
					on error
						-- some document windows are configured differently.
						click button "Convert…" of group 2 of splitter group 1 of window 1
					end try
					delay delaySeconds
					if value of radio button 1 of group 2 of sheet 1 of window 1 = 0 then
						-- Change settings to move original OmniOutliner 3 file to trash after converting it.
						click radio button 1 of group 2 of sheet 1 of window 1 -- "Delete the older format file"
						delay delaySeconds
						click checkbox 1 of sheet 1 of window 1 -- "Remember my selection for next time" 
						click button "Cancel" of sheet 1 of window 1
						delay delaySeconds
						-- leave fileIndex unchanged in order to repeat with the same file after settings were changed
					else
						-- Save converted file and move original OmniOutliner 3 file to Trash.
						keystroke return -- click "Save Converted File…" button
						delay delaySeconds
						keystroke return -- click "Save" button
						delay delaySeconds
						click button 1 of window 1 -- close button
						if (fileAlias as string) contains "Trash" then
							-- File was successfully converted and moved to the trash. Leave fileIndex unchanged and adjust the fileCount down by 1, because the converted file has a different name extension and was therefore removed from the Searching window.
							set convertedCount to convertedCount + 1
							set fileCount to fileCount - 1
						else
							display alert "Failed to convert a file" message "Manually complete any pending conversions, close any open OmniOutliner sheets and windows, and run the script again to finish converting any remaining OmniOutliner 3 files."
							exit repeat
						end if
					end if
				end tell
			end tell
		else
			set fileIndex to fileIndex + 1 -- not an OmniOutliner 3 file so loop to next file
		end if
		if fileIndex > fileCount then exit repeat
	end repeat
	
	activate application "Finder"
	display alert "Done" message "Number of files converted: " & convertedCount & "."
end tell

on enabledGUIScripting(flag)
	tell application "System Events" to set GUIScriptingEnabled to UI elements enabled -- read-only in OS X Mavericks 10.9 or later
	if flag is true then
		if not GUIScriptingEnabled then
			activate
			set scriptRunner to name of current application
			display alert "GUI Scripting is not enabled for " & scriptRunner & "." message "Open System Preferences, unlock the Security & Privacy preference, select " & scriptRunner & " in the Privacy Pane's Accessibility list, and then run this script again." buttons {"Open System Preferences", "Cancel"} default button "Cancel"
			if button returned of result is "Open System Preferences" then
				tell application "System Preferences"
					tell pane id "com.apple.preference.security" to reveal anchor "Privacy_Accessibility"
					activate
				end tell
			end if
		end if
	end if
	return GUIScriptingEnabled
end enabledGUIScripting

#2

Thank you! Could you please post the script as a text file also? The formatting got kind of screwed up on the forum page.


#3

I attempted to edit my original post, but the OmniGroup forum software does not appear to let me post a text file and it insists on breaking the script text into separately scrollable pieces. I have edited my original post in an attempt to make it clearer. You must copy and past it into your favorite Script Editor in separate pieces.


#4

Also see the scripts in this thread.

SG


#5

I’ve combined the separate parts into one code block for display in the forum. You can do this by pasting the whole code, then selecting just the lines of code (if you have other text in the post) before you hit the </> icon in the reply toolbar.

I don’t have OOv5 yet so if someone could run the script and verify my paste didn’t break any of the excellent effort of @cheeseb, I would appreciate it.

-- Convert OmniOutliner 3 Files
-- Bill Cheeseman
-- Version 1.0.6 2017-04-08

-- This script automatically converts OmniOutliner 3 files to OmniOutliner 5 format. It converts all OmniOutliner 3 files on the startup volume, and those on any connected external volumes if the "When performing a search" pop-up menu in Finder Advanced Preferences is set to Search This Mac. The original OmniOutliner 3 files are moved to the Trash.

if not enabledGUIScripting(true) then return

-- If the script fails, try increasing the delaySeconds value
-- by a tenth of a second or more.
set delaySeconds to 1.0

-- Open a Finder window listing files whose names or
-- contents contain the given name extension.
set nameExtension to "oo3" -- OmniOutliner 3
activate application "Finder"
tell application "System Events"
	tell process "Finder"
		-- This GUI Scripting Finder search is almost instantaneous.
		keystroke "n" using {command down} -- open new window
		keystroke "f" using {command down} -- make it a search window
		delay delaySeconds
		keystroke "." & nameExtension & return
		delay delaySeconds
	end tell
end tell

-- Obtain user permission to convert OmniOutliner 3 files.
tell application "Finder"
	set fileCount to count every file in front window
	set ooCount to count (every file in front window whose kind is "OmniOutliner 3")
	if ooCount = 0 then
		display alert "No OmniOutliner 3 files found" message "No OmniOutliner 3 files were found on the startup volume or on any connected external volumes."
		return
	else
		if ooCount = 1 then
			set prompt to "Convert 1 OmniOutliner 3 file?"
		else
			set prompt to "Convert " & ooCount & " OmniOutliner 3 files?"
		end if
		display alert prompt message "OmniOutliner 3 files will be converted to OmniOutliner 5 format. Each file will be opened in OmniOutliner 5 and converted in turn. You will be notified when all files have been converted.

All OmniOutliner 3 files on the startup volume will be converted, as well as files on any connected external volumes if the \"When performing a search\" pop-up menu in Finder Advanced Preferences is set to Search This Mac. The original OmniOutliner 3 files will be moved to the Trash." buttons {"Cancel", "OK"}
		if button returned of result is "Cancel" then return
	end if
	
    -- Convert the files to OmniOutliner 5 format.
	activate application id "com.omnigroup.OmniOutliner5"
	set fileIndex to 1
	set convertedCount to 0
	repeat
		get a reference to item fileIndex in front window -- work around Finder scripting issue
		set fileAlias to original item of result as alias
		if kind of fileAlias is "OmniOutliner 3" then
			tell application id "com.omnigroup.OmniOutliner5" to open fileAlias
			tell application "System Events"
				tell process "OmniOutliner"
					-- insert GUI Scripting statements here:
					try
						click button "Convert…" of group 1 of splitter group 1 of window 1
					on error
						-- some document windows are configured differently.
						click button "Convert…" of group 2 of splitter group 1 of window 1
					end try
					delay delaySeconds
					if value of radio button 1 of group 2 of sheet 1 of window 1 = 0 then
						-- Change settings to move original OmniOutliner 3 file to trash after converting it.
						click radio button 1 of group 2 of sheet 1 of window 1 -- "Delete the older format file"
						delay delaySeconds
						click checkbox 1 of sheet 1 of window 1 -- "Remember my selection for next time" 
						click button "Cancel" of sheet 1 of window 1
						delay delaySeconds
						-- leave fileIndex unchanged in order to repeat with the same file after settings were changed
					else
						-- Save converted file and move original OmniOutliner 3 file to Trash.
						keystroke return -- click "Save Converted File…" button
						delay delaySeconds
						keystroke return -- click "Save" button
						delay delaySeconds
						click button 1 of window 1 -- close button
						if (fileAlias as string) contains "Trash" then
							-- File was successfully converted and moved to the trash. Leave fileIndex unchanged and adjust the fileCount down by 1, because the converted file has a different name extension and was therefore removed from the Searching window.
							set convertedCount to convertedCount + 1
							set fileCount to fileCount - 1
						else
							display alert "Failed to convert a file" message "Manually complete any pending conversions, close any open OmniOutliner sheets and windows, and run the script again to finish converting any remaining OmniOutliner 3 files."
							exit repeat
						end if
					end if
				end tell
			end tell
		else
			set fileIndex to fileIndex + 1 -- not an OmniOutliner 3 file so loop to next file
		end if
		if fileIndex > fileCount then exit repeat
	end repeat
	
	activate application "Finder"
	display alert "Done" message "Number of files converted: " & convertedCount & "."
	
end tell

on enabledGUIScripting(flag)
	tell application "System Events" to set GUIScriptingEnabled to UI elements enabled -- read-only in OS X Mavericks 10.9 or later
	if flag is true then
		if not GUIScriptingEnabled then
			activate
			set scriptRunner to name of current application
			display alert "GUI Scripting is not enabled for " & scriptRunner & "." message "Open System Preferences, unlock the Security & Privacy preference, select " & scriptRunner & " in the Privacy Pane's Accessibility list, and then run this script again." buttons {"Open System Preferences", "Cancel"} default button "Cancel"
			if button returned of result is "Open System Preferences" then
				tell application "System Preferences"
					tell pane id "com.apple.preference.security" to reveal anchor "Privacy_Accessibility"
					activate
				end tell
			end if
		end if
	end if
	return GUIScriptingEnabled
end enabledGUIScripting

#6

It compiled and worked correctly for me. Thanks.

However, if you haven’t updated to OmniOutliner 5.0.2 yet, the script is likely to be interrupted and fail when the Dialog offering the OmniOutliner 5.0.2 upgrade appears.


#7

I’ve edited the original post to fix this.

(Sorry to be so late in doing this, everyone! Bill actually asked me about fixing this when he made his original post, but I’m currently out on vacation so I was a bit slow to respond.)


#8

Here’s the error message I received when running the script. Any ideas?


#9

Here’s a conversion script which will convert all OmniOutliner 3 .oo3 files within a folder to the current OmniOutliner 5 .ooutline format. It scripts OmniOutliner natively rather than using GUI scripting, so it can operate without having to wait for dialog prompts to appear, etc.

If you point it at your home folder, it will convert everything in your account. When it finishes, it will tell you how many files it converted and how many it skipped (because they’d already been converted).

Hope folks find this useful!

-- First, prompt the user for the folder containing their OO3 documents and find all the *.oo3 files in that folder
set folderName to quoted form of POSIX path of (choose folder with prompt "Choose the folder containing the OmniOutliner 3 documents you'd like to convert.
This will convert all documents within that folder (and its subfolders) that haven't already been converted.")
set findResults to (do shell script "find " & folderName & " -name '*.oo3' | sed -e 's#//#/#' -e 's/\\.oo3$//'")
set oo3Paths to every text item of splitString(findResults, return)
global convertedDocuments
global skippedDocuments
set convertedDocuments to 0
set skippedDocuments to 0

-- Now iterate through all those files and convert them
repeat with oo3Path in oo3Paths
	convertOO3Path(oo3Path)
end repeat
display dialog "Converted " & convertedDocuments & " documents. (Skipped " & skippedDocuments & " documents that had already been converted.)" buttons "OK" default button "OK"

on convertOO3Path(oo3Path)
	log "Converting " & oo3Path & " from .oo3 to .ooutline"
	tell application "Finder"
		set oldFile to ((oo3Path & ".oo3") as POSIX file)
		set newFile to ((oo3Path & ".ooutline") as POSIX file)
		if exists newFile then
			log "... already exists, skipping"
			set skippedDocuments to skippedDocuments + 1
		else
			-- Tell OmniOutliner to open the .oo3 document
			tell application "OmniOutliner"
				set convertedDocument to open oldFile
				save convertedDocument in newFile
				close convertedDocument
			end tell
			set convertedDocuments to convertedDocuments + 1
		end if
	end tell
end convertOO3Path

-- This is just a little utility function to split the lines of output returned by the UNIX find command
on splitString(theString, theDelimiter)
	set oldDelimiters to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set theArray to every text item of theString
	set AppleScript's text item delimiters to oldDelimiters
	return theArray
end splitString