0

I'm trying to get values from a log file of all the occurrence of a value Recorded on the following key :

> <key>MyTime</key>         <string>2019-03-29T08:48:18+0000</string>
>       <key>serviceToken</key>

I can get the first occurrence of the value I want with the following code, but how can I search for more occurrence and add in secondary variable ?

##Getting the file name
set ShortDate to short date string of (current date)
set [dayN, MonthN, YearN] to the words of ShortDate
set myUSDate to YearN & MonthN & dayN as string
set myFileName to "TIME" & myUSDate & ".log"

set p to "/Users/kevin/Library/Logs/MyAPP/" & myFileName
set plistfile_path to POSIX file p

property leftEdge1 : "<key>myValueTime</key>"
property rightEdge1 : "<key>serviceToken</key>"
set myNewCaseNote to ""
set newFile to (plistfile_path as text)
set theSource to read file newFile as text
set theText to Unicode text
try
    set saveTID to text item delimiters
    set text item delimiters to leftEdge1
    set classValue to text item 2 of theSource
    set text item delimiters to rightEdge1
    set myCaseNote to text item 1 of classValue
    set text item delimiters to saveTID
    myCaseNote
end try

But how can I get other occurrence of this time/data ? And not only the first one

  • A .log file is usually a plain text file. That looks like an XML/property list - are the time keys all the same? – red_menace Mar 29 at 14:57
  • My first tried was using the same snipped I use for plist ( to value of property list item "myValueTime"), but that's didn't work. The log file seems to be XML, I'm looking for to get all the different value of the keys name "myValueTime" the expected result is a date (different for each keys) – Kevin Mar 29 at 15:21
1

An XML file can’t have identical keys at the same level, so I’m going to guess that it is just a plain text file with those entries. Text item delimiters can be used for everything, but it would tend to add a bit more code, since you would need to get a list of items using the first delimiters, then use additional delimiters on those items, and so on. In this case, you can use them to just break the text apart at the desired key, then get the text from between the first set of tags by getting their offsets, for example:

property keyValue : "<key>myValueTime</key>"
property startTag : "<string>"
property stopTag : "</string>"

set theSource to "<key>myValueTime</key>        <string>2019-03-28T08:48:18+0000</string>
<key>serviceToken</key>
<key>myValueTime</key>
  <string>2019-03-29T09:48:18+0000</string>
<key>serviceToken</key>
  <string>whatever</string>"

set saveTID to text item delimiters
set text item delimiters to keyValue
set classValues to text items of theSource
set text item delimiters to saveTID

set myCaseNotes to {}
repeat with anItem in classValues
  set here to (offset of startTag in anItem)
  set there to (offset of stopTag in anItem)
  if here is not 0 and there is not 0 then
    set the end of myCaseNotes to text (here + (count startTag)) thru (there - 1) of anItem
  end if
end repeat

return myCaseNotes

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .