Problem with Export>Listing (script)


I’m trying to export the names of the contents os a database using the script provided (UTF-8) and although it does create a .txt file, when I open it (in TextEdit) all I see are Chinese characters.

I also tried to setup the alternative script (UTF-16) given by Christian ( … ht=listing) and the result was the same: Chinese characters.

Am I doing something wrong?

– MJ

Using DTPO (trial)
MacBook Pro OS 10.4.8

Which default encoding is specified in TextEdit’s preferences to open files? If it’s UTF-8, UTF-16 or automatic then this shouldn’t happen.

It’s UTF-8.


Can it be caused by a big .dtBase? This one has around 1.5GBs.

– MJ

This shouldn’t make a difference. But do you use an Intel or PowerPC Mac? And what’s the default language of your system?

I use an Intel (MacBook Pro) with OS 10.4.8.

The default language is English.

– MJ

Hmmm… the AppleScript Unicode handling on Intel Macs does not work as expected. Here’s a fixed script:

tell application "DEVONthink Pro"
		if not (exists current database) then error "No database is open."
		set theDatabase to the current database
		set theFile to choose file name default name ((name of theDatabase) as string) & ".txt"
		show progress indicator "Creating Listing..."
		set theListing to my createListing(children of root of theDatabase, "")
		set thePath to POSIX path of theFile
		if thePath does not end with ".txt" then set thePath to thePath & ".txt"
		set writeFile to open for access (thePath as POSIX file) with write permission
		set eof writeFile to 0
		write (ASCII character 254) to writeFile
		write (ASCII character 255) to writeFile
		write (theListing as Unicode text) to writeFile
		close access writeFile
		hide progress indicator
	on error error_message number error_number
		hide progress indicator
		if the error_number is not -128 then
				display alert "DEVONthink Pro" message error_message as warning
			on error number error_number
				if error_number is -1708 then display dialog error_message buttons {"OK"} default button 1
			end try
		end if
	end try
end tell

on createListing(theseRecords, theTabs)
	local this_record, this_type, this_listing, this_name
	set this_listing to ""
	tell application "DEVONthink Pro"
		repeat with this_record in theseRecords
			set this_name to (name of this_record as string)
			set this_listing to this_listing & theTabs & this_name & return
			set this_type to type of this_record
			if this_type is group or this_type is sheet then
				step progress indicator this_name
				set this_listing to this_listing & my createListing(children of this_record, theTabs & tab)
			end if
		end repeat
	end tell
	return this_listing
end createListing

It’s working perfectly. Thank you very much, Christian.

Your support is also a big reason why using DT/DA is such a joy. :slight_smile:

– MJ

PS. I should learn about applescript in the future – it’s way cool.

It’s always a pleasure to help customers, especially if a script can do the job. And to be honest - I’ve never done any scripting before DT Pro but the power of its AppleScript suite makes a lot of stuff possible and can automate many tasks.