1

Hello and thanks for taking the time to help.

I have limited coding experience in AppleScript. What Im trying to do is create a script that will open, set to fullscreen, and then arrange several applications that I use regularly. I added some logic into the script to help the flow if the applications are already open. but the problem I'm running into is during the For Next Loop, when I'm trying to set the applications to Fullscreen, the application Calendars is not activated, then I get a "System Events got an error: Can’t get window 1 of process "Calendar". Invalid index." error. This is because the Calendars window is not indexed yet, but if I delay for around 1-2 seconds after "Activate" then the script usually runs. But surely there is a way to command the script to index Calendars' windows before it tries to run the Fullscreen line? I'd like to remove the delay line of code, so that the script can run faster. Can anyone lend some ideas or solutions?

set app1 to "Maps"
set app2 to "Safari"
set app3 to "Mail"
set app4 to "Calendar"
set app5 to "Contacts"

set thelist to {app1, app2, app3, app4, app5}

repeat with i in thelist
 if application i is running then  
  tell application i
   activate
   delay 0.5
  end tell
  tell application "System Events" to tell process i
   set value of attribute "AXFullScreen" of window 1 to true
  end tell
 else
 end if
end repeat

1 Answers1

0

The Windows are available when app finished launching. You have to wait. but you could limit it to the time necessary:

instead of waiting a fixed delay do the following

repeat
    try
        tell application "System Events" to tell process i
                set value of attribute "AXFullScreen" of window 1 to true
                exit repeat
         end tell
    end try
    delay 0.01
end repeat

This way the method trys until it works and then exits.

Pat_Morita
  • 3,355
  • 3
  • 25
  • 36
  • Or perhaps a `repeat while not (exists window 1) \n end repeat` inserted just before the `set value` command. *Might* be a bit faster and make the code a bit neater. I haven't tested this, though. – CJK Jan 18 '18 at 13:24