Text item delimiter - Separated by ","

Hi Folks,

I am reading from a text file - inside the text file the text is separated by “,” - how can I seperate the
1st and 2nd item - and how can I replace the “,” by a “space”?


set Apple Script's tet item delimiters to the ","
set the i to 2nd text item of read_data
set AppleScript's text item delimiters to the ","
set the pegel_show to 1st text item of read data
display dialog i
display dialog pegel_show

i is showing the 2nd entry (but all of the entries in that row - there are 4 entries)
pegel_show is showing the first entry (all of the entries in that row - there are 4 entries)

Thanks for your help,

Stefan

Hi Stefan,

try this:

set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","} -- save delimiter value and set the new one
set {pegel_show, i, c, d} to text items of read_data -- assign the 4 text items to separate variables
set AppleScript's text item delimiters to space -- change the delimiter to space
set read_data to {pegel_show, i, c, d} as text -- concatenate the text with space as separator
set AppleScript's text item delimiters to ASTID -- restores the old value
display dialog i
display dialog pegel_show

HI Stefan,

thanks for your help - when I use your code, it has the same result like befor, but whe I add the following, then it is working:



--your code:

set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","} -- save delimiter value and set the new one
set {a,b,c,d,e} to text items of read_data -- assign the 4 text items to separate variables
set AppleScript's text item delimiters to space -- change the delimiter to space
set read_data to {a,b,c,d,e} as text -- concatenate the text with space as separator
set AppleScript's text item delimiters to ASTID -- restores the old value

set AppleScript's text item delimiters to the ","
set the a to 1st text item of read_data
set AppleScript's text item delimiters to the ","
set the b to 2nd text item of read_data
set AppleScript's text item delimiters to the ","
set the c to 3rdtext item of read_data
set AppleScript's text item delimiters to the ","
set the d to 4th text item of read_data
set AppleScript's text item delimiters to the ","
set the e to 5th text item of read_data

display dialog a
display dialog b
display dialog c
display dialog d
display dialog e

How can I later put the a,b,c,d,e variable into the variable i?

Thanks and best Regards,

Stefan

Are you trying to seperate out each item into a variable? or just convert the items seperated by a “,” to being sperated by a " "?

This does the latter

set read_data to "Hello,how,are,you"

set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","}
set x to text items of read_data
set AppleScript's text item delimiters to " "
set i to x as string
set AppleScript's text item delimiters to ASTID

Hi James,

thanks for your help - your code is working fine,
but combining the variables a & b is what I want…

Curious now is the fact that in the popupbutton I get all the content only 1 letter horizontal…


set {ASTID, AppleScript's text item delimiters} to {AppleScript's text item delimiters, ","} -- save delimiter value and set the new one
set {a,b,c,d,e} to text items of read_data -- assign the 4 text items to separate variables
set AppleScript's text item delimiters to space -- change the delimiter to space
set read_data to {a,b,c,d,e} as text -- concatenate the text with space as separator
set AppleScript's text item delimiters to ASTID -- restores the old value

set AppleScript's text item delimiters to the ","
set the a to 1st text item of read_data
set AppleScript's text item delimiters to the ","
set the b to 2nd text item of read_data
set AppleScript's text item delimiters to the ","
set the c to 3rdtext item of read_data
set AppleScript's text item delimiters to the ","
set the d to 4th text item of read_data
set AppleScript's text item delimiters to the ","
set the e to 5th text item of read_data

set i to a & " " & b
repeat with i in read_data
make new menu item at end of menu of popup button "networks" of window "main" with properties {title: i, enabled: true}
end repeat


but this is for the xcode forum…

Best Regards,

Stefan

Stefan, read Adam’s tutorial for using AppleScript’s text item delimiters.

http://macscripter.net/articles/402_0_10_0_C/

LOL I have no idea what we are trying to accomplish here

Hi James, hi Stefan,

thanks for your help! I will try my best to solve this issue, and let you know when I have the wanted results…

Best regards,

Stefan

Reading through this, I don’t find a sample of the text you want to process, or a sample of the result you want to achieve, Stefan. That makes it very difficult (as James notes) to help. What you want to do sounds simple. You should also read Nigel Garvey’s “Ins and Outs of File Read/Write in AppleScript”. Sometimes it is useful to read with a delimiter and that article explains how to do it and its limitations.

HI Adam,

thanks for your hint! I will check through Nigels Article…

my file looks like this:

Austria, One, apn, guest, guest
Germany, T-Mobile, apn, user, pass

and so on!

The goal is to put the first and the second in a dropdown - the rest should be also a variable - and together all of them (1 row) should be written as a variable into a file…

Thanks and best regards,

Stefan

an example

set a to {"Austria, One, apn, guest, guest", "Germany, T-Mobile, apn, user, pass"}
set {TID, text item delimiters} to {text item delimiters, ", "}
set b to text items of item 1 of a --> {"Austria", "One", "apn", "guest", "guest"}
set text item delimiters to space
set c to text items of b as text --> "Austria  One  apn  guest  guest"
set text item delimiters to TID
set {d, e} to b -- if there are more text items than variables, only the first ones are taken
a -- {"Austria, One, apn, guest, guest", "Germany, T-Mobile, apn, user, pass"}
b -- {"Austria", "One", "apn", "guest", "guest"}
c --  "Austria  One  apn  guest  guest"
d -- "Austria"
e -- "One"

or like this (hopefully LOL)

set the_data to "Austria, One, apn, guest, guest
Germany, T-Mobile, apn, user, pass"
set dataSets to {}
set ASTID to AppleScript's text item delimiters
repeat with a_dataSet in (paragraphs of the_data)
	set AppleScript's text item delimiters to ","
	set end of dataSets to text items of a_dataSet
	set AppleScript's text item delimiters to " "
	set end of item -1 of dataSets to item -1 of dataSets as string
end repeat
set AppleScript's text item delimiters to ASTID

based off of the text you need to split the text into a list of lists delimited by returns first then tabs so that your data looks like this: {{“Austria”, " One", " apn", " guest", " guest"}, {“Germany”, " T-Mobile", " apn", " user", " pass"}}

set the_data to "Austria, One, apn, guest, guest
Germany, T-Mobile, apn, user, pass"
set dataSets to {}
set ASTID to AppleScript's text item delimiters
set AppleScript's text item delimiters to "
"
set dataSets to every text item of the_data
set AppleScript's text item delimiters to ","
repeat with i from 1 to count of dataSets
	set item i of dataSets to every text item of item i of dataSets
end repeat
set AppleScript's text item delimiters to ASTID
return dataSets

Hi Folks,

thanks a lot for your help! Now it works perfect…

Thanks a lot…

Best Regards,

Stefan