How to minimise application window using System Events on load?

I’m trying to detect when an application window is open, so I can then immediately minimise it. The application is not natively scriptable so I’m having to use System Events. I can detect when the process is running, but that doesn’t necessarily mean the application window has opened yet. I’m currently using the sleep command to wait until the application window Is open, which is not ideal because sometimes the application window is open for a few seconds and other times it hasn’t opened, so it can fail to minimise it. I’m setting the properties for both minimized and visible because neither seems to minimise the application window by itself.

Here is what I have so far:

osascript -e 'tell app "BBC iPlayer Downloads" to activate' 
sleep 5
osascript -e 'tell app "System Events" to tell application process "BBC iPlayer Downloads" to set minimized to true'
osascript -e 'tell app "System Events" to tell application process "BBC iPlayer Downloads" to set visible to false'

You have to tell a window to minimize.
Not a process

Tell window 1 of application process “BBC iPlayer Downloads” to minimize

I see that your example is a terminal command, not a true AppleScript.

I’m having to use System Events because the application “BBC iPlayer Downloads” is not natively scriptable - it doesn’t have a minimize property.

This code:
tell application “BBC iPlayer Downloads”
set minimized of window 1 to true
end tell

produces this error:

error "BBC iPlayer Downloads got an error: Can’t set minimized of window 1 to true." number -10006 from minimized of window 1

Here is the code I ran to get the window properties:
tell application “System Events”
tell process “BBC iPlayer Downloads”
get properties
tell window 1
get properties
end tell
end tell
end tell

which produces:

{minimum value:*missing value*, orientation:*missing value*, position:{208, 66}, *class*:*window*, accessibility description:*missing value*, role description:"standard window", focused:*false*, title:"BBC iPlayer Downloads", size:{1024, 768}, help:*missing value*, entire contents:{}, enabled:*missing value*, maximum value:*missing value*, role:"AXWindow", value:*missing value*, subrole:"AXStandardWindow", selected:*missing value*, name:"BBC iPlayer Downloads", description:"standard window"}

Here is the code I ran to get the process properties:
tell application “System Events”
tell process “BBC iPlayer Downloads”
get properties
end tell
end tell

which produces:
{has scripting terminology:*false*, bundle identifier:"uk.co.bbc.iplayer.downloads", *file*:*alias* "Macintosh HD:Applications:BBC iPlayer Downloads.app:" **of** *application* "System Events", creator type:"????", subrole:*missing value*, entire contents:{}, selected:*missing value*, application file:*alias* "Macintosh HD:Applications:BBC iPlayer Downloads.app:" **of** *application* "System Events", orientation:*missing value*, role:"AXApplication", accepts high level events:*true*, file type:"APPL", value:*missing value*, position:*missing value*, id:3789725, displayed name:"BBC iPlayer Downloads", name:"BBC iPlayer Downloads", *class*:*application process*, background only:*false*, frontmost:*false*, size:*missing value*, visible:*true*, Classic:*false*, role description:"application", maximum value:*missing value*, architecture:"x86_64", partition space used:0, short name:"BBC iPlayer Downloads", focused:*missing value*, minimum value:*missing value*, help:*missing value*, title:"BBC iPlayer Downloads", accepts remote events:*false*, total partition size:0, description:"application", accessibility description:*missing value*, enabled:*missing value*, unix id:42972}

Any suggestions?

Using open -j was my first thought - but the application doesn’t respect the hidden flag. That’s why I switched to using System Events to manipulate it.

Any other suggestions? If I can figure out how to detect when the application window has opened, without having to use an arbitrary sleep command, then I can minimise it using:

osascript -e 'tell app "System Events" to tell application process "BBC iPlayer Downloads" to set minimized to true'
osascript -e 'tell app "System Events" to tell application process "BBC iPlayer Downloads" to set visible to false'

If it helps, the application is free to download for mac os: BBC iPlayer - BBC iPlayer Downloads app.

You could also use the ‘click’ command to click the minimize button, usually button 3

Tell application “System Events”
		 -- Click button 3 of window 1 of  application "BBC iPlayer Downloads" 
	 tell application "BBC iPlayer Downloads"
		  Click item 1 of (buttons of window “BBC iPlayer Downloads” whose description is “minimize button”)
	 End tell
End tell

Or

Tell application “System Events”
		 -- Click button 3 of window 1 of  application "BBC iPlayer Downloads" 
	 tell application "BBC iPlayer Downloads"
		  set value of attribute “AXMinimized” of window “BBC iPlayer Downloads” to true
	 End tell
End tell

EDIT
I was a little wrong. The button is button 3 not 2

Or a cleaned up version of above from Fredrik71

Tell application “BBC iPlayer Downloads” to activate
Tell application “System Events”
    Tell application process “BBC iPlayer Downloads”
        Set frontmost to true
        Repeat with I from 10 to 0 by -1
            If frontmost then exit repeat
            Delay 1
        End repeat
        If I > 0 the -- didn’t time-out
            Set visible to false
        End if
    End tell
End tell

**EDIT. Having problems formatting in iPad
**EDIT - fixed the repeat loop to end at 0, not 1

I think this is the best solution because it waits a maximum of 1 second before setting visible to false, whereas my original solution could potentially wait up to 5 seconds.

Thanks to everyone for the input, much appreciated!