Sunday, April 18, 2021

#1 2021-03-21 02:56:47 pm

old-hell
Member
Registered: 2021-03-11
Posts: 3

Unable to change Text or String to double quote

Hi, hope someone can help me out. I'm not able to use this

"\","

command. It always Returns

\",

but I only need

",

This is the script I'm using:

Applescript:


set csvLines to read "/tmp/sample.csv"

set recs to paragraphs of csvLines

-- getting values for each record
set vals to {}
set AppleScript's text item delimiters to "','"

repeat with i from 1 to length of recs
   set end of vals to text items of (item i of recs)
end repeat

--return vals as string
set the message_text to ¬
   vals
set the message_text to replace_chars(message_text, "','", "\",")

return message_text

Offline

 

#2 2021-03-22 01:23:20 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1752

Re: Unable to change Text or String to double quote

Hi,

Your vals is list of strings as I understand. So, to get some string (text) of this list you should get some item of this list:

Applescript:


set vals to {"SomeText", "OtherText", "\","} ---- here "\"," is 3rd text of vals

repeat with i from 1 to count vals
   set message_text to item i of vals
end repeat
-- The result of last command --> ",

Last edited by KniazidisR (2021-03-22 01:28:33 am)


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

Offline

 

#3 2021-03-22 01:48:31 am

old-hell
Member
Registered: 2021-03-11
Posts: 3

Re: Unable to change Text or String to double quote

This is what I want:

I'm having a .csv file with the following entries:

name1, surname1
name2, surname2

I want to put the data to a list where the list entries are: {"name1, surname1","name2,surname2"}

but my problem is, that my list looks like that:
{{"name1, surname1"}, {"name2, surname2"}, {}}

So, I need to replace "}, {" to ","

This ist the code I'm using for that:

Applescript:

set csvLines to read "/tmp/sample.csv"
set recs to paragraphs of csvLines

set vals to {}
set AppleScript's text item delimiters to "','"

repeat with i from 1 to length of recs
   set end of vals to text items of (item i of recs)
end repeat

return vals

Offline

 

#4 2021-03-22 02:45:41 am

KniazidisR
Member
From:: Greece
Registered: 2019-03-03
Posts: 1752

Re: Unable to change Text or String to double quote

Applescript:


set vals to paragraphs 1 thru -2 of (read "/tmp/sample.csv")

I tested using what you provided to me:

Applescript:


set csvLines to "name1, surname1
name2, surname2
"

set vals to paragraphs 1 thru -2 of csvLines

Last edited by KniazidisR (2021-03-22 03:00:53 am)


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

Offline

 

#5 2021-03-22 03:41:57 am

old-hell
Member
Registered: 2021-03-11
Posts: 3

Re: Unable to change Text or String to double quote

Wow, just one line.

Thank you so much that's what I was looking for :-)

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)