Applescript:循环中的文本定界
Applescript : text delimitation in a loop
我正在尝试从一个值的所有出现的日志文件中获取值
记录在以下键上:
> <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
但是我怎样才能得到这个 time/data 的其他出现?而且不止第一个
这应该会给你方向。它未经测试(我在移动设备上),但如果没有拼写错误应该可以使用。
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
我正在尝试从一个值的所有出现的日志文件中获取值 记录在以下键上:
> <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
但是我怎样才能得到这个 time/data 的其他出现?而且不止第一个
这应该会给你方向。它未经测试(我在移动设备上),但如果没有拼写错误应该可以使用。
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