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



所以我有一个脚本问题(找不到与itunes结合使用的特定应用程序)。

我可以对Python中的任何脚本做这样的事情,比如说我有歌曲标题:

我的心跳-原始混音

我想把它改成我的心跳(原始混音)

在Python中,我会:

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

简单吧?但我想在itunes(在mac上)中批量处理我标记为的曲目

有什么建议吗?

Thx

尝试:

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

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

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 & ")"

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

使用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
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

最新更新