MACSCRPT Archives

November 2006

MACSCRPT@LISTSERV.DARTMOUTH.EDU

Options: Use Monospaced Font
Show Text Part by Default
Show All Mail Headers

Message: [<< First] [< Prev] [Next >] [Last >>]
Topic: [<< First] [< Prev] [Next >] [Last >>]
Author: [<< First] [< Prev] [Next >] [Last >>]

Print Reply
Subject:
From:
Paul Berkowitz <[log in to unmask]>
Reply To:
Macintosh Scripting Systems <[log in to unmask]>
Date:
Sat, 18 Nov 2006 09:29:05 -0800
Content-Type:
text/plain
Parts/Attachments:
text/plain (115 lines)
On 11/18/06 4:41 AM, "has" <[log in to unmask]> wrote:

> Paul Berkowitz wrote:
> 
> Won't bother replying to all of this post as I had my fill of vanilla
> AS algorithms long ago, and it's one of the reasons I eventually
> moved to another language.

I wasn't asking for vanilla AS logarithms. What I was asking was if someone
who knows shell script and other Unix languages like Perl, Python etc. could
supply me with such a method for getting the line numbers of found text.
Please re-read my original message. You might be just one of those people,
it seems to me.

>Re. the last bit though:
> 
> 
>> Maybe someone even knows another list osax that does
>> it? (I've checked has's TextCommands too.)
> 
> TextCommands, as the name implies, supplies commands for processing
> text. :)

Well, that's what I'm actually doing. Please check the Perl script. I guess
I should ask on a Perl mailing list or forum. It just seems there are quite
a few people here who know it, so I tried here first. I would be accessing
it from 'do shell script', yes, but that would be the only AppleEvent
involved. (I'd guess that, under the hood, all the methods that act on whole
lists or multiline texts like the perl script and the LBS osax command
'difference', really do their own, but very fast loops, so I guess I should
try a loop in Perl, if I knew how.) The Python version, which is slower, and
fussier to call via do shell script, goes like this:


on PythonScript(list1, list2) -- find which items of list1 are not in list2
    
    set lf to (ASCII character 10) -- move to top of script as property?
    
    set AppleScript's text item delimiters to {lf}
    
    --will also coerce numbers to string
    set scpt to {"python -c '", "l1 = \"\"\"" & list1 & "\"\"\".split()",
"l2 = \"\"\"" & list2 & "\"\"\".split()", "for i in l1:", "    if  i not in
l2:", "        print i", "'"} as string
    set AppleScript's text item delimiters to {""}
    
    set string3 to (do shell script scpt) -- result a string with CR
delimiters

    set list3 to paragraphs of string3
    
    return list3 -- list of strings, even if original lists were numbers
    
end PythonScript


That gets me the 'difference' items. I'd also like to get the line numbers
of those items in the l2 text. I'm sure there's a way. This Python version
is not as fast as the Perl grep which acts on the whole set of lines at once
(although it probably loops under the surface). But the perl grep can't get
the line numbers. I'm sure there's a way to loop it in Perl too to get
those, and it's likely to be faster than Python's, and they're both
undoubtedly faster than the vanilla AS way that Emmanuel suggested. However,
since the final list3 will usually be pretty small, I think that Emmanuel's
method will work very well for me. I still wouldn't mind comparing to
alternative methods, however.
> 
> If you wanted you could easily implement a ListCommands FBA using the
> same sfba framework. For example, the implementation for a 'find
> item' command would be:
> 
> from aemreceive.sfba import *
> 
> def finditem(itm, lst):
>      try:
>          return lst.index(itm) + 1
>      except ValueError: # item not in list
>          raise EventHandlerError(-1728)
> 
> installeventhandler(
>          finditem,
>          'LiCoFind',
>          ('----', 'itm', kAE.typeWildCard)
>          ('In  ', 'lst', kAE.typeAEList)
>          )
> 
> starteventloop()
> 
> 
> Add an sdef and a setup script, then build as a standalone
> application with a user-installed Python framework. There's a
> tutorial included in the py-appscript documentation.

Not what I'm looking for, but thanks anyway.
> 
> That said, OSAXen and FBAs aren't ideal for list and record
> processing as all data must be packed into Apple events in order to
> be passed to them. This limits their usefulness since 1. all data is
> copied, so if your list contains sub-lists/records then those will be
> duplicated too (which is not always what you want), and 2. not all
> data is copyable (references to other applications can't be copied at
> all, and copying script objects is almost always a disaster). Packing
> large lists was also impossibly slow in Jaguar and earlier, though
> that's not such an issue nowadays since most Mac users will be
> running Panther or later. But you pays your money and takes your choice.
> 
I'm not getting my choice here. And everyone will be using Panther or later,
so that's OK. (I omitted alternative routines above that covered the "bad
old days" when trying to get 'text items' numbering more than  about 4000
wouldn't work, for example. Those days are thankfully gone.)


-- 
Paul Berkowitz

ATOM RSS1 RSS2