I am writing a script to generate PDF for a lot of mails. Because the PDF generation takes some time, I want the script to wait till
to disappear in Mail. So I can start to process the next one. The following are the attributes of this element.
Since it has no title, how can I specify to wait till this element to disappear?
You need two repeat loops, one waits until the sheet appears and the other waits uniil the sheet disappears.
tell application "System Events"
tell process "Mail"
repeat until exists sheet 1 of window 1
delay 0.5
end repeat
repeat while exists sheet 1 of window 1
delay 0.5
end repeat
-- sheet was dismissed
end tell
end tell
Related
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
I'm a total beginner in apple script.
I would like to open a folder directory that contains hundreds of photos and to view each photo 1 second with quick look and then go to the next (suppose to use down arrow / key code '124').
I don't have any idea of how to build the script. I tried to compile some formulas from other questions or use the manual rec but it doesn't work.
Thanks!
Try following script. I made the delay longer so you have time to stop the script (to test it):
set timePerPreview to 5
set thisFolder to (choose folder with prompt "Pick the folder containing the files to process:") as string
tell application "Finder"
activate
open folder thisFolder
select every item in folder thisFolder
delay 2
set fileCount to (count items in (get selection)) # (count files in folder thisFolder) is faster but counts also .DS_Store and other invisible files
if fileCount = 0 then
beep
return
end if
end tell
pressSpaceInFinder()
delay timePerPreview / 2 # first it has to open the window which seems to need a little time
repeat fileCount - 1 times # -1 because the first item is already displayed
delay timePerPreview
# do shell script "sleep" & space & timePerPreview as text
tell application "System Events"
tell application process "Finder"
set frontmost to true
# cursor right = 124, left = 123
key code 124
end tell
end tell
end repeat
delay timePerPreview
pressSpaceInFinder()
on pressSpaceInFinder()
tell application "System Events"
tell application process "Finder"
set frontmost to true
keystroke space
end tell
end tell
end pressSpaceInFinder
Be aware that it is hard to stop the script since the Finder gets activated every second. Will see if I can add a check to see if the Preview window is open and if not, stop the script.
Also: (without that check) the script will, when the Preview window is open before running, close it but you can just press the space key to open it again.
Also, the first part (getting the file count) isn't so great and may fail when the delay is to short. We could scan the whole folder for images and only count / select those (it has a file template for the scanner, see menu File) but of course you can remove the whole counting thing and just do repeat 1000 times.
I have an applescript that is now doing what I want it to do: Open a specific URL, close the page and wait 2 seconds then do it again. The script works.
The problem is that when I run this on my machine, and I am trying to do something else at the same time, the Safari windows keeps popping up. I would really like to run this in the background so that I can continue to work on whatever I am doing.
The code I have is:
set theURL to "https://sites.google.com/site/whatever/"
repeat 100 times
tell application "Safari"
activate
try
tell window 1 to set current tab to make new tab --with properties {URL:theURL}
set URL of document 1 to theURL
on error
open location theURL
end try
delay 2
try
close window 1
end try
end tell
tell application "Safari"
quit
end tell
delay 1
end repeat
Anyone have a solution to this one?
Safari is coming to the front because you are activating it within the loop.
repeat 100 times
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to "https://sites.google.com/site/whatever/"
delay 2
close window 1
quit
end tell
delay 1
end repeat
EDIT
set myURLs to {"https://www.google.com/", "http://www.yahoo.com/"}
repeat with aUrl in myURLs
repeat 100 times
tell application "Safari"
if not (exists document 1) then reopen
set URL of document 1 to aUrl
delay 2
close window 1
quit
end tell
delay 1
end repeat
end repeat
I'm trying to write a program that does the following...
Reads a line of text from textedit that contains only a series of URLs (each on a seperate line).
Goes to that URL in the current page on my web browser.
Makes two page clicks on the same location of the loaded page.
Hits the "Enter" button
Repeats from the first step
I am a beginner at applescript... but can learn anything needed to complete this project. Could someone show me an example of the code that I am trying to write?
set urls to "http://stackoverflow.com/questions/18069198
http://stackoverflow.com"
--set urls to read "/tmp/input.txt" as «class utf8»
repeat with u in paragraphs of urls
tell application "Safari"
activate
open location u
delay 1
tell document 1
repeat while do JavaScript "document.readyState" is not "complete"
delay 0.1
end repeat
do JavaScript "document.querySelectorAll('#hlogo a')[0].click()"
end tell
end tell
tell application "System Events" to keystroke return
end repeat
I have the following code. It runs, and i get my result, i think something wrong with the exit repeat, after it get the ipad name, the script is still keep running until time out.
Can anyone tell me what's wrong with the code? Thanks!
set deviceName to "iPad"
tell application "System Events"
tell process "iTunes"
activate
repeat with i from 1 to the count of (row of outline 1 of scroll area 2 of window "iTunes")
repeat with j from 1 to the count of static text of row i of outline 1 of scroll area 2 of window "iTunes"
set xxxx to the value of item j of static text of row i of outline 1 of scroll area 2 of window "iTunes"
if (xxxx contains deviceName) then
print xxxx
click row i of outline 1 of scroll area 2 of window "iTunes"
exit repeat
end if
--exit repeat
end repeat
end repeat
end tell
end tell
If you want to exit the script after the first iPad is found, replace exit repeat with return.
The problem (you ask for) is that you have an repeat in an repeat. That means when you're in the sub-loop and exit the repeat you will jump to the main loop. To exit from here you have to an exit repeat again.
Looking at your code I don't understand the nested repeat loop. You can remove the surrounded/main repeat and It will work as expected.
set deviceName to "iPad"
tell application "System Events"
tell process "iTunes"
activate
repeat with UIElement in rows of outline 1 of scroll area 2 of window "iTunes"
if (value of static text of UIElement as text) begins with deviceName then return select UIElement
end repeat
end tell
end tell
The reason I use an begins with is that contains will click the previous purshcase menu items.