0

i have an Array with:

set Array to {"AA", "AB", "AC", "BA", "BB", "BC", "CA", "CB", "CC"}

now, when i use

do shell script " echo " & Array & " >> " & PathToFile

then i get the Result without any spaces or something else

AAABACBABBBCCACBCC

when i use

repeat with counter from 1 to length of Array
    do shell script " echo " & item counter of Array & " >> " & PathToFile
end repeat

i get the result, but every entry in a new line:

AA
AB
AC
BA
BB
BC
CA
CB
CC

But what i want is to write the following Line into my File, with seperators and spaces:

"AA"; "AB"; "AC"; "BA"; "BB"; "BC"; "CA"; "CB"; "CC"

what i have to do?

1
  • FYI In AppleScript that is called a list, not an array. Commented Mar 19, 2021 at 12:45

2 Answers 2

1

But what i want is to write the following Line into my File, with separators and spaces:

"AA"; "AB"; "AC"; "BA"; "BB"; "BC"; "CA"; "CB"; "CC"

Example AppleScript code:

set myList to {"AA", "AB", "AC", "BA", "BB", "BC", "CA", "CB", "CC"}
set itemCount to (length of myList)
set myProcessedList to {}

repeat with i from 1 to itemCount
    if i is less than itemCount then
        copy "\"" & item i of myList & "\"; " to end of myProcessedList
    else if i is equal to itemCount then
        copy "\"" & item i of myList & "\"" to end of myProcessedList
    end if
end repeat

set myProcessedListAsString to myProcessedList as string

do shell script "echo " & myProcessedListAsString's quoted form & " > /path/to/file"

The contents of /path/to/file will contain:

"AA"; "AB"; "AC"; "BA"; "BB"; "BC"; "CA"; "CB"; "CC"
Sign up to request clarification or add additional context in comments.

Comments

0

You have a list of strings but you want to save one string, so you have to join the list. This is quite easy with text item delimiters

set theList to {"AA", "AB", "AC", "BA", "BB", "BC", "CA", "CB", "CC"}
set {TID, text item delimiters} to {text item delimiters, "; "}
set theString to theList as text
set text item delimiters to TID

try 
    set fd to open for access PathToFile with write permission
    write theString to fd as «class utf8»
    close access fd
on error
    try
       close access PathToFile
    end try
end try

The result is

AA; AB; AC; BA; BB; BC; CA; CB; CC

I doubt that you really want to have double quotes in the file. However if you do need double quotes use this

set theList to {"AA", "AB", "AC", "BA", "BB", "BC", "CA", "CB", "CC"}
set {TID, text item delimiters} to {text item delimiters, "\"; \""}
set theString to theList as text
set text item delimiters to TID

try 
    set fd to open for access PathToFile with write permission
    write (quote & theString & quote) to fd as «class utf8»
    close access fd
on error
    try
       close access PathToFile
    end try
end try

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.