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

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

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

Applescript to open youtube in safari private mode

How can the youtube be opened in safari private mode?
I have tried this, but it is not working:
tell application "Safari" to activate
tell application "System Events"
tell process "Safari"
click menu item "New Private Window" of menu "File" of menu bar 1
open location "https://www.youtube.com" -- this will open default browser
end tell
end tell
My default browser is Chrome, and it opens youtube in chrome not in safari private mode.
How to fix this?
The following example AppleScript code works for me:
For Safari use:
activate application "Safari"
tell application "System Events" to ¬
click menu item "New Private Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Safari"
tell application "Safari" to ¬
set URL of current tab of ¬
front window to "https://www.youtube.com"
Note: If one prefers, each of the two tell statements can be all on a line of its own by removing the ¬ line continuation character and the invisible linefeed character that follows.
For Google Chrome use:
activate application "Google Chrome"
tell application "System Events" to ¬
click menu item "New Incognito Window" of ¬
menu "File" of menu bar 1 of ¬
application process "Google Chrome"
tell application "Google Chrome" to ¬
set URL of active tab of ¬
front window to "https://www.youtube.com"
Note: If one prefers, each of the two tell statements can be all on a line of its own by removing the ¬ line continuation character and the invisible linefeed character that follows.
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
I don't really have a better solution for Safari than the one offered by #user3439894. The only thing I would have done differently would be to make the new private window using this code (which is probably "6 of 1 or 1/2 a dozen of the other")
tell application "Safari" to activate
tell application "System Events" to tell its application process "Safari"
set frontmost to true
keystroke "n" using {shift down, command down}
end tell
However, you may prefer this solution for Google Chrome because it does not require the use of System Events and it does not require Google Chrome to be active or frontmost.
tell application "Google Chrome"
set incognitoWindow to (make new window with properties {mode:"incognito"})
repeat while loading of active tab of incognitoWindow
delay 0.1
end repeat
set URL of active tab of incognitoWindow to "http://youtube.com"
end tell
tell application "Opera"
try
make new window with properties {mode:"incognito"}
end try
set URL of active tab of front window to "https://yoururl.com"
end tell
I wrapped make new window with properties {mode:"incognito"} in try because I get an error "Opera got an error: AppleEvent handler failed." number -10000 because i use Opera browser.
It is not necessary when using Google Chrome.

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

Break Active Finder Tab Into New Window with Applescript

I am trying to write an applescript to break the active Finder tab into a new window. I am able to do this with Chrome using:
on moveActiveTabToNewWindow()
tell application "Google Chrome"
set theURL to URL of active tab of window 1
close active tab of window 1
make new window
set URL of active tab of window 1 to theURL
end tell
end moveActiveTabToNewWindow
However, as far as I can tell Finder tabs are not accessible via Applescript. Is this possible? I'm on OS X Mavericks 10.9.2.
Manipulating tabs is not accessible directly but you could do it manually. In other words we can close that tab and then open a new window and replicate what you saw in that tab, just like you did in your Google Chrome example.
Here's some code. This code is basic. Just like I got the "target" property from the Finder window, you could get other properties too and replicate them in the new window to really do a more complete job of replicating the tab. You'll probably want to duplicate the bounds and view options at least.
Good luck.
-- get the target of the front tab
tell application "Finder"
activate
tell window 1 to set t to target
end tell
-- close that tab
tell application "System Events"
keystroke "w" using command down
end tell
-- make a new window and set its target
tell application "Finder"
set w to make new Finder window at front
tell w to set target to t
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

Resources