I need to search it looking for a match to folderName only. I then need to either delete the line if found or/and write to the eof if it doesn’t exist.
Any help would be appreciated.
Thanks.
Model: PowerMac
Browser: Safari 416.13
Operating System: Mac OS X (10.4)
property myFile : quoted form of POSIX path of ((path to desktop folder as Unicode text) & "Your File.txt")
display dialog "Search for this folder name:" default answer ""
set searchString to text returned of result
try
do shell script "/usr/bin/grep -i -m 1 " & quoted form of (searchString & space) & space & myFile
try
do shell script "/usr/bin/grep -iv " & quoted form of (searchString & space) & space & myFile & " | tee " & myFile
display dialog "Deleted "" & searchString & ""." buttons {"OK"} default button 1
on error errorMsg number errorNum
if errorNum is not 1 then error errorMsg number errorNum
end try
on error errorMsg number errorNum
if errorNum is not 1 then
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons {"Cancel"} default button 1 with icon caution
return false
else
display dialog """ & searchString & "" was not found." buttons {"Cancel", "Create Entry"} default button 2
set newEntry to searchString
repeat with thisItem in {"User Name", "Password"}
display dialog (thisItem & " for "" & searchString & "":") default answer ""
set newEntry to newEntry & space & (text returned of result)
end repeat
try
do shell script "/bin/echo " & quoted form of newEntry & " >> " & myFile
display dialog "Entry created successfully." buttons {"OK"} default button 1
on error errorMsg number errorNum
display dialog "Error (" & errorNum & "):" & return & return & errorMsg buttons {"Cancel"} default button 1 with icon caution
end try
end if
end try
Note: this script assumes the text file ends with a blank line.
Here’s an example using the standard additions read/write commands:
set f to choose file
set rec_delim to ASCII character 10
set the_rec to “folderName2 UserName Password”
set search_word to word 1 of the_rec
set ref_num to open for access f with write permission
try
set t to read ref_num
– the pointer is after eof
set byte_offset to offset of search_word in t
if byte_offset is not 0 then – found. so delete record
– the following line will error if record is at end and we’re done
read ref_num from byte_offset until rec_delim – move pointer to next paragraph
set temp_text to read ref_num – read from next paragraph
set eof ref_num to (byte_offset - 1) – delete from found paragraph
write temp_text to ref_num
end if
– the pointer is after eof
write the_rec to ref_num
write rec_delim to ref_num
close access ref_num
on error err_mess – error on read file
close access ref_num
– optional message
display dialog err_mess
–
end try
read f
Note that I used your example text and pasted into TextEdit. The saved file contains the line feed character as record delimiters. If you write your text using the standard addition, then you need to use carriage ‘return’ instead.
I just realized that if the file is blank (no records), then it will error at the first read and no record will be written. A quick fix until I think about it more would be to place the first read in another try block and initializ the offset:
set byte_offset to 0
try
set t to read ref_num
– the pointer is after eof
set byte_offset to offset of search_word in t
end try