Can't get Window 1 of process intermittently thrown - applescript

I have an applescript saved as an always on application, which generally works. However, there are odd instances where it will throw the error:
"Systems Events got an error: Can't get window 1 of process "FaceTime". Invalid index. (-1719)"
Mostly occurs if FaceTime app is closed, but not always. Odd.
Can anyone advise how I can dismiss this or write better code to avoid?
Here is my code:
on idle
if application "FaceTime" is running then
tell application "System Events"
tell process "FaceTime"
if get value of attribute "axFullScreen" of window 1 = false then
set FullscreenButton to a reference to (button "Full Screen" of window 1)
if FullscreenButton exists then
click FullscreenButton
end if
end if
end tell
end tell
end if
return 3
end idle
on quit
continue quit
end quit

Put all Code inside the idle block in a try and Error Block. If an error is thrown itll be catched
try
-- your code
on error err
log err
end try

Related

Apple script runs into error: "Can’t make missing value into type document or tab" if you move away from the page

I run the script below to do some web scraping and it works well until do anything else while the script is running. The moment I do something else the following error appears:
error "Safari got an error: Can’t make missing value into type document or tab."
How can I resolve this issue?
tell application "Safari"
activate
repeat with a from 1 to 159
open location "https://google.com" & a
delay 1
set keyword to "test"
set myWindow to current tab of first window
activate
do JavaScript "window.location.reload()" in myWindow
repeat while (do JavaScript "document.readyState" in document 1) is not "complete"
delay 0.5
end repeat
set pageContent to do JavaScript ("window.document.documentElement.outerHTML") in myWindow
if pageContent contains keyword then
display dialog "found it"
exit repeat
end if
delay 2 -- wait a bit before running again
close myWindow
end repeat
end tell
In my testing I did not see any AppleScript errors; this script as written seems to work fine, though it never finds the keyword in question. Of course, I'm on Catalina; if you're on a different OS, can you specify? Or can you post a script that doesn't have test code (e.g., open location "https://google.com" & a, which clearly does nothing), and that you are absolutely sure produces an error for you?
I'd suggest a rewrite as follows, for the moment:
tell application "Safari"
set myWindow to current tab of first window
repeat with a from 1 to 159
open location "https://google.com" & a
delay 1
set keyword to "test"
do JavaScript "window.location.reload()" in myWindow
repeat while (do JavaScript "document.readyState" in document 1) is not "complete"
delay 0.5
end repeat
set pageContent to do JavaScript ("window.document.documentElement.outerHTML") in myWindow
if pageContent contains keyword then
display dialog "found it"
exit repeat
end if
delay 2 -- wait a bit before running again
close myWindow
end repeat
end tell
All this does is remove the activate commands so that Safari stays in the background, and moves the line that sets the window out of the repeat loop, so that the reference is only set once.
There are other suggestions I could make (for instance, this would be much more efficient as a script application than a script), but save those for now.

How can I bypass an index error or have the script run again?

