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

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

Related

AppleScript: Toggle Between Safari Windows?

If I have two safari windows open, just one tab in each, how do I get applescript to switch to either of the windows at will? Or in other words, toggle between them.
I tried, activate window 1 (or, the first window), and activate window 2 (or the second window), only ever activates the first window.
I tried open window 1 etc, open doesn't exist.
I tried using the system events, click menu bar 1 option, thinking maybe menu bar 2 was for window 2, didn't work.
I tried making do javascript on a specific tab show that page, couldn't get that to work.
Ultimately I did figure out I could use a keyboard shortcut, but I wanted to see if there was a more 'vanilla' applescript way.
If the windows are not full screen (in different spaces) just change the index of the window
tell application "Safari"
set index of window 2 to 1
end tell
If the windows are in different spaces you have to switch the spaces by executing the keystrokes with System Events. The default values on the US keyboard are ⌃←
tell application "System Events"
key code 123 using (control down)
end tell
and ⌃→
tell application "System Events"
key code 124 using (control down)
end tell
When I do just this
tell application "Safari"
set index of window 2 to 1
end tell
The new window that shows up is frozen. I fixed this by doing this
tell application "Safari"
set theWindows to windows
set win2 to item 2 of theWindows
tell win2
set visible to false
set visible to true
set index to 1
end tell
activate
end tell

Control Finder's window "tab-bar" visibility with Applescript?

Is there a way in Applescript to control visibility of a Finder's window "tab-bar" (don't know the english name)?
I know Applescript can get/set statusbar and toolbar visibility, but found nothing of this "tab-bar", the one that let you have multiple tabbed windows).
This AppleScript code works for me using the latest version of macOS Mojave.
tell application "Finder"
activate
delay 0.1
if not (exists of window 1) then reveal desktop
delay 0.1
tell its window 1
activate
repeat until visible
delay 0.5
end repeat
delay 1
tell application "System Events" to key code 17 using {command down, shift down}
end tell
end tell
SIDE-NOTE: This code will not work if your Finder window 1(Frontmost Finder Window) is open with more than one tab already opened

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

AppleScript works in Script Editor but not as application

I am pretty new to programming, especially with AppleScript. I wrote a simple script for Valentine's Day to play a song from iTunes and then open a flash animation file in Safari. When I run the script in ScriptEditor, everything works as desired, but when I export as a standalone application, it fails at the command to enable full-screen mode. I am assuming it is an issue with System Events. To be clear, the application functions to the end, but at the keystroke command I hear an alert sound and the window remains as-is.
I am running Yosemite, and am fully updated.
Ideally, I would like to open the file in Google Chrome to utilize Presentation Mode, but I can't even get Chrome to open the file.
Thanks for any advice! Here is the code:
tell application "Finder"
set visible of every process whose visible is true and name is not "Finder" to false
close every window
end tell
set volume output volume 75
tell application "iTunes"
set currentVolume to sound volume
if player state is playing then
stop
back track
end if
play track "The Promise"
set player position to 6
end tell
delay 4
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
end tell
I agree with Jerry Stratton's comment that it could be an accessibility issue. However it also could be that you are issuing the keystroke command before Safari is ready to accept it. If it's opening a file then it could be busy and miss the keystroke command.
Also, I would move the system events code outside the Safari code and also just tell system events, rather than the Safari process, to perform the keystroke command. Try this as the Safari and System Events parts.
NOTE: I can't get Chrome to open a file either.
tell application "Safari"
activate
if (count of windows) is 0 then -- Remove "if" statement if you don't want to make a new window if there is none
make new window at front
end if
open (POSIX path of (path to home folder)) & "/Desktop/beMine/beMine.swf"
end tell
tell application "Safari" to activate
delay 1
tell application "System Events"
keystroke "f" using {command down, control down}
end tell
Most likely you’ll need to allow your standalone application to use System Events. At some point you needed to do that for Script Editor; you’ll need to do the same for your standalone app.
You’ll find the option in System Preferences under Security & Privacy, then Privacy, and then Accessibility. There’ll be a list of apps, and your app is probably listed there without a check for “Allow the apps below to control your computer.”
You may need to use the “+” button to add your app to the list.
I have verified that I can use this simple script to make Safari full-screen; it will work if the app is given permission under Accessibility, and it will silently fail if not.
tell application "Safari"
activate
end tell
tell application "System Events"
tell process "Safari" to keystroke "f" using {command down, control down}
end tell
This is Yosemite, Mac OS X 10.10; it may be different in other versions of Mac OS X.

Tell PDF Viewer/Adobe Reader to go to page on OS X

I am trying to make an integrated presentation for my students.
System: OS X Lion 10.7
For that I need to show a foreground window running a 3D presentation which is remotely controlled by a command line on another screen.
Besides it I would like to show a small set of PDF slides behind it.
I need some way of "remote controlling" the backgrounded PDF Viewer (Adobe Reader or PDFView) to accept simple commands like
Go to Page x (ideally)
Go to first page (home button)
Go to next page (arrow down, arrow right, page down or scroll down will all do)
I prefer to run the PDF in Adobe Reader fullscreen mode (allows me to background it and overlay another window on top, but PDFView could do too.
I have tried various Applescripts (run with osascript), but with no success:
tell application "System Events"
tell process "Adobe Reader"
tell window "starkdemo.pdf" to key code 125
end tell
end tell
sends key down to my terminal
tell application "/Applications/Adobe Reader.app"
tell active pane of active window
large scroll down 1
end tell
end tell
gives me an error 63:69: script error: Expected end of line but found class name. (-2741)
I have found that the free Viewer Skim has extensive AppleScript scripting http://sourceforge.net/apps/mediawiki/skim-app/index.php?title=AppleScript
It also have the fullscreen view that I like. So I will be using this in the future. Also the applescript command is just one line:
tell document "starkdemo.pdf" of application "Skim" to go to page 7
Try:
activate application "Adobe Reader"
tell application "System Events"
tell process "Adobe Reader"
click menu item "Full Screen Mode" of menu 1 of menu bar item "View" of menu bar 1
delay 2
key code 125
delay 2
keystroke space
delay 2
end tell
end tell
Or try this with Acrobat Pro
tell application "Adobe Acrobat Pro"
tell PDF Window 1
read page down
end tell
end tell
Or
tell application "Adobe Acrobat Pro"
tell PDF Window 1
goto page 3
end tell
end tell

Resources