如何通过Apple Script

时间:2016-05-25 09:34:48

标签: macos applescript itunes desktop-background

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

您可以下载我的第一个版本here

我发现最令人讨厌的问题是,当同一个显示屏上有一个全屏应用程序正在更新时,每当歌曲改变时,当前歌曲和之前的歌曲之间的背景都会闪烁。

这是我目前的代码(从上面的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

我得到了将图稿保存到here文件的实际代码。

桌面不会更新,除非你给它一个新的文件名进行更新,因此我用" iTunesArt2-1"重复了这个过程。和" iTunesArt2-1"。

前两个' 2-1'或者' 2-2'只是意味着第二个桌面,因为我有两个不同的应用程序来更改每个桌面,并使用我的第二个桌面进行测试。

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

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

以下是我的调试示例:

  1. 我从" .iTunesArt ##。jpg"删除了开场时间。这样我就可以在桌面上看到这些文件。
  2. 我播放Coldplay歌曲并在Automator中运行应用程序以设置背景。
  3. " iTunesArt2-1.jpg"和" iTunesArt2-2.jpg"在我的桌面上显示正确的专辑封面。
  4. 我停止了应用程序,播放了Paramore的歌曲。
  5. 我运行应用程序的第一个脚本(保存专辑封面)。
  6. " iTunesArt2-1.jpg"更新为Paramore艺术品。
  7. 我运行应用程序的第二个脚本(设置背景图像) 请注意,此脚本应将背景设置为Paramore图像。
  8. 背景设置为Coldplay图像。
  9. 起初我认为这只是因为背景图片已经设置为" iTunesArt2-1.jpg",因此系统不会再次尝试更新它,不知道文件中的数据已被改变。

    所以我运行下一个应强制后台更新的脚本:

    1. 我在应用程序中运行第三个脚本。
    2. " iTunesArt2-2.jpg"更新为Paramore艺术品。
    3. 我在应用程序中运行第四个脚本。
    4. 桌面背景更新为Paramore图稿。
    5. 因此,我们可以确认脚本3& 4工作正常。
      根据代码,当应用程序循环回到脚本1& 2,桌面背景应保留为Paramore图稿 但是...

      1. 我在应用程序中运行第一个脚本。
      2. " iTunesArt2-1.jpg"仍然是帕拉摩尔的艺术品(应该如此)。
      3. 我在应用程序中运行第二个脚本 此脚本应更新来自"" iTunesArt2-2.jpg" (Paramore)到" iTunesArt2-1.jpg" (帕拉莫尔)。
      4. 桌面背景更改为Coldplay图稿。
      5. 现在根本没有任何意义。

        每次循环播放脚本时都会发生这种情况 脚本4将桌面更改为Paramore,脚本2将其更改回Coldplay。

        请记住,只有在正在更新的同一显示器上打开全屏应用时,才会出现此问题。即。在我的主显示屏上打开全屏应用并不重要。

        我发现有时会滑到全屏应用并激活'它会停止闪烁,背景也会正确设置。

        肯定没有什么是错的'我的代码在那里?如果没有,我需要一种方法来解决这个问题。

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

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

        非常感谢任何帮助。

1 个答案:

答案 0 :(得分: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

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

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

相关问题