Ich habe eine .txt-Datei auf meinem Computer gespeichert. Der Inhalt der .txt-Datei sieht beispielsweise so aus:
Ich möchte, dass meine Automator-Anwendung die erste Zeile der TXT-Datei liest und diese Zeile in eine neue Variable einfügt. Damit kann ich überprüfen, ob diese Variable mit einer anderen Variable übereinstimmt.
Mein ultimatives Ziel ist es, dass meine Anwendung das heutige Datum automatisch an den Anfang der Datei schreibt und eine zusätzliche Leerzeile unter dem heutigen Datum hinzufügt. Aber ich will natürlich nur, dass das passiert, wenn das heutige Datum nicht schon aktenkundig ist.
Hier ist der Code, den ich bisher habe:
# Part 1
# Get and format today's date.
set TodayDate to current date
set y to text -4 thru -1 of ("0000" & (year of TodayDate))
set m to text -2 thru -1 of ("00" & ((month of TodayDate) as integer))
set d to text -2 thru -1 of ("00" & (day of TodayDate))
set FormattedTodayDate to y & "_" & m & "_" & d & " -"
# Part 2
# Get the first line of the .txt file.
set Target_Filepath to quoted form of "/Users/Me/Desktop/Documents/My Fruit Log.txt"
set FirstLineOfTxtFile to <this is where I need your help, Ask Different>
# Part 3
# Check to see if the first line of the .txt file is today's date.
set TodayDateAlreadyWritten to false
if FirstLineOfTxtFile contains FormattedTodayDate then
set TodayDateAlreadyWritten to true
end if
# Part 4
# Write today's date to the first line of the .txt file (if necessary).
if TodayDateAlreadyWritten = false then
set TextToWrite to FormattedTodayDate & "
"
set OriginalText to quoted form of (do shell script "cat " & Target_Filepath)
set TextToWrite to quoted form of TextToWrite & "\n" & OriginalText
do shell script "echo " & TextToWrite & " > " & Target_Filepath
end if
Es ist Teil 2 , wo ich Hilfe benötige.
Ich habe möglicherweise einige Fehler in einem der Teile des obigen Codes gemacht (aber meines Wissens keine), also fühlen Sie sich bitte frei, mich zu korrigieren, wenn Sie es für richtig halten.
Das sind meine Quellen:
Teil 1: Kurze Datumsangaben in AppleScript formatieren
Teil 4: Wie kann man Textdateien in AppleScript voranstellen?
0D
Okay, ich glaube, ich habe den Fehler in Bezug auf die Verwendung cat
im do shell script
Befehl eingegrenzt und den Code so geändert , dass er keine Wagenrückläufe einführt , zumindest bisher beim Testen nur dieses Codes, der unten dargestellt ist. Ich müsste weitere Tests durchführen, um explizit zu sehen, wo der Fehler liegt, aber ich habe ihn neu codiert, um einen do shell script
Befehl so zu verwenden, dass er in die Datei schreibt, ohne Wagenrückläufe einzuführen .
do shell script
Ich habe jedoch die erste Umschreibung von Teil 4, die den Befehl verwendet, auskommentiert, da er zwar keine Wagenrückläufe einführt, jedoch bei jeder Ausführung eine leere Zeile am Ende der Zieldatei hinzufügt, und obwohl er nicht schwerwiegend ist, bin ich mir dennoch nicht sicher, ob Sie es tun. Ich möchte, dass es passiert. Also habe ich einen alternativen Weg hinzugefügt, der keinen do shell script
Befehl verwendet .
Beachten Sie, dass ich für meine Variablen die CamelCase-Namenskonvention bevorzuge, also habe ich den gesamten Code neu geschrieben und zusätzlichen Code und Kommentare hinzugefügt , wie ich sie bevorzuge. Tut mir leid, wenn Ihnen das Unannehmlichkeiten bereitet, aber ich musste es auf eine Weise tun, die es mir ermöglicht, alle Probleme effektiv zu lösen. Fühlen Sie sich frei, nach Bedarf/Wunsch zu ändern.
Der folgende Code funktioniert mit der Zieldatei, unabhängig davon, ob sie anfänglich ASCII-Textinhalt enthält oder nicht, und ich habe auf meinem System nach mehreren Schreibvorgängen überprüft, dass keine Wagenrückläufe eingeführt wurden und die ursprüngliche Zieldatei zuerst überprüft wurde, ob leer oder nicht, hatte keine Im Vergleich zu anderen Codeversionen, die dieses Problem verursachten, wurden zu keinem Zeitpunkt Wagenrückläufe und keine Zeilenvorschübe konvertiert .
-- # 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
open for access
die Datei aufgetreten ist, aber nicht von geschlossen wurde close access ...
. Das heißt, bevor Sie meinen neuen Code ausgeführt haben, müssen Sie ausgeführt haben open for access
und das Dateihandle (Referenznummer) wurde nie geschlossen. Vielleicht aus dem anderen Code in der anderen Frage. Ich weiß, dass mein gegenwärtiger Code solide ist!+ 86400 * 2
der set todaysDate to (current date)
Zeile hinzufügen und den Multiplikator nach jeder *
Ausführung erhöhen, und es fügt der Datei das Datum des nächsten Tages hinzu, wie es soll. Sie müssen das Dateihandle schließen, um mit dem Zieldateinamen fortzufahren, oder den Dateinamen zum Testen ein wenig ändern, bis Sie das Dateihandle für diese benannte Datei schließen.Hallo, für Teil 2, ein Skript-Shell-Skript "head -n 1" ermöglicht es Ihnen, den Wert (Text) von Zeile 1 Ihrer Datei Ihrer Variablen FirstLineOfTxtFile zuzuweisen und dann Ihren Text wie gewünscht zu formatieren.
set TodayDate to current date
set y to text -4 thru -1 of ("0000" & (year of TodayDate))
set m to text -2 thru -1 of ("00" & ((month of TodayDate) as integer))
set d to text -2 thru -1 of ("00" & (day of TodayDate))
set FormattedTodayDate to y & "_" & m & "_" & d & " -"
set Target_Filepath to quoted form of ""/Users/Me/Desktop/Documents/My Fruit Log.txt""
set FirstLineOfTxtFile to do shell script "head -n 1 '"/Users/Me/Desktop/Documents/My Fruit Log.txt"'"
set TodayDateAlreadyWritten to false
if FirstLineOfTxtFile contains FormattedTodayDate then
set TodayDateAlreadyWritten to true
end if
# Part 4
# Write today's date to the first line of the .txt file (if necessary).
if TodayDateAlreadyWritten = false then
set TextToWrite to FormattedTodayDate & "
"
set OriginalText to quoted form of (do shell script "cat " & Target_Filepath)
set TextToWrite to quoted form of TextToWrite & "
" & OriginalText
do shell script "echo " & TextToWrite & " > " & Target_Filepath
end if
Benutzer3439894