2012-01-12 48 views
1

我写了返回由逗号划定了一个文本文件,一个随机字符串一个AppleScript: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个空格的任何位置开始。我只想返回随机行的文本部分。我该怎么做呢?另外,如果文本部分被简单(不是卷曲)引号包围,我该如何修剪引号?

回答

3

下面的脚本将处理来自其他要求您的评论,并包括从字符串的开头和结尾删除字符的语句。它不包括从文件中读取的前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 
+0

这真棒,谢谢。如果在我的文本文件中存在非标准字符(如卷曲引号),则每隔一段时间它都会返回奇怪的符号。有没有简单的方法来过滤/修复这些问题? – Zade 2012-01-12 22:26:49

+0

如果角色位于已经剪裁完的端点上,您可以复制这些角色并将它们添加到列表中,否则像删除所有不需要的角色的shell脚本可能就是要走的路。 – 2012-01-12 23:12:47

2

将行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() 
+0

您也可以从字符串修剪的第一个字符,重复操作,直至第一个字符是不是空格(或其他的两端修剪空间)。 – 2012-01-12 05:11:42

+0

@Red_Menace我该如何编码? – Zade 2012-01-12 06:21:36

+0

@ fireshadow52使用“返回”不起作用,但我只是将the_lines设置为(每个段落)。现在我怎么能从这个操作中排除文本文件的前27行? – Zade 2012-01-12 06:22:55

2

以下是我用它来从一个字符串

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