I was wondering, How do you remove an Item from a list. and although it may sound silly, is it possilbe to access a list within a list?
ex: property Data1 : {Info,{Filename, path, datecreated},option}
would it be possible to word it like this set (item 1 of (item 2 of Data1)) to “File_Name”
I really need to know how to remove an item from a list, as silly as that may sound. Any way, any help is greatly appriciated!
You could write a handler that builds a new list out of all the items prior to the one you want to remove and all the items after it.
Or you could use the “edit list” command from the Akua Sweets scripting addition package.
set theList to {"A", "B", "C", {"D1", "D2"}, "E", "F"}
set theList to (edit list theList with edits {-3, -5})
-- {"A", "B", {"D1", "D2"}, "F"}
set item 1 of item 3 of theList to "X"
theList
That’s exactly how it works. (You have to set the variables first, of course.)
–tet
That’s very cool! I added some error checking:
on deleteItemFromStringsList(thelist, itemIndex)
tell thelist
set theCnt to count
if (itemIndex is not equal to 0) and ¬
(itemIndex is not greater than theCnt) and ¬
(itemIndex is not less than (theCnt * -1)) then
set item itemIndex to missing value
strings
else
error "Bad Index"
end if
end tell
end deleteItemFromStringsList
set thelist to {"oneHen", "twoDucks", "threeSquawkingGeese"}
deleteItemFromStringsList(thelist, -3)
Just felt the need to thank the Chefs for what has turned out to be a real feast. Wonderful ingredients, splendidly presented. Thank you.
Andreas
Here’s a vanilla method in case you prefer OS X… This will delete the 3rd item, the Pear:
return deleteFromList(3, {" Lime ", " Grape ", " Pear ", " Apple ", " Orange "})
on deleteFromList(itemOffset, thelist)
try
if itemOffset > 0 then
if itemOffset = 1 then
if (count of thelist) > 1 then
copy items 2 thru -1 of thelist to a
copy {} to b
else
copy {} to a
copy {} to b
end if
else if itemOffset = (count of thelist) then
copy items 1 thru -2 of thelist to a
copy {} to b
else
copy items 1 thru (itemOffset - 1) of thelist to a
copy items (itemOffset + 1) thru -1 of thelist to b
end if
copy a & b to thelist
return thelist
else
return thelist
end if
on error
return thelist
end try
end deleteFromList
Cheers,
Dave L.
: I was wondering, How do you remove an Item from a list.
Two vanilla options:
- For lists containing items all of a known class, you can use the class deletion method:
on deleteItemFromStringsList(theList, itemIndex)
set theList's item itemIndex to missing value
theList's plain text
end deleteItemFromStringsList
set theList to {"George", "Ann", "Mary"}
deleteItemFromStringsList(theList, 2)
- For lists containing items of mixed, or unknown, class, you have to do it manually:
on deleteListItem(theList, itemIndex) -- taken from listLib 2.1 (unreleased)
if theList's class is not list then
error "Not a list." number -1703
else if itemIndex's class is not integer then
error "Index must be an integer." number -1703
end if
if itemIndex is less than 0 then
set itemIndex to (theList's length) + 1 + itemIndex
if itemIndex is less than 1 then error "Index " & itemIndex & " is out of range (list length is " & theList's length & ")." number -1728
else if itemIndex is 0 then
error "Can't get index 0." number -1728
else if itemIndex is greater than theList's length then
error "Index " & itemIndex & " is out of range (list length is " & theList's length & ")." number -1728
end if
script -- speed hack [fractionally slower on short lists; maybe 10-20% faster on long lists]
property _list : theList
end script
tell result
if itemIndex is 1 then
its _list's rest
else if itemIndex is theList's length then
its _list's reverse's rest's reverse
else
its _list's items 1 thru (itemIndex - 1) & its _list's items (itemIndex + 1) thru -1
end if
end tell
end deleteListItem
Both these handlers will work with negative indexes as well as positive ones; the second handler also does plenty error-checking so should be pretty robust.
HTH
has