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

发布于 2024-09-01 01:34:47 字数 224 浏览 5 评论 0原文

我想以编程方式修改 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖上的星空 2024-09-08 01:34:47

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

file track 类的字典条目显示其 location 属性是可写的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

选择文件方法但这不是您想要的,因为您正在执行某种自动化的、基于字符串的路径名翻译。

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

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

POSIX file str as alias

要转换存储在变量 str 中的 HFS 路径名> 对于 AppleScript 别名,请使用以下表达式:

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

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 its location property being a writable alias 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 an 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

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 AppleScript alias, use the following expression:

POSIX file str as alias

To convert an HFS pathname stored in a variable str to an AppleScript alias, use the following expression:

alias str

For example:

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
眼前雾蒙蒙 2024-09-08 01:34:47

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

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

    -- 标记缺失的曲目(并使用现有曲目的真实路径更新数据库)
    -- iTunes 中的曲目)
     -  指示:
    -- * 将新文件位置符号链接到旧位置(旧位置指向
    -- 新位置,文件存在)
    -- * 选择要扫描/更新的曲目
    --* 运行脚本
    -- * 缺失的轨道用 (!) 标记,所有其他轨道路径都有
    -- 已更新为 iTunes 中的真实路径(已消除符号链接)
    告诉应用程序“iTunes”
        将 fx 设置为固定索引
        将固定索引设置为 true
        将 Sel 设置为选择
    
        用 i 重复从 1 到 Sel 中的项目数
            将 trk 设置为 Sel 的第 i 项
            将 loc 设置为(以文本形式获取 trk 的位置)
        结束重复
    
        将固定索引设置为 fx
    结束告诉
    
  6. 所有轨道现在都应指向正确的位置,并且可以删除符号链接。这可以通过在已移动的轨道上选择“获取信息”并验证路径是否指向新位置来验证。

如果您没有获得正确的符号链接,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:

  1. Move files to new location (e.g., mv ~/MyMusic /Volumes/Huge/)
  2. Create symlink in old location pointing to new location
    (ln -s /Volumes/Huge/MyMusic ~/MyMusic)
  3. Start iTunes (if not already running)
  4. Select all tracks that were moved.
  5. Run this 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. 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

浅暮の光 2024-09-08 01:34:47

添加到 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

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.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文