How Do I Manipulate Windows in AppleScript?

@CJK, you hit the nail on the head!

Minimum size for windows:

Hey CJ,

Vanilla AppleScript considers multiple monitors to be one big Desktop.

----------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2018/07/20 04:38
# dMod: 2018/07/20 04:38 
# Appl: Miscellaneous
# Task: Handler for returning the bounds of the Desktop.
#     : Considers multiple monitors as one big desktop from left to right.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Finder, @Handler, @Desktop, @Bounds
----------------------------------------------------------------

desktopBounds()

----------------------------------------------------------------
--» HANDLERS
----------------------------------------------------------------
on desktopBounds()
   tell application "Finder"
      set _bounds to bounds of window of desktop
      return _bounds
   end tell
end desktopBounds
----------------------------------------------------------------

From there you have to figure out the coordinates of each monitor.

I used to have quite a few multi-monitor scripts, but unfortunately those have been lost for some time now.

It would probably be worthwhile to search MacScripter.net for:

window multiple monitor

And the Applescript Users List for:

multiple monitor site:http://lists.apple.com/archives/applescript-users

-Chris

1 Like

@ccstone yeah, I got the error too when trying to run @CJK on a dual monitor setup (at work). So I found that if you add at the beginning :

set screenWidth to {word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Width") as number}
set screenHeight to {word 3 of (do shell script "defaults read /Library/Preferences/com.apple.windowserver | grep -w Height") as number}

and remove the line

set [screenWidth, screenHeight] to the size of scroll area 1

the script runs correctly.

I have a couple of clues as to how to go about isolating the second monitor with the links you provided but man, this is way more complicated than I had anticipated. This script is for my father who has problem saving his document at the right place, so I just wanted a little script that would help set him up with all is Word documents. The tiling fonction in word is NOT what he wants. Anyway, thank you all for your help!

Well, it doesn't work when running from my MacBook pro only, it's like it sees another resolution or the old resolution from that file. I'll probably have to make an if else statement in KM and run two scripts. Or find a way to deal with variables from KM and put them in my Applescript.

There is no reason to depend on shell scripts for this, when both AppleScript (using ASObjC) and KM native Actions/Tokens/Functions can easily tell you which screen contains the frontmost window.

I'm not sure what other suggestions to offer at this point, because I'm confused on what exactly your current objective is.

In your OP, you stated:

Has that question/goal been solved?

If so, please:

  1. Check the "Solved" checkbox (click for details) at the bottom of that post that best solves it.
  2. Post a new topic if you have other questions.

If not, please tell us specifically what the issue is, and post the script/macro that you are currently trying to make work.

Thanks.

Thanks @JMichaelTX I know I'm a bit all over the place. I try to take baby steps to resolve problems. In that case, i wanted a solution to tile windows on the main monitor. Ultimate goal was to be able to tile windows on the main monitor but ignoring those on the second monitor, which my script still doesn't do. After thinking about it, the option to tile windows on the monitor where the cursor is currently that would solve all my problem. Since you're the expert here, how would you go about doing that? I only need to tile windows from the same app, but on the current monitor (meaning the one with the mouse cursor) or the active window.

The thing I'm trying to solve is that my father has about 6 documents open in word on his second monitor (which is on his left), than, he uses is main monitor to open other documents that can vary in number but that will change over time. I wanted a way for him to hit a key and have those documents magically get tiled WITHOUT affecting the windows on the second monitor. Since this is a very particular workflow, I wanted to make a macro that could be applied to more than just his senario, hensed, the macro I'm currently working on. I'm sorry if i wasn't clear about all this in the first place.

Again thanks for all your help.

When asking for help here, it is best for you and us to give us the complete picture/objective at the very beginning -- do not spoon feed us. :wink:

It sounds like you have stated a new problem. So, again, I'd ask you:

I thought about moving your last post to a new topic, but that is something that I think you should do after answering my above question.

I'd suggest this:

  1. First build the macro that your father needs
    • It is the simplest
    • But it will provide the building blocks for a more complicated multi-monitor macro
    • It will give your father a solution now while you work on other stuff.
  2. Then build the "ultimate" macro you now seek
    • Could be after we help you with #1, you will be able to do #2.
    • Of course, we will still help if need be.

It seems to me that @CJK has solved your OP with this post:

Perhaps it is not perfect, but it does seem to answer your OP.
I think we explained why the window size will have certain minimums in some cases.
If @CJK's post does not solve your OP, please state (without expanding the requirements) in what way specifically it does not work.

Once we get that out of the way, we can move on to your other questions, one-at-a-time. :wink: