0

我一直在 Automator 中开发一个应用程序,它将桌面背景更改为在 iTunes 中播放的当前歌曲的专辑封面。

你可以在这里下载我的第一个版本。

我发现的最烦人的问题是,当在更新的同一显示器上打开全屏应用程序时,每当歌曲更改时,背景就会在当前歌曲和上一首歌曲之间闪烁。

这是我当前的代码(从上面的 1.0 版更新):
您可能需要在代码中滚动才能看到所有代码。

tell application "System Events"
    set fileName to (((path to desktop) as text) & ".iTunesArt2-1.jpg")
    set oldFile to open for access file fileName with write permission
    write 0 to oldFile
    close access oldFile
    delete file fileName
    if process "iTunes" exists then
        tell application "iTunes"
            if (player state is not stopped) then
                -- get the raw bytes of the artwork into a var
                tell application "iTunes" to tell artwork 1 of current track
                    set srcBytes to raw data
                end tell
                -- write to file
                set outFile to open for access file fileName with write permission
                -- truncate the file
                set eof outFile to 0
                -- write the image bytes to the file
                write srcBytes to outFile
                close access outFile
            end if
        end tell
        tell desktop 2
            set picture to fileName
        end tell
    end if
    set fileName to (((path to desktop) as text) & ".iTunesArt2-2.jpg")
    set oldFile to open for access file fileName with write permission
    write 0 to oldFile
    close access oldFile
    delete file fileName
    if process "iTunes" exists then
        tell application "iTunes"
            if (player state is not stopped) then
                -- get the raw bytes of the artwork into a var
                tell application "iTunes" to tell artwork 1 of current track
                    set srcBytes to raw data
                end tell
                -- write to file
                set outFile to open for access file fileName with write permission
                -- truncate the file
                set eof outFile to 0
                -- write the image bytes to the file
                write srcBytes to outFile
                close access outFile
            end if
        end tell
        tell desktop 2
            set picture to fileName
        end tell
    end if
end tell

我从这里得到了将艺术品保存到文件的实际代码。

除非你给它一个新的文件名来更新,否则桌面不会更新,因此我用“iTunesArt2-1”和“iTunesArt2-1”复制了这个过程。

“2-1”或“2-2”中的第一个 2 仅表示第二个桌面,因为我有两个不同的应用程序来更改每个桌面,并使用我的第二个桌面进行测试。

整个应用程序设置为循环 1000 年,使用 Automator 中的三个独立循环功能(720 分钟、730 次和 1000 次)。

首次尝试调试此问题时,该过程被复制为四个单独的脚本,一个用于保存图像,然后设置为背景,另外两个脚本用于使用新文件名重复该过程。

这是我的调试示例:

  1. 我从“.iTunesArt##.jpg”中删除了开放时间段,以便可以在桌面上看到文件。
  2. 我播放一首酷玩歌曲并在 Automator 中运行应用程序来设置背景。
  3. “iTunesArt2-1.jpg”和“iTunesArt2-2.jpg”以正确的专辑封面显示在我的桌面上。
  4. 我停止了应用程序,并播放了一首 Paramore 歌曲。
  5. 我运行应用程序的第一个脚本(保存专辑封面)。
  6. “iTunesArt2-1.jpg”更新为 Paramore 作品。
  7. 我运行应用程序的第二个脚本(设置背景图像)。
    请注意,此脚本应将背景设置为 Paramore 图像。
  8. 背景仍然设置为 Coldplay 图像。

起初我认为这只是因为背景图像已经设置为“iTunesArt2-1.jpg”,因此系统不会再次尝试更新它,不知道文件中的数据已被更改。

所以我运行下一个脚本,它应该强制背景更新:

  1. 我在应用程序中运行第三个脚本。
  2. “iTunesArt2-2.jpg”更新为 Paramore 作品。
  3. 我在应用程序中运行第四个脚本。
  4. 桌面背景更新为 Paramore 图稿。

因此,我们可以确认脚本 3 和 4 工作正常。
根据代码,当应用程序循环回到脚本 1 和 2 时,桌面背景应保持为 Paramore 艺术品。
但...

  1. 我在应用程序中运行第一个脚本。
  2. “iTunesArt2-1.jpg”仍然是 Paramore 的作品(应该如此)。
  3. 我在应用程序中运行第二个脚本。
    此脚本应将桌面背景从“iTunesArt2-2.jpg”(Paramore)更新为“iTunesArt2-1.jpg”(Paramore)。
  4. 桌面背景变为酷玩艺术作品。

现在这完全没有意义。

每次我遍历脚本时都会发生这种情况。
脚本 4 会将桌面更改为 Paramore,脚本 2 会将其更改回 Coldplay。

请记住,仅当在更新的同一显示器上打开全屏应用程序时才会发生此问题。IE。在我的主显示器上打开的全屏应用程序无关紧要。

我发现有时滑到全屏应用程序并“激活”它会停止闪烁,并且背景将被正确设置。

我的代码肯定没有任何“错误”吗?如果没有,我需要一种方法来解决这个问题。

具体来说,有没有办法在后台“激活”每个全屏应用程序而不移动到这些空间?

我会喜欢任何希望它拥有这个程序的人,但它需要能够在所有情况下运行。

任何帮助将不胜感激。

4

1 回答 1

0

找到了一个解决方法,它每次都会创建一个随机文件名,这意味着桌面不会混淆它应该显示哪个图像。

screenNum指的是桌面被设置到哪个屏幕。

    set randID to screenNum
set randLoop to 0
repeat while randLoop is not 9
    set randNum to (random number from 0 to 9) as text
    set randID to randID & randNum
    set randLoop to randLoop + 1
end repeat

然后randID在创建时包含在文件名中。

使用此方法没有注意到任何闪烁。

注意:这解决了我的问题,但没有回答我最初的问题,即如何在后台激活全屏应用程序。

于 2016-05-28T22:37:37.933 回答