使用Regex(或类似)通过脚本更改iTunes中的歌曲名称(或其他标签)

时间:2013-09-03 21:23:16

标签: python string applescript itunes

所以我有一个脚本问题(并且找不到与itunes一起执行此操作的特定应用程序)。

我可以对Python中的任何脚本执行类似的操作,比如说我有歌曲标题:

击败我的心 - 原创混音

我希望改变它以击败我的心(原创混音)

在Python中我会:

string = 'Beat of my heart - original mix'
location = string.index(' - ')
fixed_string = string[,location] + '(' + string[location+3,] + ')'

简单吧? 但我想在我标记为

的轨道上的iTunes中(在Mac上)批量处理

有什么建议吗?

THX

4 个答案:

答案 0 :(得分:1)

尝试:

tell application "iTunes"
    set myTracks to get selection
    repeat with aTrack in myTracks
        set trackName to aTrack's name
        set newTrackName to do shell script "sed 's/ - \\(.*\\)/ (\\1)/' <<< " & quoted form of trackName
        if newTrackName ≠ trackName then set aTrack's name to newTrackName
    end repeat
end tell

答案 1 :(得分:1)

我看到你有一个答案,但是我想指出你可以用与python代码几乎相同的方式解析applescript中的字符串。只需使用“offset”查找文本位置即可。简单吧?

set theString to "Beat of my heart - original mix"
set theLocation to offset of " - " in theString
set fixed_string to text 1 thru theLocation of theString & "(" & text (theLocation + 3) thru end of theString & ")"

注意:如果找不到偏移文本,那么theLocation将为0,因此如果需要,可以在最后一行之前添加if语句。

答案 2 :(得分:0)

使用Applescript,您可以使用Text Item Delimiters

set ASTID to AppleScript's text item delimiters
tell application "iTunes"
    repeat with song in (selection of browser window 1)
        set the_name to name of song
        if the_name contains " - " then
            set AppleScript's text item delimiters to ("- ")
            set the_name to text items of the_name
            set AppleScript's text item delimiters to ("(")
            set the_name to (the_name as string) & ")"
            set name of song to the_name
        end if
    end repeat
    set AppleScript's text item delimiters to ASTID
end tell

答案 3 :(得分:0)

set ASTID to AppleScript's text item delimiters
tell application "iTunes"
    set sel to selection
    if sel is not {} then
        repeat with aTrack in sel
            set the_name to name of aTrack
            if the_name contains " - " then
                set AppleScript's text item delimiters to (" - ")
                set the_name to text items of the_name
                set AppleScript's text item delimiters to (" (")
                set the_name to (the_name as string) & ")"
                set name of aTrack to the_name
            end if
        end repeat
    end if
    set AppleScript's text item delimiters to ASTID
end tell