Japanese or cyrillic characters in Growl via AppleScript

The Growl forums have moved to Google Groups, this forum is read only.
barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Japanese or cyrillic characters in Growl via AppleScript

Postby barnacleboy » Tue Mar 27, 2007 1:39 pm

hi folks!

i'm implementing growl notifications into a qt application (qt framework, not quicktime) at the moment, but the textencoding is worrying me. i know it's an apple script issue, but maybe someone here has already faced this problem.

we are receiving track information data from itunes and display it in the gui of the application correct, but i can't get them work with apple script. i know that apple script is usually working with mac roman encoding. so after generating the script into a qt qstring i convert it to utf8 to get it compiled and run - converting to mac roman isn't working. at the moment only western european special characters are working like umlauts or similar chars - japanese, chinese or cyrillic characters are still fucked up. what could i do to get this symbols working with apple script?

well thanks for any help in advance!

phil

User avatar
The_Tick
Cocoaforge Admin
Posts: 4642
Joined: Thu Dec 02, 2004 6:06 am
Contact:

Postby The_Tick » Tue Mar 27, 2007 4:01 pm

Wait, you're using the quicktime framework. Does that mean your application isn't in applescript?

User avatar
Diggory
Growl Team
Posts: 129
Joined: Sat Dec 04, 2004 12:17 pm
Location: London

Postby Diggory » Tue Mar 27, 2007 4:41 pm

I think he means the http://www.trolltech.com/products/qt framework.

barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Postby barnacleboy » Tue Mar 27, 2007 4:54 pm

yeah, the qt framework from trolltech...

User avatar
The_Tick
Cocoaforge Admin
Posts: 4642
Joined: Thu Dec 02, 2004 6:06 am
Contact:

Postby The_Tick » Tue Mar 27, 2007 5:35 pm

Ahh, when you said "not quicktime", I thought you meant quicktime player.app.

User avatar
bgannin
Growl Team
Posts: 1817
Joined: Thu Dec 02, 2004 8:11 am
Location: ..here
Contact:

Postby bgannin » Tue Mar 27, 2007 8:04 pm

Let me see if I have this correct....

You're converting the string from MacRoman -> UTF8 -> MacRoman?