I am encountering an index error that appears when the app in use has an overlay or notification appear. To provide a better description, the app will occasionally show an alert if something needs to be acknowledged or dismissed. When that happens, the script is unable to return the value from the designated location in the GUI, and returns the following error message: "Can’t get group 4 of toolbar 1 of window 1 of process "App I'm Using". Invalid index.System Events got an error: Can’t get group 4 of toolbar 1 of window 1 of process "App I'm Using". Invalid index. (-1719)"
The behavior is expected, but I would like to adjust the script to where it will either delay trying again for 30 seconds or so, or just not display said error at all.
I've been toying around with using an 'on error' statement, but I can't get it to take with the 'tell' statement that it's referring to, for example:
on error error_message number error_number
if error_number = -1719 then
wait 30
end if
I'm unsure of how I can use the 'on error' function with the section of the script below, but if I can make it try again in 30 - 45 seconds without displaying an error, it would be perfect.
on idle
-- Update the status item's text here.
tell application "System Events"
if not (exists process appName) then
display alert "Application " & appName & " is not running" as warning giving up after 6
quit me
end if
tell process appName
tell first window's first toolbar's fourth group's first group's first menu button
set activityState to first item of (value as list) as text
end tell
end tell
end tell
end idle
I believe the error is encountered when the script reaches "tell window's first toolbar's fourth group's..." before it is supposed to "set activityState to first item...".
I have used the 'on error' function with 'try' statements successfully, but I'm having issues moving forward with this one.
Or you can try this approach which will remain in the repeat loop until first window's first toolbar's fourth group's first group's first menu button becomes available.
on idle
-- Update the status item's text here.
tell application "System Events"
if not (exists process appName) then
display alert "Application " & appName & " is not running" as warning giving up after 6
quit me
end if
tell process appName
repeat until exists of first window's first toolbar's fourth group's first group's first menu button
delay 0.2
end repeat
tell first window's first toolbar's fourth group's first group's first menu button
set activityState to first item of (value as list) as text
end tell
end tell
end tell
end idle
You can’t split statements (such as if or tell). The try statement needs to wrap around the complete statement(s) you want it to work with, for example, the statement telling the app process:
on idle
tell application "System Events"
if not (exists process appName) then
---
end if
try
tell process appName
---
end tell
on error errmess number errnum
return 30 -- try the idle handler again in 30 seconds
end try
end tell
end idle
You can use the exists keyword to check if an element is present before trying to access it. exists won't through an error if the element isn't there, and will let you skip over the problematic lines:
on idle
-- Update the status item's text here.
tell application "System Events"
if not (exists process appName) then
display alert "Application " & appName & " is not running" as warning giving up after 6
quit me
end if
tell process appName
-- assuming the window and toolbar are always going to be there
try
tell first window's first toolbar
-- check to see if the UI element exists
if exists fourth group's first group's first menu button then
-- only get the activity state if it does
tell fourth group's first group's first menu button
set activityState to first item of (value as list) as text
end tell
end if
end tell
on error errstr
(*
this code is in an 'idle' handler, so on any error we
just return 30 to idle for another 30 seconds and try again.
*)
return 30
end try
end tell
end tell
end idle

Applescript: Photoshop: How to find out whether the image is really open

