Applescript 以编程方式更新iTunes曲目位置

Applescript 以编程方式更新iTunes曲目位置,applescript,itunes,Applescript,Itunes,我想以编程方式修改itunes上曲目的文件系统路径,这样我就可以对一些曲目位置(现在存储在文件系统的不同位置)应用字符串转换 我尝试使用AppleScript更新相关曲目的location属性,但调用“set mytrack's location to…”时出现文件结束错误 我在网上见过其他各种黑客行为,包括导出整个曲目数据库,用XML修改它,然后重新导入它——但这似乎丢失了太多的元数据(如播放列表)。查看更多的代码确实有帮助。特别令人感兴趣的是您正在使用的值及其派生方式。查看您得到的确切错误消

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

我尝试使用AppleScript更新相关曲目的location属性,但调用“set mytrack's location to…”时出现文件结束错误


我在网上见过其他各种黑客行为,包括导出整个曲目数据库,用XML修改它,然后重新导入它——但这似乎丢失了太多的元数据(如播放列表)。

查看更多的代码确实有帮助。特别令人感兴趣的是您正在使用的值及其派生方式。查看您得到的确切错误消息也很有用(如果您是从ScriptEditor运行程序,则应该能够从AppleScript错误对话框中复制文本)/应用程序描述编辑器)

文件跟踪
类的字典条目显示其
位置
属性为可写的
别名
值。您可能遇到的问题是,您没有为值使用别名

下面的代码显示了如何使用
选择文件
(返回
别名
)中的交互式提示来更改曲目的位置:

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

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

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

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

POSIX file str as 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
如何在保持iTunes库数据库(
iTunes library.itl
)完好无损的情况下将媒体文件(并非由iTunes“组织”的)移动到其他位置:

  • 将文件移动到新位置(例如,
    mv~/MyMusic/Volumes/mage/
  • 在旧位置创建指向新位置的符号链接
    ln-s/Volumes/mage/MyMusic~/MyMusic
  • 启动iTunes(如果尚未运行)
  • 选择已移动的所有轨迹
  • 运行此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
    
  • 现在,所有轨迹都应指向正确的位置,并且可以删除符号链接。这可以通过选择“在已移动的轨迹上获取信息”并验证路径是否指向新位置来验证

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


    这在iTunes11.0.5上起到了作用

    在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
    

    我不得不添加一行
    将trk的位置设置为loc
    ,但之后效果很好。