Work with selected application

applescript

I am trying to write a AppleScript which will move my active app window to second display in full screen mode. In order to do this I am looking for a way to:

  1. Target a selected application. Doesn't matter whether it is Firefox, Spotify or Calendar.

  2. Move selected app to second display.

  3. Display selected app in full-screen view.

I am struggling to find a way how to target selected/active app/app window. Any idea? I am running macOS High Sierra.

Best Answer

The information you need in order to work through your project are in those other threads:

1) Applescript to click green (zoom) button with Option down
    --->   You can find there (^) how to fullscreen a window (or only maximise it)

2) Retrieve the screen's resolution of a window [which screen is used?]
    --->   Information on how size and position are defined on (different) screens

If you start this script (or app) from Editor or Apple's -used-items-menu you will need lines 2 & 3.
Otherwise, e.g. called by a shortcut, they must be deleted.
ANYWAY: Better test this script with a new empty window!

Your script might look like this:

tell application "System Events"
set visible of first process whose frontmost is 1 to 0   -- These 2 lines MAY be
delay 0.2                                                -- necessary, or may NOT.

set dTopSize to size of scroll area 1 of process "Finder" as list  -- Screen size
set frontApp to first process whose frontmost is true    -- Frontmost app
set win_Size to size of window 1 of frontApp as list     -- Window size

-- EITHER [1.]: to move a window onto a screen to its right side:
set position of window 1 of frontApp to {item 1 of dTopSize, 22}
perform action "AXPress" of (first button whose subrole is "AXFullScreenButton") ¬
  of window 1 of frontApp

-- OR [2.]: to move a window onto a screen to its left side (=> negative value):
set position of window 1 of frontApp to {(item 1 of win_Size) * -1, 22}
perform action "AXPress" of (first button whose subrole is "AXFullScreenButton") ¬
  of window 1 of frontApp
end tell

I do not use a secondary screen myself, so I can only deduce this code from source 2).
Please take care to use EITHER [1.] OR [2.] … if BOTH are called, funny things may happen …
(Be aware that fullscreen mode temporarily "creates" an additional work space.)
You might want to test behaviour with "AXZoomWindow" instead of "AXPress".

Please post your experience(s) and/or problems with this code here !!!