We have an automation tool for Photoshop, using a control app, which calls Applescripts controlling Photoshop.
One situation is that we must open a RAW image in the CameraRAW plug-in, and then open it in Photoshop. This part is handled, via an applet using System Events. When that applet terminates, we run the processing script for Photoshop.
As some pictures take quite a bit of time to open, we have to make sure that the picture is really open before the script can run. … and that's where I am stuck.
At the moment, I am using the following code which is intended to wait until the image is open (the criterion for "open" is correct (and tested manually), so that's not the issue here).
tell application "Adobe Photoshop CC 2015"
activate
tell application "System Events"
tell application process "Photoshop CC"
--
display alert "Waiting for Window"
--
repeat
try
set wn to name of window 1 as text
try
if (wn contains "(RGB/16") then
set wn to "image is open: " & wn
end if
end try
if (wn contains "(RGB/16") then
display alert "We are there, quitting now… " & wn
exit repeat
end if
end try
delay 1
end repeat
end tell
end tell
--
display alert "Ready for process"
--
-- and here comes the processing code
end tell
I also tried to set a variable which is tested as argument for repeat, and changed when the exit condition is fulfilled.
Trying to create even alerts within the repeat loop, does not lead to any effect; the script ends up in an infinite loop.
It is well possible that I miss the obvious… So, I am grateful for any helpful hint.
Thanks in advance.
I think you have a few small issues with your script that are causing your problem.
You are using name of window 1 when I believe you need name of document 1. With your first try block structured as it was you weren't realizing it was actually giving an error on name of window 1
The name that is returned doesn't contain the color space and bit count, so I've changed the result test to an empty string
Notice the modifications to the try block around getting the document name
I don't believe it's necessary or see a reason to use "System Events" in this case, so I've modified the version below without it.
Example Script
tell application "Adobe Photoshop CC 2015"
display alert "Waiting for Window"
repeat
try
set wn to name of document 1 as text
on error
set wn to ""
end try
try
if wn is not equal to "" then
set wn to "image is open: " & wn
end if
end try
if wn is not equal to "" then
display alert "We are there, quitting now… " & wn
exit repeat
end if
delay 1
end repeat
display alert "Ready for process"
end tell

Applescript to check App Store

Thanks for taking the time to read my question.
It's pretty simple, but i am a complete noobie to this, so am having some trouble.
Is it possible to have an applescript that will check the mac app store for updates, and if there are, output the number of updates to someplace?
A good example of this is (if you are aware of it) the geeklets that check for unread mail, and then outputs it to the desktop.
EDIT:
I downloaded a geeklet for the unread mail (as referenced above), and using that as a starting point, I tried to write my own script.
set run_bool to 1
tell application "System Events"
set run_bool to count (every process whose name is "App Store")
end tell
if run_bool is 1 then
tell application "App Store"
set update_count to 0
set output_string to ""
repeat with upd in Apps in Updates
if upd's download is not true then
set update_count to update_count + 1
end if
end repeat
if update_count is 0 then
set output_string to "zero"
else if update_count is 1 then
set output_string to "one"
else
set output_string to "two"
end if
end tell
else
set output_string to "not running"
end if
return output_string
now this is not my final code, but simply to check to see if it will work and what the output would be.
On compilation I get an error saying
error "The variable Updates is not defined." number -2753 from "Updates"
as well as
Syntax Error
Expected end of line but found unknown token
Also, when I stopped compilation, this appeared below the last line in my code
tell application "GeekTool Helper"
activate
«event ascrgsdf»
Any help is appreciated.
#foo is pretty right on with his idea. This code only requires one line. In the second line, I used display notification, but you substitute it with you preferred method to pass on the value.
tell application "System Events" to tell (first application process whose ¬
frontmost is true) to set returnValue to title of ((first menu item whose title ¬
begins with "App Store") of menu "Apple" of menu bar 1)
display notification returnValue
Result:
"App Store…, 1 update"
menu bar items are accessible everywhere (e.g. windowed/numeral desktop mode, fullscreen mode, dock hidden/disabled).
Make sure accessibility is enabled for Script Editor, or whichever app you use to invoke the script, to gain access to the UI.
There is just one weird thing: if I had used begins with "App Store..." instead of begins with "App Store", the code would be a dud. I don't know why - it might has to do with escaped characters and the .... Anyone who knows please enlighten me with a comment.
As for your code, I can tell from AppleScript Dictionary that Updates is not a property of App Store.app. Nor is any other categories in the UI. To get to the Dictionary, open Script Editor and press CMD+SHIFT+O
In addition, if you want to use return statement, you need an explicit handler. In other words, you need to wrap the code between on run and end run.

Applescript check if window is one screen

I have written this script function
on GetWindowLocation()
set front_app to (path to frontmost application as Unicode text)
tell application front_app
item 1 of (get bounds of front window)
end tell
end GetWindowLocation
on GetDockStatus()
tell application "System Events" to get the autohide of the dock preferences
end GetDockStatus
if I am on the desktop with no windows up it errors.
How can I check if a window is on screen, so I can put an if statement to not
run it if a window is not on the screen.
Probably the easiest fix here would be to simply catch the error:
tell application front_app
try
return item 1 of (get bounds of front window)
on error
-- do something here to handle there being no front window
end try
end tell
You could also try checking the count of windows before trying to reference the front window, but that's somewhat more prone to errors (as the window may disappear before you grab its bounds).

Resources