Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Positioning a window with AppleScript using dual monitors

I have two monitors set up and I am trying to position the window of an application in the second monitor but nothing I do seems to work. For example I am using my laptop and the terminal window is maximized on the screen. Then I plug in an external monitor. I then want to run the applescript and have the terminal maximize on the larger second monitor.

Here is what I have right now:

set monitorTwoPos to {1050, -600}
set monitorTwoSze to {1200, 1920}

tell application "Microsoft Outlook"
    set position of window 1 to monitorTwoPos
    set size of window 1 to monitorTwoSze
end tell

Here is the error I get:

/Users/vcutten/AppleScripts/SpacesWork.scpt:1291:1332: execution error: 
Microsoft Outlook got an error: Can’t make position of window 1 into type specifier. (-1700)

I'm pretty sure I'm just using set position and set size completely wrong :( When I used bounds it kind of works...

Bonus Question: How can I loop through the open windows and get their size? Thanks!

like image 653
gdoubleod Avatar asked May 03 '11 06:05

gdoubleod


1 Answers

What have you tried?

I think to solve this you need to calculate the screen size and coordinates of the second monitor. For example, your main monitor starts at position {0,0}. So the starting position of the second monitor has to be something different and you need to find that. Luckily I have written a tool that will give you both the starting coordinates and screen size of your monitors. Once you have the size and position then it's simple. System events can set the size and position of a window so you could do something like this...

set monitorSize to {800, 600}
set monitorPosition to {-800, 0}

tell application "System Events"
    tell process "Terminal"
        set frontWindow to first window
        set position of frontWindow to monitorPosition
        set size of frontWindow to monitorSize
    end tell
end tell

So from the above script you just need the size and position variables. You can get my tool here called hmscreens which will give you those. You may need to do some adjusting of the coordinates depending on if the screen is measured from the lower left corner or upper left, but that's just simple math.

I hope that helps...

like image 80
regulus6633 Avatar answered Nov 15 '22 06:11

regulus6633