Object: Strip a repeating date string and pipe from a text file and save the corrected text back to the same file or a create new text file.
I am able to create the date string, strip the characters and return the corrected text/results to AppleScript. I am unable to send/save the the corrected text/results to the same text file or generate a new text file.
Any help is greatly appreciated.
Thanks,
C
do shell script "date '+%m'"
set theMonth to the result
--returns two digit month
do shell script "date '+%d'"
set theDay to the result
--returns two digit day
do shell script "date '+%Y'"
set theYear to the result
--returns four digit year
set theText to read (choose file) -- Choosing a .txt file
set t to theText
set t to tid(t, theMonth & "/" & theDay & "/" & theYear & "| ") -- Text to be stripped
set t to tid(t, "") -- Content to replace stripped text
on tid(input, delim)
set {oldTID, my text item delimiters} to {my text item delimiters, delim}
if class of input is list then
set output to input as text
else
set output to text items of input
end if
set my text item delimiters to oldTID
set output to (output as text)
return output
end tid
Hello.
Here is a handler that takes a hfspath, and I also tossed in a handler for appending In case you want to append. 
set mtext to "This is
a text that should be saved to a file"
set othertext to "
this is some stuff that should be appended"
set mpath to ((path to desktop as text) & "Thefiledemo.text")
writeToFileAsUtf8(mpath, mtext)
appendToFile(mpath, othertext)
on writeToFileAsUtf8(fname, stuff)
local fRef, n, fsz
set fRef to open for access fname with write permission
try
write stuff to fRef as «class utf8» starting at 0
set fsz to get eof fRef
close access fRef
return fsz
on error e number n
try
close access fRef
end try
error e number n
end try
end writeToFileAsUtf8
to appendToFile(fname, stuff)
(*For adding text to the end of a file*)
--returns boolean success (true=success)
--Assumes: theFile is a file path string and file exists.
local theFile_ID
try
-- open the file
open for access (fname as alias) with write permission
-- using "theFile as alias" means the file must already exist.
-- if it doesn't then the attempt to append will fail
copy the result to theFile_ID
-- Get the file length
get eof theFile_ID
copy the result to theFile_eof
-- Write our message
write stuff to theFile_ID starting at (theFile_eof + 1)
-- Close the file
close access theFile_ID
return true
on error
try
close access theFile_ID
end try
return false
end try
end appendToFile
Here is your script, edited and completed.
do shell script "date '+%m/%d/%Y'"
set theDate to the result
--returns the date as mm/dd/yyyy
set originalFile to choose file of type ("public.plain-text") -- Choosing a .txt file
set theText to read originalFile
(*
# The original code behave correctly only if here text item delimiters is ""
set t to theText
set t to tid(t, theDate & "| ") -- Text to be stripped
log t
# here t is already filtered if old TID was ""
# here t is a text object so the next instruction will change nothing
set t to tid(t, "") -- Content to replace stripped text
on tid(input, delim)
set {oldTID, my text item delimiters} to {my text item delimiters, delim}
if class of input is list then
set output to input as text
else
set output to text items of input
end if
set my text item delimiters to oldTID
set output to (output as text)
return output
end tid
*)
set t to my remplace(theText, theDate & "| ", "")
my writeto(originalFile, t, text, false)
my writeto((path to desktop as text) & "newFile.txt", t, text, false)
#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
set l to text items of t
set AppleScript's text item delimiters to d2
set t to "" & l
set AppleScript's text item delimiters to oTIDs
return t
end remplace
#=====
#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
-- targetFile is the path to the file you want to write
-- theData is the data you want in the file.
-- dataType is the data type of theData and it can be text, list, record etc.
-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
try
set targetFile to targetFile as text
set openFile to open for access file targetFile with write permission
if not apendData then set eof of openFile to 0
write theData to openFile starting at eof as dataType
close access openFile
return true
on error
try
close access file targetFile
end try
return false
end try
end writeto
#=====
KOENIG Yvan (VALLAURIS, France) jeudi 3 octobre 2013 16:28:13
McUsrII - That worked! Thanks so much for the help and the quick response!
C