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
Related
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
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
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
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
I want applescript to scroll a window all the way up.
I've tried the page up key, the home key, and I've tried looking for a way to scroll using the built in scrolling capabilities of the window, but I've so far been unable to even move the scrolled position at all.
Basically, use a tell app "System Events" statement to send keystrokes and key codes.
In theory, you could use the following:
keystroke page up key
keystroke page down key
keystroke home key
But for me this doesn´t work. The good news is that you can use the key codes instead. I suggest using the excellent free Full Key Codes application to read them, though it is a bit tricky to let it read two keys pressed simultaneously.
The key codes for the fn+ arrow keys-combos are as following:
Page up: fn+ up key: key code 116
Page down: fn+ down key: key code 121
Home: fn+ left key: key code 115
End: fn+ right key: key code 119
So for example if you had a long page open in Safari, and you want to scroll to its end, use
tell application "System Events"
tell application "Safari" to activate
— to see the animation, we wait a moment:
delay 0.5
key code 119
end tell
With browsers you could also use JavaScript:
tell application "Safari" to tell document 1
do JavaScript "window.scroll(0,0)"
end tell
tell application "Google Chrome" to tell active tab of window 1
execute javascript "window.scroll(0,0)"
end tell
The alternative to sending keystrokes is to use GUI scripting.
Caveat: While GUI scripting is more robust than sending keystrokes for a given version of an application, changes in the application's layout in future versions can break your code.
Also:
GUI scripting requires that access for assistive devices be enabled; enabling requires admin privileges:
up to 10.8, this could be done programmatically, system-wide by executing tell application "System Events" to set UI elements enabled to true (required admin privileges)
Sadly, on 10.9+, this no longer works, and apps must be authorized manually, individually - the system will prompt you on first run (requires admin privileges)
however, in both scenarios tell application "System Events" to get UI elements enabled will report whether access is enabled or not.
Determining the right UI element targets can be non-trivial and tedious; using the Accessibility Inspector utility that comes with Xcode helps. The class names reported by this utility correspond to the UI element classes contained in the System Events dictionary; e.g., AXSplitGroup corresponds to splitter group.
The following scrolls Safari 6.0.3's front window to the top (access for assistive devices must be enabled):
tell application "System Events"
# Use Accessibility Inspector to find the desired target.
tell front window of process "Safari"
tell scroll bar 1 of scroll area 1 of group 1 of group 1 of last group
set value of attribute "AXValue" to 0 # Scroll to top.
end tell
end tell
end tell
Update: As a reminder that this type of scripting works well for a given version of an application, the code had to be changed for Safari 8.0.4:
tell application "System Events"
# Use Accessibility Inspector to find the desired target.
tell front window of process "Safari"
tell scroll bar 1 of scroll area 1 of group 1 of group 1 of group 2
set value of attribute "AXValue" to 0 # Scroll to top.
end tell
end tell
end tell