I once again appeal to the brilliant minds of the forum. Thanks again for all the help thus far.
Here’s my current dilemma. My Applescript Studio app generates a text log file that looks something like this:
The data1 fields will often be the same, though data3 will change each time a new line of text is generated. I’m trying to figure out the best way to search for the LAST instance of a text string that equals data1 and return the cooresponding data3 of the same line of text. What’s the best way? How do I find the last instance of a string? And then would I use word+2 to get data3? Any help much appreciated.
set myData to "data1a" & tab & "data2a" & tab & "data3a" & return & "data1b" & tab & "data2b" & tab & "data3b" & return & "data1c" & tab & "data2c" & tab & "data3c" & return
set DPR to reverse of paragraphs of myData
set tid to AppleScript's text item delimiters
repeat with P in items 2 thru -1 of DPR -- skips last item which is ""
set AppleScript's text item delimiters to tab
set lastD1 to first text item of P -- could be second or third
set AppleScript's text item delimiters to tid
exit repeat
end repeat
lastD1
set myData to "data1a" & tab & "data2a" & tab & "data3a" & return & "data1b" & tab & "data2b" & tab & "data3b" & return & "data1c" & tab & "data2c" & tab & "data3c" & return
set DPR to reverse of paragraphs of myData
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
set lastD1 to first text item of (second item of DPR) -- first is ""
set AppleScript's text item delimiters to tid
lastD1
Sorry for being a newbie, but how to I set myData to be the text file? Also, I simplified my data a bit for the example, but since I’m having a bit of trouble following your script and expanding it out, let me give an exact example:
I want to search for the last instance of Bob201 (data2) and set a variable to (data4) on the same text line, in this case “10”.
Sorry I wasn’t more precise in my original post. Hopefully this might help some people learn this method as well though. It looks very useful, and doesn’t even require Text Edit?? Thanks.
Exactly, yes, ALL the data is subject to change. I want to be able to search for the last instance of BOB201, and get the 4th piece of data from that line, though the next line might be George333. Sorry I was unclear to start with…
set myData to "551" & tab & "Bob201" & tab & "887" & tab & "9" & tab & "6" & return & "551" & tab & "Bob201" & tab & "887" & tab & "10" & tab & "7" & return & "437" & tab & "George333" & tab & "843" & tab & "2" & tab & "12" & return -- you'll read yours
set DPR to reverse of paragraphs of myData -- you would replace myData with read alias "path here" and skip first line above
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
repeat with P in items 2 thru -1 of DPR -- skips last item which is ""
if text item 2 of P is "Bob201" then
set FoundIt to text item 4 of P
exit repeat
end if
end repeat
set AppleScript's text item delimiters to tid
FoundIt
It seems to be having trouble reading my text file. Will this work if some of the data entries have spaces in them? They are separated by tabs, but they may have spaces within them. Is there a way to make it work even if that is the case? Thanks again…
If you created the log with TextEdit without saving it as text (i.e. it’s log.rtf), it will be full of rich text format stuff, so no go. Otherwise, try “read alias “full path here” as Unicode text” instead of just a plain read. Then modify the code by including that as shown in the example below which does have entries with spaces in them.
set myData to ("551" & tab & "Bob 201" & tab & "887" & tab & "9" & tab & "6" & return & "551" & tab & "Bob 201" & tab & "887" & tab & "10" & tab & "7" & return & "437" & tab & "George 333" & tab & "843" & tab & "2" & tab & "12" & return) as Unicode text -- you'll read yours
set FindIt to "Bob 201"
set DPR to reverse of paragraphs of myData -- you would replace myData with [b]read alias "path here" as Unicode text[/b] and skip first line above
set FoundIt to ""
set tid to AppleScript's text item delimiters
set AppleScript's text item delimiters to tab
repeat with P in items 2 thru -1 of DPR -- skips last item which is ""
if text item 2 of P is FindIt as Unicode text then
set FoundIt to text item 4 of P
exit repeat
end if
end repeat
set AppleScript's text item delimiters to tid
if "FoundIt" is "" then
display dialog "Didn't find the term"
else
FoundIt
end if
If in doubt about the log file path then put: set myStuff to read posix file " - drag the file here - " as Unicode text
do nothing else to test the read variable to see what you got.
PS: the normal way to write a file is to build the data by concatination using set myLog to myLog & new_data and then write it to a file thus:
set f to open for access (path_to_my_file_folder as text) & "myLog.txt" with write permission
try
write myLog to f as Unicode text -- (or without this, plain ascii text)
on error
close access f -- so it won't be left hanging open and you can't delete it.
end try
close access f
(* you don't have to open it to read it.
If you want to start afresh, put "set eof of f to 0" before the write statement. That erases anything already there.
If you don't set eof, then every time you write to the file (while it's open) it will be appended to what's already there.
*)
Worked like a charm, I just needed to add “as Unicode text” to my read command. What’s interesting is if I alter the text files at all, even if I save them back out as plain text files, this script doesn’t work anymore. I actually had the same problem with Excel, I couldn’t link to any external data files that I’d altered with text Edit. What program can I use to edit the text files that my Applescript Studio app creates without making them unreadable to Excel, or another script? I don’t plan to do this often, it’s just useful when you’re trying to troubleshoot. Thanks again for all the help.
-Evan
PS: Could you explain the “repeat with P in items 2 thru -1 of DPR line?” What is item -1? I think I uderstand the rest of it.
AppleScript “understands” counting from either end of a list of any kind. The -1 element is the last, the -2 is the second last, etc. so if we have a list L = {1, 2, 3, 4, 5} and get items 2 thru -1 of L, we’ll have {2, 3, 4}. Apple script also understands end of L, last item of L, beginning of L and rest of L which is {2, 3, 4, 5}; all handy in their place.
With respect to Unicode vs. Plain ASCII text, they aren’t interchangeable - one has to be converted to the other. Unicode uses 2 bytes per letter while ASCII uses only 1. Reading Unicode as ASCII will produce strangeness between every character. Converting ASCII to Unicode is a matter of filling in the other bytes with nulls, but going the other way, the first byte may actually contain data if the text isn’t in the first seven bits of the ASCII table.
Your best bet is to edit and create text in your script, then write it yourself. If you want to use TextEdit to edit or create text then be sure to go to the Format Menu and choose “Make Plain Text” before saving it. Make Plain Text is also Shift-Command-T