Why doesn't Automator AppleScript End? - macos

OK, I realize the solution to this is probably something very simple, but I've wasted a lot of time trying to figure it out. As you'll likely be able to tell, I'm new to AppleScript.
I have an AppleScript being run by Automator. It clicks a button in Chrome. I have an action I need to repeat nearly 1000 times, so I'm trying to automate it. Here's what I have:
tell application "Google Chrome"
activate
set theTab to tab 1 of window 1
execute theTab javascript "document.querySelectorAll('[title=Archive]')[0].click()"
end tell
This works as I want: It activates Chrome and clicks a button on the active tab. Clicking the button brings up an alert box for which the default is an OK button. Next I need to click that button, so I have a second AppleScript:
tell application "Google Chrome" to activate
delay 2
tell application "System Events"
keystroke return
end tell
Let me be clear: I know ultimately I should have a single script, but I created two to diagnose the problem I'm having.
It appears the first script just never ends, so it never gets to the second script. If I run the first script, stop it, and then run the second, I get exactly what I want. But unless I stop the first script, it just churns and never moves on. While it's executing the javascript, it seems that it just stops there.
Like I said, this is probably incredibly simple... and I feel incredibly stupid for not seeing the solution. What am I missing?

It's not as easy as you might think, because alert box with the "OK" button is modal. This means: the script will wait for the "OK" button to be pressed, only then will it continue further.
I can't test, because I don't use Google Chrome, and I don't know webpage you test with. Try my suggestion yourself (it uses idea of throwing artefact interruption):
tell application "Google Chrome"
activate
set theTab to tab 1 of window 1
try
with timeout of 1 second
set theResult to (execute theTab javascript "document.querySelectorAll('[title=Archive]')[0].click()")
end timeout
theResult
on error
tell application "System Events" to keystroke return
end try
end tell
Here is fully tested by me closing modal alert box programatically:
try
with timeout of 1 second
set theResult to display alert "TEST"
end timeout
theResult
on error
tell application "System Events" to keystroke return
end try

Related

Apple script to activate a window that pops up within Firefox accurately

I have written this piece of code on my MAC OS X 10.10.5 to automate keystrokes on a window that pops up within Firefox.
It does not work reliably and, in many cases, performs the keystrokes on the window in the background. I have tried increasing the delay but it does not seem to be related to timing. The problem, instead, seems to be that the wrong Firefox window is activated.
This is my code. Any ideas on how I can fix it to work reliably.
set myBrowser to "/Applications/Firefox.app"
tell application myBrowser
activate window 2
end tell
tell application "System Events"
keystroke tab
delay 2
keystroke enter
end tell
If Firefox opens up a pop-up window, I believe that window now becomes window 1. The system events will be sent to that front pop-up window. Try using this code
activate application "Firefox"
tell application "System Events"
delay 2
key code 48
delay 1
key code 36
end tell

Applescript to open an application in full-screen mode?

I'm trying to program Alfred to open my Terminal, Sublime Text, and Chrome with a workflow.
I would like for my terminal to open normally as a window, but I've been trying to get Chrome and Sublime to open full screen.
I was able to get Chrome to open up in full screen mode with:
on alfred_script(q)
tell application "Google Chrome"
tell window 1 to enter presentation mode
end tell
end alfred_script
However, this did not translate to work with my Sublime Text.
What am I missing here?
Another way to do this assuming you have not changed the default keyboard shortcut for "Enter Full Screen" is simply to have System Events invoke that shortcut (⌃⌘F). As with the other approach I've seen to doing this (changing the value of AXFullScreen—see mklement0's answer here for a thorough discussion of this method), this requires making the relevant window active.
For instance, to toggle the full-screen state of the frontmost window in Safari, run:
tell application "Safari" to activate
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
As found here (i need an applescript to open safari in full screen an to hide the toolbar on mavericks). The make new document line prevents the can't get window 1 error by opening a new tab if one has not previously been opened.
tell application "Safari"
make new document
activate
delay 3
tell application "System Events" to tell process "Safari"
set value of attribute "AXFullScreen" of window 1 to true
end tell
end tell

How to keep applescript running and redirecting urls?

