Move all open applications to current desktop "space" with applescript - macos

I have been searching endlessly with no avail on how to move a window between spaces on macos in applescript. The seemingly basic thing I want to do is move all open application windows from any space to the first space. If anyone out there can help, please reach out. There seems to be no straight forward way in doing this in the latest version of macos (Mojave 10.14.4). I am also open to doing this in any another language that will interact with macos if it is possible/simpler.
tell application "System Events"
repeat with proc in application processes where background only is false
tell proc
log "found app: " & name
set processName to name
log count of windows
repeat with win in windows
-- move window to first "space"
end repeat
end tell
end repeat
end tell

I think the only way to do this is just simulating moving spaces using
tell application "System Events" to key code 19 using {control down}
but then your desktop will bounce for each app like crazy.

There's a window manager for macOS called Phoenix that's scriptable using JavaScript.
I wrote a Phoenix script, phoenix-move-windows that moves windows based on the current monitor and screen layout. It is configurable so that it will move windows to a specified position, and you can have different arrangements based on the current number of screens, and number of spaces on each screen.
It does support moving all windows to a specified screen and space, using the "defaultBinding" in an arrangement.

Related

Is it possible to get a list of all windows on the screen on OS X

I was wondering if there is a way to programatically get a list of all windows on the screen, including windows that are outside the current application.
I will explain what I am trying to achieve. I am a linux user who recently started using a mac. I was using the fluxbox desktop manager on linux. When switching between windows, instead of pressing alt+tab repeatedly to cycle to the desired window, I was able to modify fluxbox to jump directly to the window I want. I was able to do that because fluxbox is opensource, I modified the code, setup a shortcut to give focus to a specific opened window of my choice.
I am trying to achieve the same thing on mac and I was wondering if that is even possible. It seems I can access the windows that are owned by my application, but I can't access the windows owned by other applications
I experimented with the Core Graphics library by following this post
I first use CGWindowListCopyWindowInfo to a get a list of CGWindowId
Next I use [NSApp windowWithWindowNumber:windowId] to get a NSWindow. But the NSWindow I get are nil except for windows that are owned by my application.
Is this the way mac is designed and we cannot access windows outside of our own application ? Or is there a way around that ? I am wondering how GUI automation tools work on Mac if Mac is designed to prevent an application from controlling windows of other applications.
The tool you likely want here is AppleScript. As an example of how you'd gather this, this script collects the names of all the windows and displays them in a dialog:
set names to {}
tell application "System Events"
repeat with theProcess in processes
if not background only of theProcess then
tell theProcess
set processName to name
set theWindows to windows
repeat with theWindow in theWindows
set end of names to (processName & ":\"" & (name of theWindow) & "\"")
end repeat
end tell
end if
end repeat
end tell
set AppleScript's text item delimiters to "
"
display dialog names as text
set AppleScript's text item delimiters to ""

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.

Launch/raise application and cycle through its windows using AppleScript and keyboard shortcut

How can I use AppleScript to create a keyboard shortcut which will:
Launch an application if it's not running.
Bring the application to the foreground if it is running.
Cycle through only the application's windows if I keep pressing the shortcut keys.
For example, when I press Option-Y, it will launch Terminal or bring it to the foreground, and if I keep pressing Option-Y it will cycle through the multiple open Terminal windows if I have more than one open.
I have successfully created an AppleScript using Automator as a service. I have successfully mapped this script to a keyboard shortcut by going to System Preferences, Keyboard, Shortcuts, Services and under General, checking and assigning a keyboard shortcut to the Automator service BringTerminalToTheForeground.
Here is the script to launch the Terminal:
tell application "Terminal"
activate
end tell
I want to do this only with the tools which come with the Mac by default (e.g. AppleScript, Automator). I'm running El Capitan.
Command-"`" will cycle through an application's windows.
tell application "Terminal" -- try other applications. This script might work in other cases. It happens to work with Terminal but I haven't tested it.
activate
set c to count of windows -- we need to know how many windows the application has so we only cycle through all of them once.
tell application "System Events"
repeat c times
keystroke "`" using command down -- this is what cycles through the windows. This doesn't work in every application.
delay 1 -- this is a 1-second delay. Change this to whatever delay you want.
end repeat
end tell
end tell
I have exactly what you are asking for, but I'm using hammerspoon, Karabiner Elements and GokuRakuJoudo (optional but highly recommended).
First you have to create this lua module using Hammerspoon https://github.com/fabiomcosta/keyboard/blob/73b22906b31dfc12cc834c382515e30c4450d0f5/hammerspoon/hyper.lua#L76 then on my karabiner.edn (using Goku) I define a keymapping calling that function using Hammespoon's cli hs.
It's not easy to setup and in fact that's why I'm looking for a new way to achieve the same behavior without this much setup haha.
But it definitely works.

