Recent

Author Topic: Running from applescript (bourne shell?) problem  (Read 10194 times)

alex80

  • New Member
  • *
  • Posts: 13
Running from applescript (bourne shell?) problem
« on: January 18, 2009, 06:28:47 pm »
Hi all!

I've a problem and I have not ideas how to solve it - I've googled, read apple guides - but no luck.

I've a fpc/lazarus app (intel). This app is launched from another app.

When I'm running it from terminal (applescript: "do shell script "open PathToMyApp"") or Finder  - it runs well. But when I'm running it from a AppleScript script (do shell script PathToMyApp) it can't read/write uft8 inifile. Let me explain what I mean when I said "it can't" - I've some language ini files in the app folder. Let it be "german.ini". This files are UTF8 encoded.
And I'm reading a language file in following way:

var s : WideString;

s :=DecodeUTF8String(ini.ReadString(...));
write_widestring_to_anotherfile(s);
.

When app is launched from Terminal (or using "open" command) it writes string ok, but when it's launched from applescript without "open" command - it shows only "?" when special characters are present. As another site said (http://forum.soft32.com/mac/Recipes-shell-script-commands-applescript-ftopict38741.html_-

The reason is because Terminal uses the Tsch shell (an enhanced C
shell), and the applescript "do shell script " function uses the
Bourne shell. Slightly different commands and syntax are needed for
each shell.


But I can't figure out why app when lauched from "sh" it can't decode utf8 to widestring correctly.
It's a some sort of mystery?  :o

Anybody has similar problem?

p.s. sorry for my English, I know it's not good  :(



alex80

  • New Member
  • *
  • Posts: 13
Re: Running from applescript (bourne shell?) problem
« Reply #1 on: January 18, 2009, 08:29:33 pm »
Small addition - using do shel script "/bin/tcsh -c 'PathToMyApp'" doesn't help too.
But when I run app from Finder/terminal - it works :(

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Running from applescript (bourne shell?) problem
« Reply #2 on: January 18, 2009, 08:37:16 pm »
What version of FPC, Lazarus and OS X are you using?

There have been some bug reports and other postings related to UTF8 over the past year or so. And it does appear as though there are some bugs there still. For example:

http://bugs.freepascal.org/view.php?id=12198

I'm wondering if the locale is different depending on how the app is launched. Open works just like Finder (see man open), so that makes sense.

Although I get the same results entering locale as I do with do shell script "locale".

It's possible that your app is working correctly - it's just being given the wrong information about locale or something because of different shells?

Thanks.

-Phil

alex80

  • New Member
  • *
  • Posts: 13
Re: Running from applescript (bourne shell?) problem
« Reply #3 on: January 18, 2009, 09:02:36 pm »
What version of FPC, Lazarus and OS X are you using?

I'm running latest fpc-2.2.2-20090118/   lazarus-0.9.27.18323-20090118-i386.


There have been some bug reports and other postings related to UTF8 over the past year or so. And it does appear as though there are some bugs there still. For example:
http://bugs.freepascal.org/view.php?id=12198
I'm wondering if the locale is different depending on how the app is launched. Open works just like Finder (see man open), so that makes sense.
Although I get the same results entering locale as I do with do shell script "locale".
It's possible that your app is working correctly - it's just being given the wrong information about locale or something because of different shells?

Hmm, thank you! It looks like a key for answer.
Here is locale (terminal):
LANG="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_CTYPE="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_ALL=


From AppleScript:
"LANG=
LC_COLLATE=\"C\"
LC_CTYPE=\"C\"
LC_MESSAGES=\"C\"
LC_MONETARY=\"C\"
LC_NUMERIC=\"C\"
LC_TIME=\"C\"
LC_ALL="

alex80

  • New Member
  • *
  • Posts: 13
Re: Running from applescript (bourne shell?) problem
« Reply #4 on: January 18, 2009, 09:57:47 pm »
Small addition - I've just tried to copy cwstrings.pp to cwstrings2.pas in directory with my sources and modify it in following way:

setlocale(LC_ALL,'en_US.UTF8');
instead of
setlocale(LC_ALL,'');

But it did not help. %) Any ideas how to setup locale?
« Last Edit: January 18, 2009, 10:35:58 pm by alex80 »

alex80

  • New Member
  • *
  • Posts: 13
Re: Running from applescript (bourne shell?) problem
« Reply #5 on: January 20, 2009, 11:04:53 pm »
Problem was found and solved.

So the problem is - when lazarus application has no locale information or LANG = "C" (as it happens by default when running on Mac OS Tiger 10.4.*) you cannot assign AnsiString to WideString.
I.e. if you have:

var
  s_a : utf8string;// s_a :AnsiString;
  s_w : WideString;
begin
  s_a := 'Some utf8 string ';
  s_w := s_a;
  // s_w = '????????'
end;


or ever more mystic:


var

  s_w1, s_w2 : WideString;
begin
  s_w1 := 'Some unicode string (now I'm using alternative AnsiToWideString procedure)';
  s_w2 := s_w1;
  // s_w2 = '' (it's empty!)  %)
end;



So I've removed all widestring to widestring assignments (widestring->ansistring), and in unit that writes unicode string to unicode file I placed correct AnsiToWideString proc + NO s_w2 := s_w1; , after the conversion is made I'm writing @s_w1 (as buffer) to file.

Thanks everybody!

Any questions are welcome.

Phil

  • Hero Member
  • *****
  • Posts: 2737
Re: Running from applescript (bourne shell?) problem
« Reply #6 on: January 21, 2009, 04:34:28 am »
Both of your examples work as expected on my OS X 10.4 with "C" locale info, so there must be something else going on.

Thanks.

-Phil

 

TinyPortal © 2005-2018