TextMate Command with Bash and AS script: encoding problem?

Hi,

i’m working on a TextMate Command that contains BASH and Applescript commands.

The purpose of the command is to transfer the selected text in Texmate to an
entry in a specific database and group in DevonThink Pro.

The problem ist that Applescript seems to mangle with the character encoding
thus the german umlauts in the transfered text are illegible. I spent hours
to solve this problem to no avail up now.

The text that is meant to deal with is a template for a literature excerpt
for academic research. I created a snippet in TextMate for this template:

Here’s an example of the snippet with text:

The command is supposed to extract the term (here “Ãœbercoffee”) and remove the
“_” and “:” which are supposed to be transcoded (later) with Multimarkdown,
thus the term is displayed in italics. That’s the

part.

set theRecord to create record with {name:"$(head -n 1 | tr -d ":_")", content: theText, type:rtf} in theGroup

The term is used as the filename in DevonThink. All the lines including the term are the content of this file in DevonThink. That’s the

Ok, the term as filename is used correctly with german umlauts in DevonThink, but the content of file isn’t.

The reason might be that the filename is actually just the content of $TM_SELECTED_TEXT, which contains the
content of the selected text in TextMate, and is piped from the STDIN through

while the content in the Applescript variable theText is filled via Applescipt’s

set theText to get system attribute "TM_SELECTED_TEXT".

My testing revealed that $TM_SELECTED_TEXT as a bash variable contains the correct content w/ german umlauts
while theText filled via get system attribute does not!

Further finding is that if I use this for instance

set theRecord to create record with {name:"Ãœbercoffee", content: "Ãœbercoffee", type:rtf} in theGroup

the umlauts both in the filename as in the content are correct.

I guess Applescript is to blame messing around with the encoding of that bash variable.

Who can help me, mates? Thanks in advance! System is OS X 10.7.3, Textmate 1.5.10 (1631) and DevonThink Pro Office 2.3.3

Ok, here’s the whole TextMate command:

#!/bin/bash

        osascript <<-APPLESCRIPT

	set theText to get system attribute "TM_SELECTED_TEXT" -- Import dynamic variable of selected text of Textmate 

	set theLength to (length of theText) as number

	if (theLength as number) > 0 and (theLength as number) < 64*1024 then 
	tell application id "com.devon-technologies.thinkpro2"
		try
			set theDatabase to open database "/Users/Administrator/Documents/Wettbewerbsrecht.dtBase2"
			set theGroup to get record at "Exzerpte" in theDatabase
			set theRecord to create record with {name:"$(head -n 1 | tr -d ":_")", content: theText, type:rtf} in theGroup
			do shell script "afplay /System/Library/Sounds/Purr.aiff"
		end try
	end tell
	 else 
	 error_handler("Please mark some text first! Max. 64KB!")
	end if

	on error_handler(theErrorMsg)
	do shell script "afplay /System/Library/Sounds/Basso.aiff"
	tell application "TextMate"	
		display alert "Warning" ¬
		message theErrorMsg ¬
		as warning
	end tell
	end error_handler

APPLESCRIPT