KevinCork KevinCork - 1 month ago 14
AppleScript Question

AppleScript count text assurance

My script is saving texts

set dataBackupFile to (path to desktop folder as text) & "Note_Backup.txt"


It's time I run the script, this added the date

set myDate to date string of (current date)
set myTime to time string of (current date)

set myDateTime to "--------------------------------------------------------------
" & myDate & " - " & myTime & "


Result :


Wednesday 9 November 2016 -


How can I get AppleScript to count how many time this date is listed on the text file, so I can added to the title

E.g :
Wednesday 9 November 2016 - (2)

My script :

set myDate to date string of (current date)
set filetocheck to (path to desktop folder as text) & "Note_Backup.txt"
set counter to (count text items of myDate) of filetocheck


But the result is 25, while the real result on the files is 38.

The text to count is in this format (Wednesday 9 November 2016):




Wednesday 9 November 2016 -- 10:10:54 - 1 (-41)

0000 other data here

Answer

You can do it with text item delimiters, replace linefeed with return if the line delimiter characters are \r

set {TID, text item delimiters} to {text item delimiters, "--" & linefeed & date string of (current date)}
set counter to (count text items of myDateTime) - 1
set text item delimiters to TID

counter will contain the number of occurrences

To check your text file use

set myDate to date string of (current date)
set filetocheck to (path to desktop folder as text) & "Note_Backup.txt"
set theText to read file filetocheck
set {TID, text item delimiters} to {text item delimiters, "--" & linefeed & date string of (current date)}
set counter to (count text items of theText) - 1
set text item delimiters to TID