【问题标题】:How to activate full screen apps in background via Apple Script如何通过 Apple Script 在后台激活全屏应用
【发布时间】:2016-09-22 19:42:05
【问题描述】:

我一直在开发 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”中的前 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。在我的主显示器上打开全屏应用无关紧要。

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

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

具体来说,有没有一种方法可以在后台“激活”每个全屏应用而不移动到这些空间?

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

任何帮助将不胜感激。

【问题讨论】:

    标签: macos applescript itunes desktop-background


    【解决方案1】:

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

    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 包含在文件名中。

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-14
      • 1970-01-01
      • 1970-01-01
      • 2017-09-26
      • 2021-11-20
      • 2018-12-10
      • 2022-01-06
      • 2012-02-10
      相关资源
      最近更新 更多