• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: POSIX and lists question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: POSIX and lists question


  • Subject: Re: POSIX and lists question
  • From: "Mark J. Reed" <email@hidden>
  • Date: Wed, 10 Aug 2005 23:17:47 -0400

> ------------------
>
> set theFiles to {}
> repeat with currPath in thePaths
>      set end of theFiles to POSIX file currPath
> end repeat
>
> ------------------
>
> If you have no further use for the original paths after conversion,
> you might even consider converting each item of the list in place,
> thus avoiding the need to build a new list altogether. (Here, let's
> assume that the incoming list is already called 'theFiles', rather
> than 'thePaths'):
>
> ------------------
>
> repeat with n from 1 to count theFiles
>      set item n of theFiles to POSIX file (item n of theFiles)
> end repeat
>
> ------------------
>
> ---
> kai
>

There are any number of add-ons that provide sophisticated list-handling, er, handlers for AppleScript.  I use a home-grown one (the better to learn the language), which lets me do this:

on POSIXize(pathString)
    return POSIX file pathString
end POSIXize

set thePaths to map(theFiles, POSIXize)

Or I could use the apply() function to modify the list in place:

apply(theFiles, POSIXize)

Yeah, I know, mixing metaphors with Perl and Lisp, but I don't know how to write a single handler that will honor changes made within the passed-in closure.  There might be a way with references, but it's beyond my as-yet meager AppleScript skillz.  It'd also be nice if there were a syntax to create an anonymous closure on the fly so I wouldn't have to define a named handler like POSIXize . . .

This is the source code to the functions in question . . . . improvements welcome:

on map(L, func)
    script mapper
        property block: func
        on map(L)
            set newList to {}
            repeat with eachItem in L
                set end of newList to block(eachItem)
            end repeat
            return newList
        end map
    end script
    return mapper's map(L)
end map

on apply(L, func)
    script applicator
        property block: func
        on apply(L)
            repeat with i from 1 to count L
                set item i of L to block(item i of L)
            end repeat
            return L
        end apply
    end script
    return applicator's apply(L)
end apply

--
Mark J. Reed <email@hidden>

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >POSIX and lists question (From: "aaron spesard" <email@hidden>)
 >Re: POSIX and lists question (From: kai <email@hidden>)

  • Prev by Date: Re: POSIX and lists question
  • Next by Date: MS Entourage to Text file
  • Previous by thread: Re: POSIX and lists question
  • Next by thread: Re: POSIX and lists question
  • Index(es):
    • Date
    • Thread