Hello Alan, Detlef, Bart, John, Ronald and others,

Thank you very much for your interest in my problem.

This issue of processing StyledText data is a long story for me.  Years 
ago, I first was interested in it when I downloaded and studied a Frontier 
suite named "StyledText", written by John (Baxter).  Then, I told about the 
structure of 'styl' resource in a Japanese mailing list -- and the friend 
of mine who wrote the script that I posted in my previous message (that I 
have modified slightly) became interested in it.  He (his name is 
"Nowral-san") wrote that script along with some other scripts dealing with 
'styl' resource:

See for example:
http://member.nifty.ne.jp/Nowral/32_Style/32_Style.html#GetStyl
   (this is the script I posted the other day [with some modification])
http://member.nifty.ne.jp/Nowral/31_Unicode/Uni2Multi.html
   (this is a converter script from Unicode multilingual text to StyledText Mac
    multilingual text: an example of making StyledText file...)

There are also some OSAX which read and write StyledText (Text X and Text 
Jobs, included in the package of a scriptable editor "QuoEdit":
http://hyperarchive.lcs.mit.edu/HyperArchive/Abstracts/text/HyperArchive.htm 
l, looking for "QuoEdit").  I wrote some scripts using these OSAX for 
converting Asian diacritical font data into Unicode:
http://www.bekkoame.ne.jp/~n-iyanag/researchTools/diacriticalfontsandunicode 
.html
and
http://www.bekkoame.ne.jp/~n-iyanag/researchTools/for_nisus_writer.sit.hqx

--------------

Now, I find the script by Alan (or the modified script by Detlef) very 
useful.  I wrote in my message:

> If I could write all this in MacJPerl, I would be very glad.

And Alan wrote:

> Maybe it is not possible to acquire the 'styl' record (safely) by purely
> MacPerl methods. It is certainly possible to extract it (safely) with
> AppleScript  but the origin of this thread was a request for a way of
> doing it with MacPerl that _didn't_ involve Applescript.

This is true.  But now, I think that a combination of an AppleScript (or 
Frontier) script and a MacPerl script would be very useful also,  because 
this way, we would not need to bring MacPerl to the front (otherwise, if it 
is a clipboard data, we need to bring MacPerl to the front to make:
     use Mac::LowMem;
     my $lmh = LMGetScrapHandle;
     my $str = $lmh->get;
work as expected).

On the other hand, if there is a way to pass "styl" and "TEXT" data to 
MacPerl as @ARGV data, we would be able to write an AppleScript script 
looking like this:

set stxt to ""
tell application "Style"
        set stxt to selection of front document as styled text
end tell

set stxtRecord to stxt as record
set txt to <<class ktxt>> of stxtRecord
set styl to <<class ksty>> of stxtRecord

tell application "MacJPerl"
        activate
        set res to Do Script {"my_read_StyledText.pl", txt, styl} mode Batch
end tell
res

==============

On the other hand, it would be very useful if one could make easily 
StyledText data in MacPerl, that could be "pasted" to the clipboard -- 
either from within MacPerl itself
[that would be something like...:

    use Mac::LowMem;

    my $lmh = LMSetScrapHandle;
    my $stxt = $lmh->set;
]

or as data to return to the AppleScript script, which would put it in the 
clipboard
[with something like:

tell application "MacJPerl"
        activate
        set res to Do Script {"my_read_StyledText.pl", txt, styl} mode Batch
end tell
set the clipboard to res
]

(Note that Nowral-san developed a method to make 'styl' data, and put it in 
a file as 'styl' resource: see an example in his script "Uni2Multi")

============

Thank you again for your interest in this issue, and for your scripts.

And thank you in advance for any further ideas.

Best regards,

Nobumi Iyanaga
Tokyo,
Japan

P.S.
I tried to write a combination of AppleScript script and MacPerl script, 
which makes a hash of style data with the corresponding text data.

My AppleScript script is:

set stxt to the clipboard as styled text

set stxtRecord to stxt as record
set txt to <<class ktxt>> of stxtRecord
set stl to <<class ksty>> of stxtRecord

set fref to open for access file "Macintosh HD:Desktop Folder:styldata" 
with write permission
try
        write stl to fref
on error
        close access fref
end try
close access fref

tell application "MacJPerl"
        set res to Do Script {"Macintosh HD:Read 
StyledText:readStylTest3.pl", txt} mode Batch
end tell
res

and my MacPerl script is:

#!perl -w
use Mac::Fonts;

my ($buf, $nsty, $ofs, $is, $styl1, $strt, $high, $asct, $font, $face1, 
$delim);
my ($size, $r, $g, $b, $face, $fontname, $text, $str, $i);
my (@offsets);

open (IN, "Macintosh HD:Desktop Folder:styldata") || die;

while (<IN>) {
        $buf .= $_;
}

close (IN);

$text = $ARGV[0];

$nsty = unpack("n", substr($buf, 0, 2));
print "# styles : $nsty\n";
print "offset\thi\tas\tfontID\tfont_name\tstyle\tsize\t(R,\tG,\tB)
----------------------------------------------------------------------
";
for($ofs=2, $is=0; $is<$nsty; ++$is, $ofs+=20) {
        $styl1 = substr($buf, $ofs, 20);
        ($strt, $high, $asct, $font, $face1, $delim, $size, $r, $g, $b)
                        = unpack("NnnnB8Cnnnn", $styl1);
        $face = "";
        foreach(qw{B I U O S C E R}) { $face .= $_ if chop($face1); }
        $face = $face ? $face : 'Plain';

        $fontname = GetFontName($font);

        $str = "a";
        $styles{$is} = {text => $str, offset => $strt, high => $high, as => 
$asct, fID => $font,
                fname => $fontname, style => $face, size => $size, r => $r, 
g => $g, b => $b, style => $styl1};

        push @offsets, $strt;

        #print 
"$strt\t$high\t$asct\t$font\t$fontname\t$face\t$size\t$r\t$g\t$b\n";
}

for $i (0 .. ($#offsets - 1)) {
        $str = substr ($text, $offsets[$i], ($offsets[$i + 1] - $offsets[$i]));
        $styles{$i}{text} = $str;
}
$styles{$#offsets}{text} = substr ($text, $offsets[$#offsets]);

for $i (0 .. $#offsets) {
        print "$styles{$i}{text}, $styles{$i}{fname}\n";
}

Reply via email to