How to activate full screen apps in background via Apple Script
<p>I've been working on an application in Automator that changes the desktop background to the album art of the current song playing in iTunes.</p>

<p>You can download my first version <a href="https://www.dropbox.com/s/a9u0awhjmljaqex/iTunes%20Wallpaper%201.0.zip?dl=0" rel="nofollow noreferrer">here</a>.</p>

<p>The most annoying issue I have found is that when there is a full screen app open on the same display being updated, whenever the song changes the background flickers between the current song and the previous.</p>

<p>This is my current code (Updated from version 1.0 above):<br>
You may need to scroll within the code to see all of it.</p>

<pre><code>tell application "System Events"
    set fileName to (((path to desktop) as text) &amp; ".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) &amp; ".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
</code></pre>

<p>I got the actual code that saves the artwork to a file from <a href="https://stackoverflow.com/questions/16995273/getting-artwork-from-current-track-in-applescript">here</a>.</p>

<p>The desktop will not update unless you give it a new file name to update, therefore I have duplicated the process with "iTunesArt2-1" and "iTunesArt2-1".</p>

<p>The first 2 in '2-1' or '2-2' simply means the second desktop, as I have two different applications to change each desktop, and use my 2nd desktop for testing.</p>

<p>The entire application is set to loop for 1000 years, using three separate Loop functions in Automator (720 minutes, 730 times &amp; 1000 times).</p>

<p>When first trying to debug this issue, the process was duplicated into four separate scripts, one for saving the image, then setting as background, and two more scripts to repeat the process with a new file name.</p>

<p>Here's an example of my debugging:</p>

<ol>
<li>I remove the opening period from ".iTunesArt##.jpg" so that I can see the files on my desktop.  </li>
<li>I play a Coldplay song and run the application in Automator to set the background.  </li>
<li>"iTunesArt2-1.jpg" and "iTunesArt2-2.jpg" are shown on my desktop with the correct album art.  </li>
<li>I stop the application, and play a Paramore song.  </li>
<li>I run the first script of the application (Saves the album art).</li>
<li>"iTunesArt2-1.jpg" is updated to the Paramore artwork.  </li>
<li>I run the second script of the application (Sets background image).<br>
Note, this script should be setting the background to the Paramore image.</li>
<li>The background remains set to the Coldplay image.</li>
</ol>

<p>At first I think this is only because the background image was already set to "iTunesArt2-1.jpg", and therefore the system will not try to update it again, not knowing the data in the file has been changed.</p>

<p>So I run the next script which should force the background to update:</p>

<ol start="9">
<li>I run the third script in the application.</li>
<li>"iTunesArt2-2.jpg" is updated to the Paramore artwork.</li>
<li>I run the forth script in the application.</li>
<li>The desktop background is updated to the Paramore artwork.</li>
</ol>

<p>So, we can confirm that scripts 3 &amp; 4 worked correctly.<br>
According to the code, when the application loops back to scripts 1 &amp; 2, the desktop background should remain as the Paramore artwork.<br>
BUT...</p>

<ol start="13">
<li>I run the first script in the application.</li>
<li>"iTunesArt2-1.jpg" remains the Paramore artwork (as it should).</li>
<li>I run the second script in the application.<br>
This script should update the desktop background from ""iTunesArt2-2.jpg" (Paramore) to "iTunesArt2-1.jpg" (Paramore).</li>
<li>The desktop background changes to the Coldplay artwork.</li>
</ol>

<p>Now that makes zero sense at all.</p>

<p>This will happen every time I loop through the scripts.<br>
Script 4 will change the desktop to Paramore and script 2 will change it back to Coldplay.</p>

<p>Remember this issue only happens when there is a full screen app open on the same display being updated. ie. A full screen app open on my primary display doesn't matter.</p>

<p>I have found sometimes that sliding over to the full screen app and 'activating' it will stop the flickering, and the background will be set correctly.</p>

<p>There surely is nothing 'wrong' with my code is there? If not, I need a way to get around this issue.</p>

<p>Specifically, is there a way of 'activating' every full screen app in the background without moving to those spaces?</p>

<p>I would love anyone who wants it to have this program, but it needs to be able to run under all circumstances.</p>

<p>Any help would be greatly appreciated.</p>