Applescript to open an application in full-screen mode? - macos

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

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 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.

How can I activate an application in AppleScript without switching desktops?

I'm trying to write an AppleScript that will open a new Safari window on my current desktop space. That's easy enough. The catch is that I've already got a Safari window running on another space, and I don't want to switch to that space, or have the new window show up on that space.
Is there a way to tell AppleScript to open the new window on the current space regardless of which spaces Safari might be active on?
I've got two options for the basic AppleScript. The first switches to the space that Safari is active on, and then opens the new window;
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Window" of menu "File" of menu bar 1
end tell
end tell
The second just opens a new window on Safari's current space, without switching desktops.
tell application "Safari"
make new document
end tell
Can't you just put the activate command after the command for opening a new window?
tell application "Safari"
make new document
activate
end tell

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

Mac: reloading document in Chrome or Firefox?

How can I tell Chrome or Firefox to reload the document in the top window? Here's what I'm using for Safari:
osascript -e '
tell application "Safari"
activate
do JavaScript "history.go(0)" in document 1
end tell
'
Here's the code for Chrome:
tell application "Google Chrome"
tell the active tab of its first window
reload
end tell
end tell
Or more concisely:
tell application "Google Chrome" to tell the active tab of its first window
reload
end tell
I do not think Firefox or Chrome have special Applescript support, but you can send the keystrokes (Cmd-R) to refresh the page:
tell application "Firefox"
activate
tell application "System Events" to keystroke "r" using command down
end tell
Here's another way to do it in Safari without using JavaScript:
tell application "Safari"
tell its first document
set its URL to (get its URL)
end tell
end tell
Automator
Open Automator and choose a New Document
Choose Service
Set Service receives to no input
Choose Run AppleScript action from the action list.
Paste the following code in the script:
 
tell application "Google Chrome" to tell the active tab of its first window
reload
end tell
Save the the service, for example, using the name Chrome Refresh
System Preferences
Open System Preferences > Keyboard
In the Shortcuts tab, choose Services
Assign a new shortcut
The following answers above work well but using them result in DevTools refreshing in a new tab if it was the last tab/window in focus. I don't want DevTools to refresh in a new tab, I just want the first tab to refresh regardless of last focus/active and this worked well for me. Leaving for someone searching for this use case as well.
tell application "Google Chrome"
activate
tell application "System Events"
tell process "Google Chrome"
keystroke "r" using {command down, shift down}
end tell
end tell
end tell

Resources