How can I use AppleScript to see if a specific line of a .txt file matches a variable?
Solution 1:
Okay, I believe I've narrowed down the 0D
bug in relation to using cat
in the do shell script
command and have modified the code so it doesn't introduce carriage returns, at least so far in testing just this code presented below. I would have to do further testing to see explicitly where the bug is however I've recoded it to use a do shell script
command in a manner to write to the file without introducing carriage returns.
However I've commented out the first rewriting of Part 4 that uses the do shell script
command because while not introducing carriage returns it does add an empty line to the end of target file each time it's runs and while not fatal nonetheless I'm not sure you'd want it to happen. So, I've added an alternate way not using a do shell script
command.
Note that I prefer to use the camelCase naming convention for my variables, so I've rewritten all of the code adding additional code and comments as I prefer them. Sorry if this inconveniences you however I needed to do it in a manner that enabled me to effectively work through any issues. Feel free to modify as needed/wanted.
The code below works on the target file whether or not it initially contains ASCII Text content and I've verified on my system after multiple writes there are no carriage returns introduced and the original target file was first verified, whether empty or not, had no carriage returns and no line feeds were converted at any time as compared to other versions of code that caused this issue.
-- # Part 1 - Get and format today's date.
set todaysDate to (current date)
set y to text -4 thru -1 of ("0000" & (year of todaysDate))
set m to text -2 thru -1 of ("00" & ((month of todaysDate) as integer))
set d to text -2 thru -1 of ("00" & (day of todaysDate))
set formattedTodaysDate to y & "_" & m & "_" & d & " -" as string
-- # Part 2 - Get the first line of the target file.
set targetFilePathname to (POSIX path of (path to desktop as string) & "My Fruit Log.txt")
-- # Initialize firstLineOfFile variable in case the targetFilePathname file is empty.
set firstLineOfFile to ""
try
-- # The commented line of code below is to be used when defining the actual code
-- # in order to ensure a line feed "\n" is used and not a carriage return "\r".
-- # Note that when compiled, the "\n" is converted to a literal newline
-- # and a commented code line will be shown for all similiar instances.
-- # set firstLineOfFile to first item of (read targetFilePathname using delimiter "\n")
set firstLineOfFile to first item of (read targetFilePathname using delimiter "\n")
end try
-- # Part 3 - Check to see if the first line of the target file is today's date.
set isTodaysDateAlreadyWritten to false
if firstLineOfFile is equal to formattedTodaysDate then
set isTodaysDateAlreadyWritten to true
end if
(*
-- # Part 4 - Write today's date to the first line of the target file, if necessary.
if isTodaysDateAlreadyWritten is equal to false then
-- # set theTextToWrite to formattedTodaysDate & "\n"
set theTextToWrite to formattedTodaysDate & "\n"
set theOriginalText to ""
try
set theOriginalText to (read targetFilePathname) as string
end try
-- # set theTextToWrite to theTextToWrite & "\n" & theOriginalText
set theTextToWrite to theTextToWrite & "\n" & theOriginalText
do shell script "echo " & quoted form of theTextToWrite & " > " & quoted form of targetFilePathname
end if
*)
-- # While the commented out Part 4 above does work by not introducing any carriage returns nonetheless
-- # it does introduce and additional empty line at the end of the target file and therefore will not be used.
-- #
-- # The following Part 4 does not use the do shell script command to make the writes nor does it add extra lines.
-- # Part 4 - Write today's date to the first line of the target file, if necessary.
if isTodaysDateAlreadyWritten is equal to false then
-- # set theTextToWrite to formattedTodaysDate & "\n"
set theTextToWrite to formattedTodaysDate & "\n"
set theOriginalText to ""
try
set theOriginalText to (read targetFilePathname) as string
end try
-- # set theTextToWrite to theTextToWrite & "\n" & theOriginalText
set theTextToWrite to theTextToWrite & "\n" & theOriginalText
try
set referenceNumber to open for access targetFilePathname with write permission
write theTextToWrite to referenceNumber starting at 0
close access referenceNumber
on error eStr number eNum
display dialog eStr & " number " & eNum buttons {"OK"} default button 1 with title "File I/O Error..." with icon caution
try
close access referenceNumber
end try
return
end try
end if