English 中文(简体)
果字:3米空间和回归线
原标题:Applescript: trim spaces and return line

我写了一幅 Apple果大本,从 com马划定的文字档案中随机取回:

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处空间开始。 我只想回到随机线的文字部分。 我如何这样做?

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

最佳回答

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
问题回答

这里指的是我从护卫两端到三角的面积。

 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

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

关于你的第二个问题:

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

www.un.org/esa 你可以阻止该方案返回空线,但你可以把空线过滤出去。

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




相关问题
Weighted random numbers

I m trying to implement a weighted random numbers. I m currently just banging my head against the wall and cannot figure this out. In my project (Hold em hand-ranges, subjective all-in equity ...

Comprehensive information about hash salts

There are a lot of questions about salts and best practices, however most of them simply answer very specific questions about them. I have several questions which feed into one another. Assuming a ...

Generate unique names?

I am working on a php site in which we have to upload images from users.i have to rename that file for preventing conflicts in the name of the image. uniqid(rand(), true); and adding a large random ...

How to get two random records with Django

How do I get two distinct random records using Django? I ve seen questions about how to get one but I need to get two random records and they must differ.

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

热门标签