Get first item in list using AppleScript How do I get the first item from a list into a variable?

I'm trying to get the first window of an application's windows.

I tried

tell application "System Events"
    repeat with theProcess in processes
        if not background only of theProcess then
            if processName is "Xcode" then
                set theWindow to first window of windows
            end if
        end if
    end repeat
end tell


but it throws this error:


  Can't get window of every window.
Unless you have an explicit need for going about it in the manner shown in your question, I'd keep it much more simple and use:

tell application "System Events" to set isRunning to exists (processes where name is "Xcode")
if isRunning then set theWindow to (first window of application "Xcode")


Otherwise, modifying your existing code block in the following manner will get that same result as the two lines of code above:

tell application "System Events"
    repeat with theProcess in processes
        if not background only of theProcess then
            if name of theProcess is "Xcode" then
                tell application "Xcode"
                    set theWindow to first window of application "Xcode"
                end tell
            end if
        end if
    end repeat
end tell


If you want to use the first two line of code in my answer in a more tokenized manned, as an example, use:

set appName to "Xcode"
tell application "System Events" to set isRunning to exists (processes where name is appName)
if isRunning then set theWindow to (first window of application appName)


Then all you need to do is set appName to the target app in the first line of code and the two lines of code following it stand as is and do not need to be edited for use with other app names.