I was using this script since long, but now, the first letter of the second line is truncated.
The script takes a .CSV file and split every line as a single file for which the first column is used for the file name, the second /which is the same as the first column) and following as part of the file, however, the third column is truncated, the first letter is missing.
The .CSV is like this:
“Proverbea”,“Proverbea”,“Ils peuvent tuer toutes les hirondelles ils n’empêcheront pas la venue du printemps”
“Proverbeb”,“Proverbeb”,“L’amour ne pleure jamais comme pleure le sang”
“Proverbec”,“Proverbec”,“La nuit est destinée au sommeil le jour au repos et l’âne au travail”
“Proverbed”,“Proverbed”,“Qui a des éléphants doit avoir de grandes portes”
“Proverbee”,“Proverbee”,“Si la chance est avec toi pourquoi te hâter ? Si la chance n’est pas avec toi pourquoi te hâter”
“Proverbef”,“Proverbef”,“Si le vin est gratuit même le juge le boit”
“Proverbeg”,“Proverbeg”,“Aimons naître, aimons vivre aimons mourir: le néant n’existe pas”
“Proverbeh”,“Proverbeh”,“Au bout de la patience il y a le ciel”
“Proverbei”,“Proverbei”,“Au chef il faut des hommes et aux hommes un chef”
The script is:
set destFolder to choose folder with prompt "Please select the Destinationfolder"
set myArray to paragraphs of (read (choose file with prompt "Please choose the CSV-File") as «class utf8»)
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "\",\""
repeat with i from 1 to count of myArray
set fName to text 1 thru -1 of (text item 2 of (item i of myArray))
set destPath to (destFolder as text) & fName & ".txt"
set myContent to text 1 thru -1 of (text item 2 of (item i of myArray))
set myContent2 to text 2 thru -2 of (text item 3 of (item i of myArray))
my write2file(destPath, myContent, myContent2)
end repeat
set AppleScript's text item delimiters to TID
on write2file(thisFile, thisText, thisText2)
try
open for access file the thisFile with write permission
write (thisText) & return & linefeed & return & linefeed as «class utf8» to file the thisFile starting at eof
write (thisText2) as «class utf8» to file the thisFile starting at eof
close access file the thisFile
on error
try
close access file the thisFile
end try
end try
end write2file
set destFolder to choose folder with prompt "Please select the Destinationfolder"
set myArray to paragraphs of (read (choose file with prompt "Please choose the CSV-File") as «class utf8»)
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to quote & "," & quote -- "\",\""
repeat with i from 1 to count of myArray
set fName to (text item 2 of (item i of myArray)) -- dont need (text 1 thru -1 of)
set destPath to (destFolder as text) & fName & ".txt"
set myContent to (text item 2 of (item i of myArray)) -- dont need (text 1 thru -1 of)
set myContent2 to text 1 thru -2 of (text item 3 of (item i of myArray)) -- text 2 thru -2 is whats causing the problem
my write2file(destPath, myContent, myContent2)
end repeat
set AppleScript's text item delimiters to TID
on write2file(thisFile, thisText, thisText2)
try
open for access file the thisFile with write permission
write (thisText) & return & linefeed & return & linefeed as «class utf8» to file the thisFile starting at eof
write (thisText2) as «class utf8» to file the thisFile starting at eof
close access file the thisFile
on error
try
close access file the thisFile
end try
end try
end write2file
The main problem is solved, but the code may be enhanced somewhat:
No need loop with index i and refer to item i of myArray. It is better loop directly with items of myArray
Coercing destFolder as text is better be done outside of repeat loop.
file the thisFile is simply file thisFile. Getting file descriptor need only once, and not 4 times.
No need 2 write commands.
As myContent is same with fName, no need get text item 2 of anItem 2nd time. Set directly to fName. Or, better remove one of them.
No need destPath variable. Put the expression directly in the handler’s call.
set destFolder to (choose folder with prompt "Please select the Destinationfolder") as text
set myArray to paragraphs of (read (choose file with prompt "Please choose the CSV-File") as «class utf8»)
set TID to AppleScript's text item delimiters
set AppleScript's text item delimiters to quote & "," & quote -- "\",\""
repeat with anItem in myArray
set fName to (text item 2 of anItem)
set myContent2 to text 1 thru -2 of (text item 3 of anItem) -- text 2 thru -2 is whats causing the problem
my write2file(destFolder & fName & ".txt", fName, myContent2)
end repeat
set AppleScript's text item delimiters to TID
on write2file(thisFile, thisText, thisText2)
try
set thisFile to open for access file thisFile with write permission
write (thisText & return & linefeed & return & linefeed & thisText2) as «class utf8» to thisFile starting at eof
close access thisFile
on error
try
close access thisFile
end try
end try
end write2file