Hello my friends…
How can I make it so that characters after * in a list item are moved to the following list item, yet characters before the * in a list item stay where they are (at the end of the list)?
set x to {"cat", "brick", "broom", "*Dragon's Lair", "lettuce", "*", "salad", "horse", "zebra", "urchin", "Space Ace*", "cattle", "blackjack", "piano"}
set z to 1
set q to {}
repeat with y from 1 to (count items of x)
if characters of item y of x contains "*" then
set item y of x to (switchText of (item y of x) from "*" to "")
set the end of q to items z through (y) of x
set z to y + 1
end if
end repeat
return q
to switchText of t from s to r
set d to text item delimiters
set text item delimiters to s
set t to t's text items
set text item delimiters to r
tell t to set t to beginning & ({""} & rest)
set text item delimiters to d
t
end switchText
in other words:
if I have
{{“dog”,“cat”,“pillow"},{“mushroom”,"tennis”}}
it would look like this:
{{“dog”,“cat”},{“pillow”,“mushroom”,“tennis”}}
thank you!!!
patrick:
Try this out:
set x to {"cat", "brick", "broom", "*Dragon's Lair", "lettuce", "*", "salad", "horse", "zebra", "urchin", "Space Ace*", "cattle", "blackjack", "piano"}
set {master_list, temp_list} to {{}, {}}
global temp_list, master_list
repeat with a from 1 to count x
if item a of x does not contain "*" then
set end of temp_list to item a of x
else
Remove(temp_list, (item a of x))
end if
end repeat
set end of master_list to temp_list
master_list
---------------------------------------
on Remove(ql, aa)
set end of master_list to ql
set temp_list to {}
if length of aa is greater than 1 then
my FixString(aa)
end if
end Remove
----------------------------------
on FixString(ss)
set s_list to {}
set j to every character of ss
repeat with k from 1 to count j
if item k of j is not "*" then
set end of s_list to item k of j
end if
end repeat
set new_string to s_list as string
set end of temp_list to new_string
end FixString
That should return the following master_list:
{{“cat”, “brick”, “broom”}, {“Dragon’s Lair”, “lettuce”}, {“salad”, “horse”, “zebra”, “urchin”}, {“Space Ace”, “cattle”, “blackjack”, “piano”}}
Good Luck
casdvm
patrick:
Wait a minute. I did not read your request fully the first time. I believe that this is more in line with what you were looking for:
set x to {"cat", "brick", "broom", "*Dragon's Lair", "lettuce", "*", "salad", "horse", "zebra", "urchin", "Space Ace*", "cattle", "blackjack", "piano"}
set {master_list, temp_list} to {{}, {}}
global temp_list, master_list
repeat with a from 1 to count x
if item a of x does not contain "*" then
set end of temp_list to item a of x
else
Remove((item a of x))
end if
end repeat
set end of master_list to temp_list
master_list
---------------------------------------
on Remove(aa)
if length of aa is 1 then
set end of master_list to temp_list
set temp_list to {}
else
my FixString(aa)
end if
end Remove
----------------------------------
on FixString(ss)
if character 1 of ss is "*" then
set nss to characters 2 thru -1 of ss as string
set end of master_list to temp_list
set temp_list to {}
set end of temp_list to nss
else
set nss to characters 1 thru -2 of ss as string
set end of temp_list to nss
set end of master_list to temp_list
set temp_list to {}
end if
end FixString
Now, the returned master_list is:
{{“cat”, “brick”, “broom”}, {“Dragon’s Lair”, “lettuce”}, {“salad”, “horse”, “zebra”, “urchin”, “Space Ace”}, {“cattle”, “blackjack”, “piano”}}
casdvm
wonderful! Thank you so much!!!
set theList to {"*cat", "brick", "broom", "*Dragon's Lair", "lettuce*", "*salad", "horse", "*", "*", "zebra", "urchin", "*Space Ace*", "cattle", "blackjack", "piano*"}
set newList to {{}}
set makeEnd to false
repeat with thisItem in theList
set thisItem to contents of thisItem
if makeEnd and last item of newList is not {} then set end of newList to {}
if thisItem is "*" then
set makeEnd to true
else
if first character of thisItem is "*" then
set thisItem to text 2 thru -1 of thisItem
if last item of newList is not {} then set end of newList to {}
end if
set makeEnd to last character of thisItem is "*"
if makeEnd then set thisItem to text 1 thru -2 of thisItem
set end of last item of newList to thisItem
end if
end repeat
newList
--> {{"cat", "brick", "broom"}, {"Dragon's Lair", "lettuce"}, {"salad", "horse"}, {"zebra", "urchin"}, {"Space Ace"}, {"cattle", "blackjack", "piano"}}
interesting…
is it faster to do what you did:
set makeBeginning to first character of thisItem is "*"
if makeBeginning then...
than to do:
if first character of thisItem="*" then...
Or is it the same?
Oops, Sorry! In an earlier version of the script, makeBeginning needed to be used later on. In my fiddling around, I forgot to change it. Thanks! 