I'd imagine the problem here is that the encoding translations are inserting encoding-specific characters not recognized in the various contexts you're attempting. Is there a specific reason you have to do this (I say this as an AS n00b [so simply say, bzzt... check the docs bgannin if needed], but given that Mac OS X itself really likes UTF(8 || 16) is there a reason you can't stick with it completely?)
Try my software!

#define ADIUMX pimp //by me
#define QUESTION ((2b) || (!2b))
Have you hugged a programmer today?

barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Postby barnacleboy » Wed Mar 28, 2007 12:48 am

at the moment i'm converting the qstring into utf8 and compile & execute the script. i've tried to use only mac roman, but if i do so the script gets neither compiled nor executed...

User avatar
The_Tick
Cocoaforge Admin
Posts: 4642
Joined: Thu Dec 02, 2004 6:06 am
Contact:

Postby The_Tick » Wed Mar 28, 2007 2:13 am

I misread that. bgannin is asking why you aren't just sending utf8 to Growl. Are you explaining why you cannot?

barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Postby barnacleboy » Wed Mar 28, 2007 2:20 am

i'm sending utf8...

User avatar
bgannin
Growl Team
Posts: 1817
Joined: Thu Dec 02, 2004 8:11 am
Location: ..here
Contact:

Postby bgannin » Wed Mar 28, 2007 5:21 am

I'll look into this a bit tonight to see if I can figure something out. I've been meaning to flesh out my AppleScript exposure anyway. This is against 0.7.6, correct?
Try my software!



#define ADIUMX pimp //by me

#define QUESTION ((2b) || (!2b))

Have you hugged a programmer today?

barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Postby barnacleboy » Wed Mar 28, 2007 11:08 am

yeah, exactly. i don't think it's a growl issue. i think the problem is that my script simply doesn't get the special characters...

User avatar
The_Tick
Cocoaforge Admin
Posts: 4642
Joined: Thu Dec 02, 2004 6:06 am
Contact:

Postby The_Tick » Wed Mar 28, 2007 4:25 pm

Can you write them out to a text file? That'd prove or disprove it being a problem on the Growl side.

barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Postby barnacleboy » Wed Mar 28, 2007 7:24 pm

yeah it works perfectly if i copy and paste the text into the os x script editor...

User avatar
bgannin
Growl Team
Posts: 1817
Joined: Thu Dec 02, 2004 8:11 am
Location: ..here
Contact:

Postby bgannin » Wed Mar 28, 2007 9:44 pm

I came across this thread that I thought may be relevant (they treated the string as data and then cast it to Unicode text basically)

http://bbs.applescript.net/viewtopic.ph ... action=new
Try my software!



#define ADIUMX pimp //by me

#define QUESTION ((2b) || (!2b))

Have you hugged a programmer today?

barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Postby barnacleboy » Thu Mar 29, 2007 2:29 pm

thanks this hint sounds very useful, BUT now i have another problem: the script isn't compiling - i guess it's because of "»". when looking in my console output i get different characters instead of "«" and "»"...

Code: Select all

"   notify with name \"Track Notification\" title \"" + metaData.track() +
      "\" description «data utxt0020041B0435043D0438043D0433044004300434» as Unicode text"...

barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Postby barnacleboy » Thu Mar 29, 2007 3:11 pm

argh. now i've gotten these 2 symbols right and copy/paste from my console output into the script editor works fine. it isn't working when compiling/running the script from the program...

here are the apple script handling functions:

Code: Select all

OSAID LowCompileAppleScript( const void* text, long textLength )
{
//     qDebug() << "Compiling script";

    QString result;
    OSStatus err;
    AEDesc scriptTextDesc;
    OSAID scriptID = kOSANullScript;

    AECreateDesc( typeNull, NULL, 0, &scriptTextDesc );

    /* put the script text into an aedesc */
    err = AECreateDesc( typeUTF8Text, text, textLength, &scriptTextDesc );
    if ( err != noErr )
        goto bail;

    /* compile the script */
    err = OSACompile( theComponent, &scriptTextDesc, kOSAModeNull, &scriptID );
    if ( err != noErr )
        goto bail;

bail:
    return scriptID;
}


/* LowRunAppleScript compiles and runs an AppleScript
provided as text in the buffer pointed to by text.  textLength
bytes will be compiled from this buffer and run as an AppleScript
using all of the default environment and execution settings.  If
resultData is not NULL, then the result returned by the execution
command will be returned as typeChar in this descriptor record
(or typeNull if there is no result information).  If the function
returns errOSAScriptError, then resultData will be set to a
descriptive error message describing the error (if one is
available). */
bool LowExecAppleScript( OSAID scriptID, QString& resultToken )
{
//     qDebug() << "Executing script";

    QString s;
    char result[4096] = "\0";

    OSStatus err;
    AEDesc resultData;
    OSAID resultID = kOSANullScript;

//     qDebug() << "Running script";
    /* run the script/get the result */
    err = OSAExecute( theComponent, scriptID, kOSANullScript, kOSAModeAlwaysInteract, &resultID );

//     qDebug() << "Running script done" << err;
    if ( err == errOSAScriptError )
    {
        OSAScriptError( theComponent, kOSAErrorMessage, typeUTF8Text, &resultData );
        int length = AEGetDescDataSize( &resultData );
        {
            AEGetDescData( &resultData, result, length < 4096 ? length : 4096 );
            result[ length ] = '\0';
            s = QString::fromUtf8( (char *)&result, length );
            LOG( 1, "AppleScript error: " << s << "\n" );
        }

        return false;
    }
    else if ( err == noErr && resultID != kOSANullScript )
    {
//         qDebug() << "Getting script result";

        OSADisplay( theComponent, resultID, typeUTF8Text, kOSAModeNull, &resultData );

        int length = AEGetDescDataSize( &resultData );
        {
            AEGetDescData( &resultData, result, length < 4096 ? length : 4096 );
            s = QString::fromUtf8( (char*)&result, length );

            // Strip surrounding quotes
            if ( s.startsWith( "\"" ) && s.endsWith( "\"" ) )
            {
                s = s.mid( 1, s.length() - 2 );
            }

            // iTunes sometimes gives us strings with null terminators which
            // fucks things up if we don't remove them.
            while ( s.endsWith( QChar( QChar::Null ) ) )
            {
                s.chop( 1 );
            }

            // It also escapes quotes so convert those too
            s.replace( "\\\"", "\"" );
        }
    }
    else
    {
        // AppleScript not responding
        return false;
    }

//     qDebug() << "Executing script done";
    if ( resultID != kOSANullScript )
        OSADispose( theComponent, resultID );

    resultToken = s;
    return true;
}

barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Postby barnacleboy » Thu Mar 29, 2007 4:30 pm

works! put typeChar instead of typeUTF8Text in compile function! thx! :grin:

User avatar
The_Tick
Cocoaforge Admin
Posts: 4642
Joined: Thu Dec 02, 2004 6:06 am
Contact:

Postby The_Tick » Thu Mar 29, 2007 5:18 pm

If you have a blog, be sure to post about this. I'm sure someone else will end up wanting to do the same thing eventually.

barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Postby barnacleboy » Thu Mar 29, 2007 6:28 pm

i would do, but i still have a problem - and i guess the situation is the same like before.

for test reasons i tried this script bgannin posted. i copied a song title from itunes and pasted the text into the dialog so that i get the code string out of it. then i put the encoded string («data utxt...» as Unicode text) into my selfwritten script generator and it worked fine with japanese and cyrillic signs. but now i've inserted the generated metadata into the script instead of the static encoded string i got from bgannin's script and it's the same problem like before... :cry:

now i've tried to convert the incoming data from mac roman to unicode, generating the script and convert the whole script to mac roman before compiling and executing... still not working...

barnacleboy
Harmless
Posts: 11
Joined: Tue Mar 27, 2007 1:23 pm

Postby barnacleboy » Fri Mar 30, 2007 3:11 pm

problem solved now. REALLY! :grin:

i wrote an own function which converts the special chars to hex:

Code: Select all

QString
convertUnicodeToHex( QString string )
{   
   QString conversionReturn = QChar( 0x00AB );
   conversionReturn += "data utxt";
   
   for( int i = 0; i < string.length(); i++ )
   {
      QChar singleChar = string[i];
      QString hexadecimal = QString( "%1" ).arg( (int)singleChar.unicode(), 0, 16 );
      if( hexadecimal.length() == 1 ) hexadecimal.prepend("000");
      if( hexadecimal.length() == 2 ) hexadecimal.prepend("00");
      if( hexadecimal.length() == 3 ) hexadecimal.prepend("0");
      
      conversionReturn += hexadecimal;
   }
   
   return conversionReturn + QChar( 0x00BB ) + " as unicode text";
}


Return to “Growl”

Who is online

Users browsing this forum: No registered users