Applescript:修剪空格和回车线

发布于 2024-12-26 02:34:04 字数 459 浏览 1 评论 0原文

我编写了一个 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 个空格开始。我只想返回随机行的文本部分。我该怎么做?

另外,如果文本部分被普通(非卷曲)引号包围,我该如何修剪引号?

I wrote an AppleScript that returns a random string from a text file delineated by commas:

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

But I now have a text file delineated by new lines instead of commas. And each line begins with anywhere from 0-20 spaces. I want to return just the text portion of a random line. How do I do this?

Also, if the text portion is surrounded by plain (not curly) quotes, how can I trim the quotes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

鸢与 2025-01-02 02:34:04

以下脚本将处理您的注释中的附加要求,并包含用于修剪字符串开头和结尾字符的语句。它排除从文件中读取的前 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

The following script will handle the additional requirements from your comments, and includes statements to trim characters from the beginning and ending of the string. It excludes the first 27 lines read from the file and will repeatedly get and trim random lines from the list until the final result is not empty.

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
伴随着你 2025-01-02 02:34:04

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

 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

Here is what I use to trim spaces from both ends of a string

 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
度的依靠╰つ 2025-01-02 02:34:04

set the text item delimiters of AppleScript to ", " 行中的逗号和空格更改为 return。新行(没有双关语)看起来像这样:

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()

Change the comma and space in the line set the text item delimiters of AppleScript to ", " to return. The new line (no pun intended) looks like this:

set the text item delimiters of AppleScript to return

As for your second question try this:

set AppleScript's text item delimiters to ""
return text items 2 thru -2 of some_string

Let's put it all together!

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

EDIT: You can't stop the program from returning empty lines, but you can filter them out like so...

if paragraph i of some_text is not "" then do_something()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文