Random Applescript:修剪空间和返回线

Random Applescript:修剪空间和返回线,random,applescript,text-files,Random,Applescript,Text Files,我编写了一个AppleScript,它从用逗号表示的文本文件中返回一个随机字符串: set some_file to "Macintosh HD:Users:Zade:Library:Application Support:Notational Data:Words.txt" as alias set the_text to read some_file as string set the text item delimiters of AppleScript to ", " set the_l

我编写了一个AppleScript,它从用逗号表示的文本文件中返回一个随机字符串:

set some_file to "Macintosh HD:Users:Zade:Library:Application Support:Notational Data:Words.txt" as alias
set the_text to read some_file as string
set the text item delimiters of AppleScript to ", "
set the_lines to (every text item of the_text)
return some item of the_lines
但我现在有一个文本文件,用新行而不是逗号表示。每行从0-20个空格开始。我只想返回随机行的文本部分。我该怎么做


此外,如果文本部分被纯引号(不是卷曲的)包围,我如何修剪引号?

更改行中的逗号和空格
将AppleScript的文本项分隔符设置为“,”
设置为
返回
。新行(没有双关语)如下所示:

set the text item delimiters of AppleScript to return
至于你的第二个问题,试试这个:

set AppleScript's text item delimiters to ""
return text items 2 thru -2 of some_string
让我们把它放在一起

set some_file to "Macintosh HD:Users:Zade:Library:Application Support:Notational Data:Words.txt" as alias
set the_text to read some_file as string
set the_lines to (every paragraph of the_text)
set this_line to some item of the_lines
if this_line is not "" then
    set AppleScript's text item delimiters to ""
    set this_line to (text items 2 thru -2 of this_line) --remove the quotes
    set AppleScript's text item delimiters to space
    set these_items to (every text item of this_code)
    set the this_line to (item 1 of these_items & this_line)
    set AppleScript's text item delimiters to ""
    return this_line
end if
编辑:你不能阻止程序返回空行,但你可以像这样过滤掉它们

if paragraph i of some_text is not "" then do_something()

以下脚本将处理注释中的附加要求,并包含从字符串开头和结尾修剪字符的语句。它排除从文件中读取的前27行,并将从列表中重复获取和修剪随机行,直到最终结果不为空

set theFile to (choose file)
set theLines to paragraphs 28 thru -1 of (read theFile)

repeat -- forever

    set someLine to some item of theLines

    -- trim characters at the start
    if someLine is not "" then repeat until the first character of someLine is not in {space, tab, quote}
        if (count someLine) is 1 then
            set someLine to ""
            exit repeat
        end if
        set someLine to text 2 thru -1 of someLine
    end repeat

    -- trim characters at the end
    if someLine is not "" then repeat until the last character of someLine is not in {quote}
        if (count someLine) is 1 then
            set someLine to ""
            exit repeat
        end if
        set someLine to text 1 thru -2 of someLine
    end repeat

    if someLine is not "" then exit repeat
end repeat

return someLine

下面是我用来修剪字符串两端的空格的方法

 on trimWhiteSpace(aString)
    if aString is not "" then
        -- setup for no delimiter
        set savedTextItemDelimiters to AppleScript's text item delimiters
        set AppleScript's text item delimiters to ""
        -- start with the tail end by revering the list
        set these_items to reverse of (every text item of aString)
        -- keep peeling off 1st space
        repeat while item 1 of these_items is space
            set these_items to rest of these_items
        end repeat
        -- flip the list, now do the leading characters
        set these_items to reverse of these_items
        repeat while item 1 of these_items is space
            set these_items to rest of these_items
        end repeat
        -- reconstruct the string
        set these_items to these_items as string
        -- restore and return
        set AppleScript's text item delimiters to savedTextItemDelimiters
        return these_items
    end if
end trimWhiteSpace

您还可以修剪字符串中的第一个字符,重复直到第一个字符不是空格(或其他任何字符)。@fireshadow52使用“return”不起作用,但我只是将_行设置为(每个段落)。现在,如何从这个操作中排除文本文件的前27行?此外,如何排除返回空行,而只返回另一行有文本的行?这太棒了,谢谢。如果在我的文本文件中有非标准字符,比如卷曲引号,它会时不时地返回奇怪的符号。有没有一种简单的方法可以过滤/修复这些字符?如果字符位于您已经修剪的末端,您可以复制字符并将其添加到列表中,否则,像shell脚本这样的方法可能会删除所有不需要的字符。