4

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.

2

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.

  • What does the isRunning keyword? – Iulian Onofrei May 30 '17 at 12:29
  • I don't want the name of the window. – Iulian Onofrei May 30 '17 at 12:30
  • @Iulian Onofrei, isRunning is not a keyword, it's a variable, which gets set to either true or false depending on if the process exists. If you don't want the name of the window, what do you want? – user3439894 May 30 '17 at 12:41
  • The window object, if there is such thing. I somehow want to retrieve some checkboxes in that window. – Iulian Onofrei May 30 '17 at 12:43
  • 1
    @Iulian Onofrei, I figured you did, but wanted to make sure, and also have it available for others that might read this thread and don't have a full understanding of how the code works. :) – user3439894 May 30 '17 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .