• 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: Snippet to remove an item from a list
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Snippet to remove an item from a list


  • Subject: Re: Snippet to remove an item from a list
  • From: Arthur J Knapp <email@hidden>
  • Date: Mon, 09 Jul 2001 10:24:25 -0400

> Date: Sun, 8 Jul 2001 05:27:05 -0400
> From: Victor Yee <email@hidden>
> Subject: Snippet to remove an item from a list

> Don't know if this one's been done before, but here's a snippet to remove an
> item from a list:
>
> on deleFromList(thisListRef, thisIndex)
> if (count of thisListRef) < thisIndex then
> error "Can't remove non-existent item."
> end if
> repeat with i from thisIndex to 2 by -1
> set item i of thisListRef to item (i - 1) of thisListRef

This repeat loop is the "programatic" way to do this, but in
vanilla AppleScript, it can be quite slow.

> end repeat
> set contents of thisListRef to rest of thisListRef
> end deleFromList

Here is the more traditional AppleScript technique:

on DeleteAt(lst, x)
if (x = 1) then
return lst's rest
else if (x = lst's length) then
return lst's items 1 thru -2
else
return (lst's items 1 thru (x - 1)) & ,
(lst's items (x + 1) thru -1)
end if
end DeleteAt

set oldList to {1, 2, 3, 4, 5}
set newList to DeleteAt(oldList, 3)
-- > {1, 2, 4, 5}


Another approch is the Class-Delete method. It has the
advantage of easily allowing for a negative index to
delete, but the disadvantage that every item in the list
must be of the same class, and your script has to know
which class that is:

-- Class-Delete technique

set list_of_strings to {"a", "b", "c", "d", "e"}

set not_a_string to 0

set item 3 of list_of_strings to not_a_string

set list_of_strings to every string of list_of_strings
-- > {"a", "b", "d", "e"}



Arthur J. Knapp
http://www.stellarvisions.com
mailto:email@hidden

Hey, check out:
http://www.eremita.demon.co.uk/scripting/applescript/


  • Follow-Ups:
    • Apple events dictionary or documentation?
      • From: "Joseph A. Weaks" <email@hidden>
  • Prev by Date: OE Hotmail account selection
  • Next by Date: What to AS on an app?
  • Previous by thread: Re: QuarkXPress: selection spans multiple pages
  • Next by thread: Apple events dictionary or documentation?
  • Index(es):
    • Date
    • Thread