In applescript, is there any way to specify which desktop a file is opened in?

When my system boots I would like to run applescript that opens files in three different desktops/spaces.
First Space: Mail and Things (my to do list program)
Second Space: Textmate and a Safari for my first project
Third Space: Textmate and a Safari for my second project
First, in Mission Control I created two more desktops which will remain there the next time my system boots unless they are manually removed. Instead of creating one long script, I chained three applescripts (boot1, boot2 and boot3) to break it up into simpler blocks of code. At the end of boot1 you will see:
run script file "<drive name>:Users:<username>:boot2.scpt"
In boot2 and boot3 you will see a bunch of delay lines. One thing I dislike about applescript is that it often starts processing the next command before the OS finishes responding to the prior one. This causes inconsistencies and errors. Delays are a hack to force things to slow down. They help, but even when you use them things are still a bit dicey. In boot2.script:
# this emulates the keyboard shortcut to move to desktop 2
# there doesn't seem to be any way to modify an `open` command to open a file on desktop 2
tell application "System Events"
delay 2
# key code 19 is the key code for the number 2.
# <cntl> 2 is the shortcut to get to desktop 2
key code 19 using control down
end tell
tell application "TextMate"
activate
# 'sites' is the name of the directory my projects are in
open "/users/<username>/sites/project1/"
end tell
tell application "Terminal"
activate
do script "cd /users/<username>/sites/project1/"
delay 2
do script "rails s" in front window
delay 2
tell application "System Events" to tell process "Terminal" to keystroke "t" using command down
tell application "System Events" to tell process "Terminal" to keystroke return
delay 2
do shell script "open -a Safari http://localhost:3000"
end tell
OK... so this mostly works to get desktop 2 in place except for inconsistencies when the delays aren't long enough. Boot3.script is almost the same as boot2 but when trying to open an application on desktop 3, because there is a window on desktop 2 the system jumps back to that desktop. This is the next problem. How do I overcome that?
2305491 is no longer relevant because space preferences are gone.
Thanks.
Boot3.script is almost the same as boot2 but when trying to open an application on desktop 3, because there is a window on desktop 2 the system jumps back to that desktop.
There is an option in the Mission Control Preferences called "When switching to an application, switch to a Space with open windows for the application". Uncheck this.
OK... so this mostly works to get desktop 2 in place except for inconsistencies when the delays aren't long enough.
Better solution is always something like this
repeat until something exists
delay 0.1
end repeat

Apple Script: Resize and position Virtual Keyboard / KeyboardViewer

If these are too many questions in 1, please tell me it and I edit the question and split it into 3? question.
I am currently writing some apple scripts and tried some things with it.
I want to:
open virtual keyboard (keyboardViewer)
resize it
position it
deactivate controls like close restore and minimize
My scripts are these:
App 1:
tell application "System Events"
if not (exists (process "KeyboardViewer")) then
do shell script "/usr/bin/keyboardViewer"
end if
end tell
App 2:
tell application "System Events"
if exists (process "KeyboardViewer") then
click (process "KeyboardViewer"'s window 1's buttons whose subrole is "AXCloseButton")
end if
end tell
I am using the keyboardViewer by https://github.com/nriley/keyboardViewer in /usr/bin/
Seems I can not script my application. Resizing and positioning works with some applications but not with this one. Also hiding the controls on the top left of the window in mac os x is not possible.

Resources