Read data from txt file into a variable then delete a part of the data

Hi all,
by looking on the site I have found two scripts that I would like to merge into one : the first allows me to read data from a txt file and put them into a variable, the second allows you to delete part of the data stored into a variable.
My goal is to have a single script to load data from txt file into the variable, search for the entered text, delete it and get the list of the remaining text.

The first :


set myFile to "test.txt"
set mypathtodesktop to path to desktop
set myfilepath to mypathtodesktop & myFile as string
set theData to paragraphs of (read file myfilepath)

set searchValue to text returned of (display dialog "Inserisci il nome da cercare" default answer "")

repeat with i in theData
	if i contains searchValue then
		display dialog contents of i
	end if
end repeat

and the second :


set a to "My Name is Alessio and I am a total applescript noob"
set myReplace to text returned of (display dialog "Insert the word(s) you want to delete :" default answer "")
set a to simpleReplace(myReplace, "", a)

on simpleReplace(search, replace, subject)
	local search, replace, subject, ASTID
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to search
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to ASTID
	
	return subject
end simpleReplace

The data in the txt file should be this way :
Alessio Archive
Alessio Photo
Alessio Software
Alessio Video
Tom Archive
Tom Software
Jenny Photo
Jenny Video

For example: by looking “Alessio” I would like to get a list that contains “Archive, Photo, Software, Video”, by looking “Jenny” I would like to get a list that contains "
Photo, Video".

I did some tests but I can not understand how to proceed.
As always, any help is welcome.

In the post where I found the second script (http://macscripter.net/viewtopic.php?id=22497) there is also an “almost” solution to my question. “Almost” because I can delete the entered text but I get a list that also includes other data that I do not want.

The script is :


set searchValue to text returned of (display dialog "Insert the word(s) you want to delete" default answer "") & space
set theFile to alias ((path to desktop as Unicode text) & "test.txt")
set del_list to (read theFile)

set a to del_list
set a to simpleReplace(searchValue, "", a)

on simpleReplace(search, replace, subject)
	local search, replace, subject, ASTID
	
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to search
	set subject to text items of subject
	
	set AppleScript's text item delimiters to replace
	set subject to "" & subject
	set AppleScript's text item delimiters to ASTID
	
	return subject
end simpleReplace

The data in the variable (obtained from the txt file) are :
Alessio Archive
Alessio Photo
Alessio Software
Alessio Video
Tom Archive
Tom Software
Jenny Photo
Jenny Video

and the ones I get from this script by searching for "Tom " are :
Alessio Archive
Alessio Photo
Alessio Software
Alessio Video
Archive
Software
Jenny Photo
Jenny Video

while I would get :
Archive
Software

that is, only the text next to "Tom ".

Hi,

try this, the script expects the text file test.txt on desktop.
The result is in the variable resultList


set theFile to ((path to desktop as text) & "test.txt")
set theData to paragraphs of (read file theFile)

set searchValue to text returned of (display dialog "Inserisci il nome da cercare" default answer "")
if searchValue is "" then return
set resultList to {}
set searchValueLength to (length of searchValue) + 2
repeat with aParagraph in theData
	if aParagraph starts with searchValue then
		set end of resultList to text searchValueLength thru -1 of aParagraph
	end if
end repeat

Here is one way to do it. Note that we replace "Alessio " (space after name) because it is in the first position of each item that contains it in the list. Otherwise, a trim function would need to be added to clean the altered text item.


tell application "Finder"
	activate
	set dataFile to (choose file with prompt "Select the tab separated text file...") as text
	set dataRecords to read file dataFile using delimiter {return}
	set cleanedList to {}
	repeat with aRecord in dataRecords
		set theText to aRecord as text
		set theText to my replaceAll(theText, "Alessio ", "")
		set end of cleanedList to theText
	end repeat
end tell

on replaceAll(theText, findText, replaceText)
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, findText}
	set tempText to text items of theText
	set AppleScript's text item delimiters to replaceText
	set theText to tempText as string
	set AppleScript's text item delimiters to atid
	return theText
end replaceAll

Hi StefanK and haolesurferdude and thanks for your fast replies.

@StefanK
With your script I get almost the result that I need. For example, if I delete “Alessio” the result is “ArchivioFotografieManualiSoftwareVideo” with no space between the words, while I would need a list like :
Archives
Photographs
Manuals
Software
Video.

This is because I would like to be able to use each word to make selections.

@haolesurferdude
With your script I get the same result as the script that I posted above, that is in addition to the data sought appear even those that do not contain “Alessio”. But the result would be what I need.

my script returns an applescript list, not a string containing paragraphs.
Now the variable resultText contains one string

set theFile to ((path to desktop as text) & "test.txt")
set theData to paragraphs of (read file theFile)

set searchValue to text returned of (display dialog "Inserisci il nome da cercare" default answer "")
if searchValue is "" then return
set resultList to {}
set searchValueLength to (length of searchValue) + 2
repeat with aParagraph in theData
	if aParagraph starts with searchValue then
		set end of resultList to text searchValueLength thru -1 of aParagraph
	end if
end repeat
set {TID, text item delimiters} to {text item delimiters, return}
set resultText to resultList as text
set text item delimiters to TID

Stefan, you’re right, you had written very clearly, my fault that in the rush to try the script I did not pay attention.
Once again thank you for the solution.

PS You speak Italian?

un po’
(a bit)

Stefan’s solution is much more elegant than mine. However, I couldn’t help myself and came up with another way to achieve your goals:


tell application "Finder"
	activate
	set dataFile to (choose file with prompt "Select the tab separated text file...") as text
	set dataRecords to read file dataFile using delimiter {return}
	set cleanedList to {}
	repeat with aRecord in dataRecords
		set theText to aRecord as text
		if my isIn(theText, "Alessio ") then
			set theText to my replaceAll(theText, "Alessio ", "")
			set end of cleanedList to theText
		end if
	end repeat
end tell

on replaceAll(theText, findText, replaceText)
	set {atid, AppleScript's text item delimiters} to {AppleScript's text item delimiters, findText}
	set tempText to text items of theText
	set AppleScript's text item delimiters to replaceText
	set theText to tempText as string
	set AppleScript's text item delimiters to atid
	return theText
end replaceAll

on isIn(theText, findThis)
	set theDelims to AppleScript's text item delimiters
	set AppleScript's text item delimiters to the findThis
	set the itemList to every text item of theText
	set theCount to the number of items of itemList
	if theCount > 1 then
		set theResult to true
	else
		set theResult to false
	end if
	return theResult
end isIn