代码之家  ›  专栏  ›  技术社区  ›  Kevin

applescript:循环中的文本分隔

  •  0
  • Kevin  · 技术社区  · 6 年前

    我正在尝试从一个日志文件中获取一个值的所有出现的值 记录在以下键上:

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

    我可以用下面的代码得到我想要的值的第一个匹配项,但是如何搜索更多的匹配项并添加辅助变量呢?

    ##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
    

    但是,我如何才能得到这个时间/数据的其他事件呢?不仅是第一个

    1 回复  |  直到 6 年前
        1
  •  1
  •   Pat_Morita    6 年前

    这应该给你指明方向。这是未经测试的(我在手机上),但如果没有打字错误应该可以工作。

    property leftEdge1 : "<key>myValueTime</key>"
    property rightEdge1 : "<key>serviceToken</key>"
    
    
    --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"
    
    -- Read file
    set p to "/Users/kevin/Library/Logs/MyAPP/" & myFileName
    set plistfile_path to POSIX file p      
    set newFile to (plistfile_path as text)
    set theSource to read file newFile as text
    
    -- Save text item delimiter
    set saveTID to text item delimiters
    
    -- First get all elements that start with leftEdge1
    set text item delimiters to leftEdge1
    set allContent to every text item of theSource
    set text item delimiters to rightEdge1
    
    set classValues to {}
    
    -- Loop through text items and find content up to rightEdge1
    repeat with singleContent in allContent
        set end of classValues to ((text item 1 of singleContent) as text)
    end repeat
    
    -- Restore text item delimiters
    set text item delimiters to saveTID
    
    classValues