How do I make a Safari window active using AppleScript (elegantly)? - applescript

While one can simulate command-shift-` using System Events to change windows in Safari, is there no better way to do this with AppleScript? I can change the index of Safari's windows, this changes their ordering on the screen but does not make them active. Suppose you have two Safari windows containing about:blank -- how could you bring the one in the back to the front and make it active?
Granted, command-shift-` works, so maybe there only needs to be one way to skin this cat.

I also noticed that changing the index does reorder the windows however the new window does not actually come to the front. By playing around I found you can fix that by playing with the "visible" property...
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
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

How do I use AppleScript to close all applications that are hidden?

I have an applescript made to close all apps:
tell application "System Events" to set quitapps to name of every application process whose visible is true and name is not "Finder"
repeat with closeall in quitapps
quit application closeall
end repeat
It works just fine. It quits all open applications.
My problem is that I want to modify this script to close only the apps that are hidden. For some reason, Apple hides all Apple-made apps with no active windows, and eventually it starts eating up my RAM.
What I thought was that if I just change the line whose visible is true to whose visible is false I would get that result.
Not quite:
I don't even know what this window is supposed to be, so I just hit cancel.
Well, it pops up again. Turns out I have to hit cancel exactly four times before the script bombs out.
Is there any way to quit all hidden applications, while leaving open the visible ones?
(Bonus points if you can explain the pop-up above.)
If it makes a difference, I'm running the latest version of OSX.
Setting visible to false affects all processes – even processes / applications without a GUI. If a process is not an application (.app) the application chooser appears.
Add a check for background only which affects only applications with a GUI.
tell application "System Events" to set quitapps to name of every application process whose visible is true and background only is false and name is not "Finder" ...
If I understand correctly, you are talking about how when you press the red close button, it only closes the application's windows, but not the application itself, so it's just left there still open.
In which case, you can use this script I made, it seems it works flawlessly:
-- Get open apps
tell application "System Events" to set openApps to name of every application process whose background only is false and name is not "Finder"
-- Do this for every open app
repeat with openApp in openApps
tell application openApp
-- Tell application to quit if it doesn't have any open windows
if (count of windows) is less than or equal to 0 then quit
end tell
end repeat
OSX doesn't "hide" apps. They just aren't active, or don't have any documents open. Hidden apps is a very specific process usually done with Command-H. Apps don't hide themselves in this fashion.
Rather than trying to close apps with no windows, use the document count to determine whether to close an app.
tell application "SomeApp" if count of documents = 0 then quit
This is a quirky issue. For reasons unknown to me, you have to separate stuff in order to get it to work. This quits every non-background app that's not in the igApp list. I added 'without saving' to deal with document apps like TextEdit but obviously, it's optional. While this could likely be streamlined somewhat, it seems to work reliably.
tell application "System Events"
set igApp to {"Finder", "Script Editor", "firefox"}
set qList to {}
set fApp to name of every application process whose background only is false
repeat with xx in fApp
if xx is not in igApp then
copy contents of xx to end of qList
end if
end repeat
end tell
repeat with yy in qList
quit application yy without saving
end repeat
-- qlist
Note that if you want to quit some (or all) of your background applications you should probably reconsider. Those are doing things in the background and unless something goes awry, they generally don't abuse a computer's resources. Some may require force-quit to stop. If your computer is starved for RAM when it's idle, then either some app is broken/buggy or you need more RAM.
With that said, if you swap 'background process' to true, it should work on any of the background applications. As I said, I recommend against this but you could comment out the 'quit application' line and then uncomment 'qlist' (the last line), and then you'll get a list of those applications. You could look those apps up in Activity Monitor's Memory panel to see how modest these apps' demands are. On my mac, if you exclude obvious apps like 'Dock', combined they use < 200 MB of RAM.

Looking for a way to set (default) zoom level of iWorks app

Scanning the internet I found that in the past one could set the view scale or fit width of an iWorks application like this in Applescript:
tell window 1 of application "Keynote"
set fit width to true
end tell
or
tell window 1 of application "Keynote"
set view scale to 80
end tell
Both these methods don't work in Yosemite anymore (and most likely did not work since Snow Leopard according to some Google searches.
I also looked through the Applescript API Library for Keynote and could not see anything that refers to a view scale or zoom level.
Is there really no way anymore to set these via AppleScript?
My end goal is actually just to have new iWorks apps always be set to "Fit in Window", but lacking the possibility to set and save it via a Theme, I was assuming that AppleScript would be the only possible solution.

move any window outside screen under Mac OS

I am trying to hide (or to move outside teh screen bounds) any window (I mean not belonging to my application).
I tried using Accessibility but unfortunately, there is always a small part of the window which is still on the screen.
Here are my questions:
- Is there a way to completly move a window outside the screen using accessibility?
- Is there an alternative way (Apple Script, ...) to achive that?
Thanks!
I am also using Lion and I am quite sure it was working before.
I am discovering a lot of small things like that in 10.7.
Apple have introduced more changes that it seem.
Thanks for your trial.
I tried the following code with a TextEdit window. It wouldn't work. I tried to move it off all 4 sides of the screen and none of it worked (as you explained there's always a part of the window showing). I'm using 10.7. I'm pretty sure I could do it in 10.6 and earlier... maybe it's a 10.7 thing???
tell application "System Events"
tell process "TextEdit"
set theWindows to windows
repeat with aWindow in theWindows
tell aWindow
set currentPosition to position
set position to {-1000, item 2 of currentPosition}
end tell
end repeat
end tell
end tell

Making a another applications's window frontmost and focused

I've been trying to get any given window from another Application to become front most and focused.
Here are the two methods I have tried:
AppleScript
tell application "Safari"
set index of window "Downloads" to 1
activate
end tell
Why it doesn't work: While it seems to change the z-Order, it doesn't change the focus! The previous frontmost window, even if obfuscated, retains focus.
CoreGraphicServices
CGSWindowOrder(cid, wid, kCGSOrderAbove, nil)
Why it doesn't work: Without being the Universal Owner, you can't change the order of a window owned by another process. In this case, making myself the Universal Owner is not an option -- I don't want to depose the Dock.
Recap
I need to make a window frontmost and focused from one process to another. AppleScript fails to focus, and CGS isn't an option since I don't own the window.
Any solutions/ideas?
Try using the Accessibility API to send an AXPress action to the AXWindow. Obviously, the user will need to have access for assistive devices turned on.

Resources