【问题标题】:programmatically updating iTunes track location以编程方式更新 iTunes 曲目位置
【发布时间】:2011-02-17 10:30:24
【问题描述】:

我想以编程方式修改 iTunes 上曲目的文件系统路径,以便我可以对某些曲目位置(现在存储在文件系统上的不同位置)应用字符串转换。

我尝试使用 AppleScript 更新相关曲目的位置属性,但在调用“将 mytrack 的位置设置为 ...”时出现文件结尾错误

我在网上看到过各种其他的黑客攻击,包括导出整个音轨数据库、在 XML 中修改它,然后重新导入它——但这似乎丢失了太多元数据(例如播放列表)。

【问题讨论】:

    标签: applescript itunes


    【解决方案1】:

    查看更多代码真的很有帮助。特别感兴趣的是您正在使用的值以及它是如何派生的。查看您收到的确切错误消息也很有用(如果您从 Script Editor/ 运行程序,您应该能够从 AppleScript 错误对话框表中复制文本AppleScript 编辑器)。

    file track 类的字典条目显示其location 属性是可写的alias 值。您可能遇到的问题是您没有使用该值的别名。

    以下代码显示了如何使用来自choose file(返回alias)的交互式提示来更改轨道的位置:

    set m to path to music folder
    tell application "iTunes"
        set trk to first item of selection
        set l to location of trk
        if class of l is alias then
            set m to l
        end if
        set {d, a, n} to {database ID, artist, name} of trk
        choose file with prompt "Choose the file to use for " & d & ": " & a & "—" & n default location m
        set location of trk to result
    end tell
    

    choose file 方法不是您想要的,因为您正在执行某种基于字符串的自动路径名转换。

    在 AppleScript 中使用路径名时,您可能会使用两种路径名:POSIX 和 HFS。 POSIX 路径名具有斜杠分隔的组件(并允许在任何组件内使用冒号)。 HFS 路径名有冒号分隔的组件(并且允许在任何组件内使用斜杠),并且它们通常以卷名组件开头。

    要将存储在变量 str 中的 POSIX 路径名转换为 AppleScript alias,请使用以下表达式:

    POSIX file str as alias
    

    要将存储在变量 str 中的 HFS 路径名转换为 AppleScript alias,请使用以下表达式:

    alias str
    

    例如:

    tell application "iTunes"
        set trk to first item of selection
        set l to location of trk
        set newPath to my computeNewPath(POSIX path of l)
        set location of trk to POSIX file newPath as alias
    end tell
    
    to computeNewPath(pp)
        -- presumably something interesting happens here
        return pp
    end computeNewPath
    

    【讨论】:

      【解决方案2】:

      如何在保持 iTunes 库数据库 (iTunes Library.itl) 完整的同时将媒体文件(不是由 iTunes“组织”的)移动到其他位置:

      1. 将文件移动到新位置(例如,mv ~/MyMusic /Volumes/Huge/
      2. 在旧位置创建指向新位置的符号链接
        (ln -s /Volumes/Huge/MyMusic ~/MyMusic)
      3. 启动 iTunes(如果尚未运行)
      4. 选择所有已移动的曲目。
      5. 运行这个 AppleScript:

        -- Mark missing tracks (and update database with real path of existing
        -- tracks) in iTunes
        -- Instructions:
        -- * symlink new file location to old location (old location points to
        --   new location, file exists)
        -- * select tracks to scan/update
        -- * run the script
        -- * missing tracks are marked with (!) and all other track paths have
        --   been updated to the real path (symlinks eliminated) in iTunes
        tell application "iTunes"
            set fx to fixed indexing
            set fixed indexing to true
            set Sel to the selection
        
            repeat with i from 1 to number of items in Sel
                set trk to item i of Sel
                set loc to (get location of trk as text)
            end repeat
        
            set fixed indexing to fx
        end tell
        
      6. 现在所有轨道都应该指向正确的位置,并且可以删除符号链接。这可以通过在已移动的轨道上选择“获取信息”并验证路径是否指向新位置来验证。

      如果您没有获得正确的符号链接,iTunes 将在每个缺失的曲目旁边显示(!)。要解决此问题,只需在旧位置创建一个指向新位置的符号链接,然后再次运行脚本。提示:Get Info 对话框可用于确定丢失曲目的旧位置。

      这适用于 iTunes 11.0.5

      【讨论】:

      • 我不得不添加一行set location of trk to loc,但它工作正常。
      【解决方案3】:

      添加到 millerdev 的先前答案,我更新了脚本以与 MacOS Catalina 和新的音乐应用程序一起使用。只需创建一个 $HOME/Library/Music/Scripts 目录并将其放置在那里。

      tell application "Music"
        set fx to fixed indexing
        set fixed indexing to true
        set Sel to the selection
      
        repeat with i from 1 to number of items in Sel
          set trk to item i of Sel
          set l to location of trk
          set location of trk to l
        end repeat
      
        set fixed indexing to fx
      end tell
      

      【讨论】:

        猜你喜欢
        • 2013-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多