2016-07-04 34 views
0

我可以将歌曲列表分为两个单独的列表:一个歌曲标题是三个或更少的词,一个是四个词或更多的歌?我想要一个列表(作为变量)并对其执行一个操作,然后在另一个列表上单独执行一个操作。在Applescript中获得一定长度的段落(然后对这些段落做些什么)

这里的,据我得到:

tell application "TextEdit" 
activate 
tell the front document 
    set my_list to every paragraph whose (count of words) is 2 
end tell 
end tell 

我似乎无法得到的单词数的属性在每个段落的工作 - 我收到错误消息:“文字编辑得到了一个错误:无法将2671转换为类型引用。“


这可能有助于解释什么,我接下来要做。

我拿到名单

set MySongs to paragraphs of my_list 
-- read artist names (separated by newlines) from the file 

的段落,然后将它们添加到iTunes播放列表:

tell application "iTunes" 
repeat with AnItem in MySongs -- get all tracks from each artist 
    set AnItem to (contents of AnItem) 
    if AnItem is not "" then try -- don't bother with empty names 
     set MyTracks to (location of file tracks of playlist "Music" whose name is AnItem) 
     --can also modify the above from "is" to "contains" or "_begins with_" 
     add MyTracks to NewPlaylist 
    on error errmess -- oopsie (not found, etc) 
     log errmess -- just log it 
    end try 
end repeat 
end tell 

然后做类似的事情与第二个列表。

因此:是否可以从文本文档中获取小于4个单词的段落,并将其转换为以这种方式传递的变量?

感谢

塔迪

回答

1

这里是什么,我觉得你要完成的例子。

set mySampleText to "this is 
my sample text data, lets see what happens if we 
have different 
length paragraphs" 

set paras to paragraphs of mySampleText 
set list1 to {} 
set list2 to {} 
repeat with apara in paras 
    set apara to apara as string 
    if (count of words in apara) is less than or equal to 2 then 
     set end of list1 to apara 
    else 
     set end of list2 to apara 
    end if 
end repeat 

在这个脚本的最后,您将有两个列表:列表1和列表2(3个或更多的词项)(2个字以下的项目)。

在附注上,我建议不要使用脚本编辑TextEdit。真的没有理由这么做。您可以将read文本文件直接放入您的脚本并使用它。

例如...

set mySampleText to read file "Macintosh SSD:path:to:file.txt"