以编程方式更新 iTunes 曲目位置
我想以编程方式修改 iTunes 上曲目的文件系统路径,以便我可以将字符串转换应用于某些曲目位置(现在存储在文件系统上的不同位置)。
我尝试使用 AppleScript 更新相关曲目的位置属性,但在调用“将 mytrack 的位置设置为...”时出现文件结束错误
我在网上看到了各种其他涉及导出整个曲目的黑客行为db,在 XML 中修改它,然后重新导入它 - 但这似乎丢失了太多元数据(例如播放列表)。
I would like to modify the filesystem path for tracks on itunes programmatically, so that I can apply a string transformation to some of the tracks locations (which are now stored in a different places on the filesystem).
I've tried using AppleScript to update the location property of the relevant tracks but I get an end-of-file error when calling "set mytrack's location to ..."
I've seen various other hacks online that involve exporting the entire track db, modifying it in XML, and then reimporting it - but that seems to lose too much metadata (such as playlists).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看更多代码确实会有帮助。特别令人感兴趣的是您正在使用的值以及它是如何得出的。查看收到的确切错误消息也很有用(如果您从脚本编辑器运行程序,您应该能够从 AppleScript 错误对话框中复制文本)/AppleScript 编辑器)。
file track
类的字典条目显示其location
属性是可写的alias
值。您可能遇到的问题是您没有为该值使用别名。以下代码展示了如何使用
选择文件
(返回别名
)中的交互式提示来更改曲目的位置:选择文件
方法但这不是您想要的,因为您正在执行某种自动化的、基于字符串的路径名翻译。在 AppleScript 中使用路径名时,您可能会使用两种路径名:POSIX 和 HFS。 POSIX 路径名具有斜杠分隔的组件(并且允许在任何组件内使用冒号)。 HFS 路径名具有冒号分隔的组件(并且允许在任何组件内使用斜杠),并且它们通常以卷名称组件开头。
要将存储在变量
str
中的 POSIX 路径名转换为 AppleScript别名
,请使用以下表达式:要转换存储在变量
str
中的 HFS 路径名> 对于 AppleScript别名
,请使用以下表达式:例如:
It would really help to see more of your code. Of particular interest is the value you are using and how it is derived. It would also be useful to see the exact error message you get (you should be able to copy the text out of the AppleScript error dialog sheet if you are running the program from Script Editor/AppleScript Editor).
The dictionary entry for the
file track
class shows itslocation
property being a writablealias
value. The problem you are probably running into is that you are not using an alias for the value.The following code shows how one might change a track's location using an interactive prompt from
choose file
(which returns analias
):The
choose file
method is not what you want though, since you are doing some kind of automated, string based pathname translation.When working with pathnames in AppleScript, there are two kinds that you might use: POSIX and HFS. POSIX pathnames have slash delimited components (and allow colons inside any component). HFS pathnames have have colon delimited components (and allow slashes inside any component), and they usually start with a volume name component.
To convert a POSIX pathname stored in a variable
str
to an AppleScriptalias
, use the following expression:To convert an HFS pathname stored in a variable
str
to an AppleScriptalias
, use the following expression:For example:
如何将媒体文件(不是由 iTunes“组织”的)移动到其他位置,同时保持 iTunes 资料库数据库 (
iTunes Library.itl
) 完好无损:>mv ~/MyMusic /Volumes/Huge/
)(
ln -s /Volumes/Huge/MyMusic ~/MyMusic
)运行此 AppleScript:
所有轨道现在都应指向正确的位置,并且可以删除符号链接。这可以通过在已移动的轨道上选择“获取信息”并验证路径是否指向新位置来验证。
如果您没有获得正确的符号链接,iTunes 将在每个丢失的曲目旁边显示
(!)
。要解决此问题,只需在旧位置创建一个指向新位置的符号链接,然后再次运行脚本即可。提示:“获取信息”对话框可用于确定丢失曲目的旧位置。这适用于 iTunes 11.0.5
How to move media files (that are not "organized" by iTunes) to a different location while keeping the iTunes library database (
iTunes Library.itl
) intact:mv ~/MyMusic /Volumes/Huge/
)(
ln -s /Volumes/Huge/MyMusic ~/MyMusic
)Run this AppleScript:
All tracks should now point to the correct location, and the symlink(s) can be removed. This can be verified by selecting Get Info on a track that was moved and verify that the path points to the new location.
If you didn't get the symlink correct iTunes will display
(!)
beside each missing track. To fix the issue, simply create a symlink in the old location pointing to the new location and run the script again. Hint: the Get Info dialog can be used to determine the old location of a missing track.This worked on iTunes 11.0.5
添加到 millerdev 之前的答案中,我更新了脚本以与 MacOS Catalina 和新的音乐应用程序一起使用。只需创建一个 $HOME/Library/Music/Scripts 目录并将其放置在那里。
Adding to the previous answer by millerdev I updated the script to work with MacOS Catalina and the new Music app. Just create a $HOME/Library/Music/Scripts directory and place it there.