Hi,
I’m working with a loop in Applescript. It does replacements–taken from members of an array–to do replacements.
Basically, I’m trying to replace every even numbered item with the odd numbered item behind this. I got the loop working with an array of {“item1”, “item2”, “item3”, “Item4” and so on…} The loop worked fine.
Now, here’s the weird part. I put in an ampersand and the XMlL entity equivalent for part of the the replacement list (the idea is to eventually have a little script that does entity replacements for XML.) I put in t"&" for the first member of the array, and “&” for the second member.
Suddenly, the array doesn’t work. So I choose two innocuous words “horse” and “cow.” These don’t work not, either–it’s as if I jinxed the array someone by changing it.
Anyway, here’s the script as it stands now. The input file (file_a) contains:
cow
cow
cow
item2
item4
item6
item8
item10
The output file (end_file) contains:
cow
cow
cow
item1
item3
item5
item7
item9
(Notice how it changed the numbers, but didn’t change “cow” into a horse
Here is the script. I can’t see, for the life of me, what I did. It worked before. The idea was to–slowly–sub in the XML entities.
Best,
Laurel
set even_odd to {“item1”, “item2”, “item3”, “item4”, “item5”, “item6”, “item7”, “item8”, “item9”, “item10”, “item11”, “item12”, “cow”, “horse”}
set x to 2
set y to 1
do shell script “cp /Users/kschalk/desktop/file_a /Users/kschalk/desktop/hold_file1”
–loop to replace entites
repeat while x ≤ 14
set var1 to item x of even_odd --setting the item to be replaced (character)
set var2 to item y of even_odd --setting the item it will be replaced with (entity)
–loop works on hold_file1 and outputs data to hold_file2
do shell script “sed 's/” & var1 & “/” & var2 & “/g’ /Users/kschalk/desktop/hold_file1 > /Users/kschalk/desktop/hold_file2”
do shell script "cp /Users/kschalk/desktop/hold_file2 /Users/kschalk/desktop/hold_file1"
set x to x + 2
set y to y + 2
--loop copies hold_file2 to hold_file1
end repeat