Checking for default text editor

[Reposting here from Code Exchange: http://macscripter.net/viewtopic.php?id=33635]

Hi all,

I’m writing a script that scrapes the web pages at http://kindle.amazon.com/ that contain the saved notes and highlights from my Kindle books and exports them into OmniOutliner. Everything is working great so far.

I’d like to share the script with others, but they won’t necessarily have OmniOutliner. I wrote some code that exports everything to generic OPML and puts it into TextEdit. That works too, but unless you convert the document to plain text (or have that set as your default) you can’t save with the .opml extension. I figured out how to script the keypress that converts the document to plain text (Shift-Cmd-T, btw) and that works fine except for the warning that pops up which I get rid of by pressing return programmatically.

It occurred to me that if I could open up the user’s default text editor instead, it might make it easier for folks like me who use TextEdit for RTF content and TextMate (or a similar text editor) for the plain text stuff. I’m not autosaving anything; I just need to open the editor and drop in the OPML text.

I’ve been looking around, but I can’t find where the setting for the user’s default text editor is stored. If figure that if it’s in a plist somewhere that I should be able to query it and open that text editor instead of TextEdit.

Does anyone know how to get that info via applescript?

-Tim

Hello.

I just assume that the default text editor is assumed to be text edit.
I believe there is one way to figure this out, and that would be to create a new text file and inspect the uti (uniform type identifier ) if someone have taken measure to tinker with the default editor via default prefs pane.
if not set this way, then you must find an association with public.text. I guess this will be installed with mime types, but they should also be around somewhere in the defaults system. Happy hunting.
If the assumptions work as other assumptions work, then you would get no value for public.text, and then TextEdit is the default. If not, then you will get a value out. Maybe you will get TextEdit out as a value afterwards, if reset to TextEdit.

Hi everyone,

I’ve been doing some more digging, and I finally found where the default text editor setting is stored.

I used the suggestion offered here: http://www.westwind.com/reference/OS-X/finding-settings.html and found that the setting is in ~/Library/Preferences/com.apple.LaunchServices.plist. If you search through you’ll see a record with something like the following:

LSHandlerContentType String public.plain-text
LSHandlerRoleAll String com.macromates.textmate

If you have TextEdit as your default it might be set to com.apple.textedit or be missing entirely.

Now I just need to figure out how to query this plist to determine what the setting is.

I hope this helps someone else.

-Tim

Tim, I think you could save your opml text easier. A google search told me “opml” is just an xml file. An xml file can be written to disk directly by applescript by writing it as text. So my question would be why do you need TextEdit or any other application for that matter. Presumably you have the opml text in applescript, so just write it like this…

set opmlText to "the xml text"
set savePath to (path to desktop folder as text) & "notes.opml"
writeTo(opmlText, savePath, false, string)

on writeTo(fileData, targetFile, apendData, mode) -- apendData is true or false... mode is string, list, or record (no quotes around either)
	try
		set targetFile to targetFile as text
		if targetFile does not contain ":" then set targetFile to POSIX file targetFile as text
		set openFile to open for access file targetFile with write permission
		if apendData is false then set eof of openFile to 0
		write fileData to openFile starting at eof as mode
		close access openFile
		return true
	on error
		try
			close access file openFile
		end try
		return false
	end try
end writeTo

And if you then wanted to open that file in the user’s default text editor, use this…

tell application "Finder" to open file savePath

If you want the default application you can use a command line tool I wrote. See here.

Hello.

I found out that I could issue the command defaults read com.apple.LaunchServices
to get all the default settings. It the setting had been changed to something else than TextEdit there would have been an entry there. see man defaults for info on the defaults command. I think Hanks app might be easier to use though.

I really like those West Wind pages, contains lots of useful info. Thanks for reminding me.

Great suggestions everyone! I’ll get to work on this.

-Tim

Here’s a quick shell script which reads, and attempts to parse out the text editor (can there be more than 1?, don’t know).


try
	set text_editor to (do shell script "defaults read com.apple.LaunchServices | grep -A 1 'public.plain-text' | sed 1d | cut -d = -f 2 | cut -d ';' -f 1")
	-- "com.barebones.bbedit" (includes the quotes, which I can't be bothered to remove)	
on error
	-- an error would indicate that TextEdit is still the default, ie., never actually set?
	set text_editor to "TextEdit"
end try

Hello.

Couldn’t help it. :slight_smile:


try
	set text_editor to (do shell script "defaults read com.apple.LaunchServices | grep -A 1 'public.plain-text' | sed 1d | cut -d = -f 2 | cut -d ';' -f 1")
	-- "com.barebones.bbedit" (includes the quotes, which I can't be bothered to remove)	
on error
	-- an error would indicate that TextEdit is still the default, ie., never actually set?
	set text_editor to "\"TextEdit\""
end try
” acme quote go away!
set {tids, text item delimiters} to {text item delimiters, "\""}
set text_editor to text item 2 of text_editor as text
set text item delimiters to tids
log text_editor --> (*com.apple.textedit*)

Hello.

I found a utility named duti for setting the default app for a document type.
Duti can be downloaded from here