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 个空格开始。我只想返回随机行的文本部分。我该怎么做?
另外,如果文本部分被普通(非卷曲)引号包围,我该如何修剪引号?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
以下脚本将处理您的注释中的附加要求,并包含用于修剪字符串开头和结尾字符的语句。它排除从文件中读取的前 27 行,并将重复从列表中获取和修剪随机行,直到最终结果不为空。
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.
这是我用来修剪字符串两端空格的方法
Here is what I use to trim spaces from both ends of a string
将
set the text item delimiters of AppleScript to ", "
行中的逗号和空格更改为return
。新行(没有双关语)看起来像这样:至于你的第二个问题,试试这个:
让我们把它们放在一起!
编辑:您无法阻止程序返回空行,但您可以像这样过滤掉它们......
Change the comma and space in the line
set the text item delimiters of AppleScript to ", "
toreturn
. The new line (no pun intended) looks like this:As for your second question try this:
Let's put it all together!
EDIT: You can't stop the program from returning empty lines, but you can filter them out like so...