Saturday, July 4, 2020

#1 2020-04-22 12:32:12 pm

hsp
Member
Registered: 2012-07-14
Posts: 29

script not working as excepted as before

Hello,

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:

Applescript:

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

If someone can help, this will be great.

Thanks and regards
Henri

Offline

 

#2 2020-04-22 01:02:21 pm

robertfern
Member
Registered: 2011-11-29
Posts: 63

Re: script not working as excepted as before

Here is the fixed version

Applescript:


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

Offline

 

#3 2020-04-22 01:08:16 pm

hsp
Member
Registered: 2012-07-14
Posts: 29

Re: script not working as excepted as before

Hi Robert,

Hundred of thanks.

Regards
Henri

Offline

 

#4 2020-04-24 12:14:00 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: script not working as excepted as before

The main problem is solved, but the code may be enhanced somewhat:

1) No need loop with index i and refer to item i of myArray. It is better loop directly with items of myArray
2) Coercing destFolder as text is better be done outside of repeat loop.
3) file the thisFile is simply file thisFile. Getting file descriptor need only once, and not 4 times.
4) No need 2 write commands.
5) 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.
6) No need destPath variable. Put the expression directly in the handler's call.

Applescript:


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

Last edited by KniazidisR (2020-04-24 12:45:14 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)