I have this code I can keep running inside Script Editor. But it causes my laptops fans to scream! So I am not sure is this right way to do this?
repeat until application "Google Chrome" is not running
if application "Google Chrome" is running then
tell application "Google Chrome"
set (URL of every tab of every window where URL is equal to "facebook.com") to "twitter.com"
end tell
end if
end repeat
Other question is that why my code isn't working? I would like to use this for redirecting from spesific urls to another. But even this implementation doesn't work.. If I open facebook.com it doesn't redirect to twitter.com.
Any help is appreciated.
NOTE: YES, I want to use applescript for this so focus on it, pls :)
When an application appears to work, but doesn’t seem to be matching its conditions, it is always helpful to look at the actual values being checked. In this case, open the URL you think should be matched, and then get that URL to look at.
tell application "Google Chrome"
get URL of tab 1 of window 1
end tell
When I entered “Facebook.com” in a new window in Chrome, and then ran the above script, what I found was https://www.facebook.com/?_rdr=p.
When I replaced “Facebook.com” in your code sample with https://www.facebook.com/?_rdr=p, the script now had an affect; it redirected to Chrome’s about page.
Taking a cue from the necessity of using the full URL in the condition, I switched “twitter.com” to “http://twitter.com/”.
The script now performs as I understand you intended it to: every tab in every window that matches https://www.facebook.com/?_rdr=p gets redirected to twitter.com.
repeat until application "Google Chrome" is not running
if application "Google Chrome" is running then
tell application "Google Chrome"
set (URL of every tab of every window where URL is equal to "https://www.facebook.com/?_rdr=p") to "http://twitter.com/"
end tell
end if
end repeat
Applescript and Google Chrome are matching your conditional’s text against the full URL, so your conditional has to use the full URL as well.
#vadian’s note about using an idle handler is a very good one. See this Hourly Pop-up Alert question for ideas on using an idle handler. Something like this should work:
on idle
if application "Google Chrome" is running then
tell application "Google Chrome"
set (URL of every tab of every window where URL is equal to "https://www.facebook.com/?_rdr=p") to "http://twitter.com/"
end tell
end if
--number of seconds to wait for the next check
return 1
end idle
Save as application, stay open after run handler.

AppleScript: "set frontmost to true" does not work when the screensaver is running?

I have a shell script foo.sh which would keep running in a Terminal window. When some condition is true foo.sh would call an AppleScript bar.osa to make Thunderbird go offline. Following is the bar.osa script (my first applescript):
tell application "System Events" to tell process "Thunderbird"
set frontmost to true
tell menu bar item "File" of menu bar 1
tell menu "File"
tell menu item "Offline"
tell menu "Offline"
set v to (value of attribute "AXMenuItemMarkChar" of menu item "Work Offline") as string
if (v is not equal to "✓") then
click menu item "Work Offline"
end if
end tell
end tell
end tell
end tell
end tell
This works fine for most of the time but when the screensaver is running it does not work. Seems like set frontmost to true does not work when the screen is locked. So how can I make it work even when the screensaver is running?
I am on mavericks, and there is no way I can figure out if the screen saver is running, all I can recommend you to do, is to check the status afterwards of the frontmost property of "Thunderbird".
I have actually spent a good hour on this, and I hope that someone else comes up with a solution, that works! :)
Edit
Actually it was my bad, I thought that launching the ScreenSaver from the System Preferences Preview pane would do it.
When I run the screen saver started from a script, then the running property of it works.
tell application id "sevs" -- "System Events"
set itsRunning to false
if (running of screen saver preferences) then
set itsRunning to true
end if
end tell
if itsRunning then
tell application id "com.apple.ScreenSaver.Engine" to quit
end if
So the solution I propose, is to check if frontmost is true after you have set it frontmost, if the screen saver is running, then turn off the screen saver (requires that you have no password on the screen saver, then rerun the code that sets your app frontmost, and then finally launches the screen saver again.
try
tell application id "com.apple.ScreenSaver.Engine" to launch
end try
You can make this to work in a repeat loop, of course you don't have to use a repeat loop.
Edit+ I was really tired last night: you can really just start out by testing if the screensaver is running, and then either drop going forward, or turn the screen saver off, do your stuff, and turn it on again.

How to close a floating window using AppleScript?

One of my application processes occasionally creates a floating window that I'd like to programmatically auto-close.
I have no problem checking for the existence of this window through its name, but then I am unable to close it with neither close window "windowname" nor tell window "windowname" to close.
E.g.:
tell application "System Events" to tell process "processname"
if exists window "windowname" then
close window "windowname"
end if
end tell
This result in:
error "System Events got an error: window "windowname" of process "processname" doesn’t understand the “close” message."
How can I close this window, then?
I can close a Finder window like this. Maybe you can do the same.
tell application "System Events"
tell process "Finder"
click button 1 of window 1
end tell
end tell
If "Command-W" closes the floating window, which it usually does, this will work:
tell application "System Events"
if exists window "<window name>" of process "<application name>" then
keystroke "w" using command down
end if
end tell
You can also use Automator instead of AppleScript proper to do this. Record yourself clicking the close button.
When I do this, Automator records the action as, I kid you not, Click the "<fill in title>" button.

Resources