Applescript:循环中的文本定界

时间:2019-03-29 14:27:29

标签: loops text applescript

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

> <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 个答案:

答案 0 :(得分:1)

这应该为您提供指导。它未经测试(在移动设备上即时显示),但如果没有错字也可以使用。

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