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

问题描述 投票:3回答:3

我想以编程方式修改iTunes上音轨的文件系统路径,以便可以对某些音轨位置(现在存储在文件系统上的其他位置)进行字符串转换。

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

[我在网上看到了其他各种骇客,涉及到导出整个track db,以XML对其进行修改,然后重新导入-但这似乎丢失了太多的元数据(例如播放列表)。

applescript itunes
3个回答
3
投票
确实有助于查看更多代码。特别令人感兴趣的是您正在使用的价值以及它的产生方式。查看收到的确切错误消息也很有用(如果从

脚本编辑器 / 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进行“整理”的文件移动到另一个位置:

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

  5. 所有轨道现在都应指向正确的位置,并且可以删除符号链接。可以通过选择已移动的轨道上的“获取信息”并验证路径是否指向新位置来进行验证。

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

这在iTunes 11.0.5上有效


0
投票
[通过添加到millerdev的先前答案中,我更新了脚本以与MacOS Catalina和新的Music应用程序一起使用。只需创建$ 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

© www.soinside.com 2019 - 2024. All rights reserved.