2

I have an applescript function which processes each item of an array. I'm looking for a way to drop a list like the following into my applescript file:

arrayitem1
arrayitem2
arrayitem3

and to use applescript to automatically format it into the proper applescript array syntax so I end up with this:

set array1 to {"arrayitem1", "arrayitem2", "arrayitem3"}

As you can imagine, for longer arrays it would be a hassle to manually add all the commas and quotations.

Thanks!

6
  • What is the format of the source data? Commented Jun 11, 2017 at 18:22
  • The source data is copied from Google Docs. I'm not sure the format but it is essentially text in the clipboard that I would paste it into the applescript file. If needed the source data could exist as lists in a plain text file. Commented Jun 11, 2017 at 18:33
  • Programming and scripting is a very precise subject. Without knowing the exact format of the source it's hard to help. Commented Jun 11, 2017 at 18:40
  • To further clarify - each array item is a website. So in Google Docs each list item looks something like: website1.com/example1. Essentially I'm trying to figure out how to use applescript to take the list (perhaps as a string?) and add quotations at both ends, as well as turn each line break into: ", " Commented Jun 11, 2017 at 18:48
  • I suggest to use applescript delimiters (in this case may be space ?) and build a list with a loop "repeat for each text item if Source" Commented Jun 11, 2017 at 20:09

3 Answers 3

2
set stringofitems to "
item1
item2
item3
"

set text item delimiters to "
"

set listofitems to text items of stringofitems

repeat with theitem in listofitems

    # stuff to do with each item

end repeat
Sign up to request clarification or add additional context in comments.

Comments

1

If the original file is a line separated text, "paragraphs" can be used to split it. endList has to be defined as list '{}' Now thru repeat, each line of the content in the clipboard will be entered as a new entry into the list endList.

set {var, endList} to {the clipboard, {}}
repeat with x in paragraphs of var
    set endList to endList & x
end repeat

Comments

0

If I understand what all you're wanting, I'd suggest a script where you select and copy the string in your google doc, then run a script that pulls the string from the clipboard, does the changes you want, then puts the result onto the clipboard ready to be pasted where you want.

Here's an example using what you said you wanted.

-- get the string into the script through the clipboard
set theString to the clipboard

-- convert the string into a list, using paragraphs
set theParagraphs to paragraphs of theString

-- convert the list to a string, with ", " between items
set AppleScript's text item delimiters to "\", \""
set theResult to ("\"" & theParagraphs as string) & "\""
set AppleScript's text item delimiters to ""

set the clipboard to theResult
-- "arrayitem1", "arrayitem2", "arrayitem3"

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.