Turn list into applescript array

余生长醉 提交于 2020-05-15 08:36:05

问题


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!


回答1:


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



回答2:


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



回答3:


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"


来源:https://stackoverflow.com/questions/44486999/turn-list-into-applescript-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!