【问题标题】:Sort string by values in text file using applescript使用applescript按文本文件中的值对字符串进行排序
【发布时间】:2015-07-04 21:45:44
【问题描述】:

我正在尝试构建一个苹果脚本来对 iTunes 中音乐文件的流派字段中的标签进行排序。

我在流派字段中使用分号分隔的列表,如下所示:Rock;艺术摇滚;女歌手

我要排序的字符串在我的脚本中被命名为genreList:

{"Art Rock", "Female Vocalists", "Rock"}

我的首选标签顺序列为 sortList:

{"Rock", "Dance", "Art Rock", "New Wave", "Female Vocalists", "Male Vocalists"}

我希望genreList中的项目按照sortList排序,像这样:

{"Rock", "Art Rock", "Female Vocalists"}

如何使用第二个列表对第一个列表进行排序?我在此处和 Google 上进行了搜索,但由于我是脚本新手,我什至不确定我是否在搜索正确的术语。

【问题讨论】:

    标签: string sorting applescript text-files itunes


    【解决方案1】:

    试试这个,在 iTunes 中选择一首或多首曲目并运行脚本。

    如果分隔符应该是分号+空格,请更改第二个属性行。

    如果流派属性不包含分隔符,则不会更改任何内容。

    如果一个流派与排序列表中的项目不匹配,它将被附加到最后。

    property sortedGenreList : {"Rock", "Dance", "Art Rock", "New Wave", "Female Vocalists", "Male Vocalists"}
    property separator : ";"
    
    tell application "iTunes" to set selectedItems to (get selection)
    if selectedItems is {} then
        display dialog "Nothing selected" buttons {"Cancel"} default button 1
    end if
    
    set {TID, text item delimiters} to {text item delimiters, separator}
    repeat with aTrack in selectedItems
        set orderedGenreList to {}
        set nonOrderableGenres to {}
        tell application "iTunes" to set trackGenreList to text items of (get genre of aTrack)
        if (count trackGenreList) > 1 then
            repeat with aGenre in trackGenreList
                if sortedGenreList does not contain aGenre then set end of nonOrderableGenres to contents of aGenre
            end repeat
            repeat with aGenre in sortedGenreList
                if trackGenreList contains aGenre then
                    set end of orderedGenreList to contents of aGenre
                end if
            end repeat
            set orderedGenres to (orderedGenreList & nonOrderableGenres) as text
            tell application "iTunes" to set genre of aTrack to orderedGenres
        end if
    end repeat
    set text item delimiters to TID
    

    如果您想使用每行具有流派的文本文件,请将第一个属性行替换为

    set sortedGenreList to paragraphs of (read (choose file))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-25
      • 1970-01-01
      • 2019-05-03
      • 1970-01-01
      • 2018-04-02
      • 1970-01-01
      • 2020-04-10
      • 2021-08-02
      相关资源
      最近更新 更多