如何通过苹果脚本修复“找不到原始文件”错误

问题描述

我的音乐库有问题。 有些歌曲我无法播放,因为它们在本地找不到。 以下是我在播放特定歌曲时收到的错误消息示例:

这首歌……无法使用,因为找不到原始文件。你想找到它吗?

enter image description here

我只需按 Cancel 即可通过 Apple Music 服务匹配歌曲。 这样我就可以播放这首歌了。

这个问题已经讨论过 here,尽管不是以自动化的方式。因此,我想找到一个自动化的解决方案。

为此,我采用了通过播放每首歌曲来循环播放我的音乐库的方法认情况下,如果找不到歌曲,脚本会自动跳到下一首歌曲。 但是,我希望脚本处理“找不到文件错误并按 Cancel

不幸的是,我目前的尝试不起作用:

-- Play first song in library (turn off shuffle and repeat)
set i to 4000 --number of songs in library
repeat while i > 0
    tell application "Music" to play (next track)
    tell application "System Events"
        key code 53
    end tell
    set i to i - 1
end repeat

如何强制脚本处理这些弹出错误

注意:如果您有任何建议,我也愿意接受任何其他更有效的解决方案。我决定不选择 Locate 选项,因为它需要更多时间,而且我会在稍后阶段从我的磁盘中删除任何未引用的歌曲。

解决方法

更新版本。以下脚本不会播放曲目,并在曲目损坏时以编程方式单击“取消”按钮。就像 OP 修复轨道手动工作流程所描述的那样:

tell application "Music"
    activate -- required
    tell source "Library"
        repeat with nextTrack in tracks
            try
                with timeout of 2 seconds
                    set theResult to play (contents of nextTrack)
                end timeout
                theResult
            on error
                delay 1
                tell application "System Events" to tell process "Music" to tell window 1 to if UI element "Cancel" exists then click UI element "Cancel"
            end try
            stop
        end repeat
    end tell
end tell
,

我可以提出脚本与手动方法不完全相同的两个原因:1)您需要在“告诉系统事件...”句子之后给出一些延迟。尝试使用不同的延迟值,2) Music.app 需要真正的鼠标点击,并将鼠标指针移动到“取消”按钮。在这种情况下,请尝试以下脚本:

use scripting additions
use framework "Foundation"
use framework "ApplicationServices"
use framework "Quartz"
use framework "AppKit"
property updateMouseCursorPosition : true
property buttonCount : 1 -- left mouse button number
property mouseButtonDown : true -- mouse button pressed?

set currentMousePosition to getCurrentMousePosition() -- get current cursor position on screen
--> {156,60}

set aPoint to current application's CGPointZero
set aPoint to {100,50} -- set the point coordinates on the screen

mouseLeftClick at aPoint -- perform left click and move the cursor to new position

tell application "Music"
    activate -- required
    tell source "Library"
        repeat with nextTrack in tracks
            try
                with timeout of 2 seconds
                    set theResult to play (contents of nextTrack)
                end timeout
                theResult
            on error
                delay 1
                tell application "System Events" to tell process "Music" to tell window 1
                    if UI element "Cancel" exists then
                        set aPoint to position of UI element "Cancel"
                        set aPoint to {20 + (item 1 of aPoint),10 + (item 2 of aPoint)}
                        mouseLeftClick of me at aPoint -- perform left click and move the cursor to new position
                    end if
                end tell
            end try
            stop
        end repeat
    end tell
end tell

on getCurrentMousePosition()
    -- Getting the screen resolution and the bottom bound
    tell application "Finder" to set screen_resolution to bounds of window of desktop
    set theBottom to item 4 of screen_resolution
    -- Getting current mouse position {0,0} is top left corner of screen
    set currentMousePosition to (current application's NSEvent's (mouseLocation)) as record
    set x to currentMousePosition's x as integer
    set y to theBottom - (currentMousePosition's y) as integer
    return {x,y}
end getCurrentMousePosition

on setPoint(xPos,yPos)
    set pt to current application's CGPointMake(0,0)
    set pt to {xPos,yPos}
    return pt
end setPoint

on mouseLeftClick at aPoint
    current application's CGPostMouseEvent(aPoint,updateMouseCursorPosition,buttonCount,mouseButtonDown)
    current application's CGPostMouseEvent(aPoint,not mouseButtonDown)
end mouseLeftClick

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...