My goal is to read each line of a text file, have a selection list appear asking which lines I’d like to delete, then write the modified file after deleting.
For example, I’d point the script to ~/Desktop/food_items.txt which would have four lines:
hot dogs
hamburgers
french fries
ice cream
A selection list would appear showing each item… if I highlighted ‘french fries’ and ‘hot dogs’, these lines would be deleted from the text file. The file would be saved so that only ‘hamburgers’ and ‘ice cream’ remain.
Any ideas as to how to achieve this??
Hi,
try this
set filePath to (path to desktop as text) & "food_items.txt"
try
set fileDescriptor to open for access file filePath with write permission
set foodList to paragraphs of (read fileDescriptor as «class utf8»)
set itemsToDelete to choose from list foodList with multiple selections allowed
set itemsToWriteBack to {}
if itemsToDelete is not false then
repeat with anItem in foodList
if contents of anItem is not in itemsToDelete then
set end of itemsToWriteBack to contents of anItem
end if
end repeat
set {TID, text item delimiters} to {text item delimiters, linefeed}
set textToWriteBack to itemsToWriteBack as text
set text item delimiters to TID
set eof fileDescriptor to 0
write textToWriteBack to fileDescriptor as «class utf8»
end if
close access fileDescriptor
on error
try
close access file filePath
end try
end try
if the text is MacRoman encoded remove the two as «class utf8» coercions
Perfect! Thanks so much for the help, this was definitely beyond my scripting skills.