Applescript:在TextWrangler中将多行压缩成一行

时间:2014-03-07 11:53:19

标签: applescript

我正在尝试制作一个将多行压缩到一行的Applescript。

例如

"a
b
c
d"

('a','b','c','d')

字符串在TextWrangler中,我的脚本是

set AppleScript's text item delimiters to {return & linefeed, return, linefeed, character id 8233, character id 8232}

tell application "TextWrangler"
    tell text window 1
        set i to 2
        set selection to "('
"
        repeat while i < 6
            select (insertion point before line i)
            select line the (startLine of the selection)
            copy (contents of the selection) as text to myText
            set newText to text items of myText
            set AppleScript's text item delimiters to {""}
            set newText to newText as text
            select insertion point after line 1

            if i = 2 then
                set selection to newText
            else
                set selection to "','" & newText
            end if
            set i to i + 1
        end repeat

        set selection to "')"
    end tell
end tell

问题是脚本的结果是('a','b','b'),所以我无法删除'new line'字符。如果有人帮助我改进它以使用动态行数,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的事情:

set myString to "a
b
c
d"

set myList to paragraphs of myString

set listCount to count myList
repeat with i from 1 to my listCount
    set item i of myList to quoted form of item i of myList
end repeat

set {TID, text item delimiters} to {text item delimiters, ", "}
set newString to ("(" & myList as text) & ")"
set text item delimiters to TID

return newString

答案 1 :(得分:1)

要从TextWrangler获取文本,您可以使用:

tell application "TextWrangler"
    set myString to text of window 1
end tell

要更新TextWrangler中的文本,请使用:

tell application "TextWrangler"
    set text of window 1 to myString
end tell

因此,您可以围绕adayzdone's code包装这两段代码。

tell application "TextWrangler"
    set myString to text of window 1
end tell

set myList to paragraphs of myString

set listCount to count myList
repeat with i from 1 to my listCount
    set item i of myList to quoted form of item i of myList
end repeat

set {TID, text item delimiters} to {text item delimiters, ", "}
set newString to ("(" & myList as text) & ")"
set text item delimiters to TID

tell application "TextWrangler"
    set text of window 1 to myString
end tell