Friday, May 27, 2022

#1 2022-01-18 11:02:59 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1327

Script to position and resize the frontmost app

This script sets the position and size of the frontmost app to specified values. The user can add or remove apps by editing the appData record, and apps not included in this record are set to the "Default" values. The format of each item in the appData record is:

|App Name|:{x position, y position, window width, window height}

The script will optionally reposition the cursor to a location within the frontmost app. To enable this feature, add the app's name to the cursorApps list.

As currently written, the script only works on the frontmost window of an app. If the user prefers that the script work on every window, remove line 1 below and replace it with line 2.

Applescript:

tell application "System Events" to tell process (activeApp as text) to tell window 1
tell application "System Events" to tell process (activeApp as text) to tell every window

This script works on the premise that the name of an app and its process name are the same. I have only encountered one instance where this was not the case, and, if encountered, this is easily fixed within the script on a case-by-case basis.

To test this script, open it in Script Editor or Script Debugger and run.

Applescript:

use framework "AppKit"
use framework "CoreGraphics"
use framework "Foundation"

on main()
   set appData to {|Default|:{510, 33, 900, 750}, |Finder|:{375, 33, 1170, 700}, |Mail|:{380, 33, 1160, 987}, |Preview|:{468, 33, 984, 1037}, |Safari|:{160, 23, 1600, 1057}, |Script Debugger|:{480, 23, 960, 1057}, |Script Editor|:{480, 23, 960, 1057}} -- do not delete Default entry
   set appData to current application's NSDictionary's dictionaryWithDictionary:appData
   
   set cursorApps to {"Finder", "Safari", "Script Debugger"} -- reposition cursor in these apps
   set cursorApps to current application's NSArray's arrayWithArray:cursorApps
   set cursorPosition to {30, 30} -- cursor position relative to upper-left corner of app
   
   set activeApp to current application's NSWorkspace's sharedWorkspace()'s frontmostApplication()'s localizedName()
   set windowData to (appData's valueForKey:activeApp) as list
   if windowData = {missing value} then set windowData to (appData's valueForKey:"Default") as list
   set moveCursor to cursorApps's containsObject:activeApp
   
   try
       tell application "System Events" to tell process (activeApp as text) to tell window 1
           set position to {item 1, item 2} of windowData
           set size to {item 3, item 4} of windowData
       end tell
   on error
       error number -128
   end try
   
   if moveCursor = true then -- thanks Mark FX
       set cursorPoint to current application's NSMakePoint((item 1 of windowData) + (item 1 of cursorPosition), (item 2 of windowData) + (item 2 of cursorPosition))
       current application's CGWarpMouseCursorPosition(cursorPoint)
   end if
end main

main()

Last edited by peavine (2022-01-19 08:36:51 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

#2 2022-01-25 07:42:39 am

peavine
Member
From:: Prescott, Arizona
Registered: 2018-09-04
Posts: 1327

Re: Script to position and resize the frontmost app

The operation of this script is similar to that above, differing in that it resizes all active apps, does not reposition the cursor, has a do-not-resize list, and makes hidden apps visible.

Applescript:

use framework "AppKit"
use framework "Foundation"
use scripting additions

on main()
   set appData to {|Default|:{510, 33, 900, 750}, |Finder|:{375, 33, 1170, 700}, |Mail|:{380, 33, 1160, 987}, |Preview|:{468, 33, 984, 1037}, |Safari|:{160, 23, 1600, 1057}, |Script Debugger|:{480, 23, 960, 1057}, |Script Editor|:{480, 23, 960, 1057}} -- do not delete Default entry
   set appData to current application's NSDictionary's dictionaryWithDictionary:appData
   set doNotResize to {}
   
   set openApps to current application's NSWorkspace's sharedWorkspace()'s runningApplications()
   set thePredicates to current application's NSPredicate's predicateWithFormat:"activationPolicy == 0 AND NOT localizedName IN %@" argumentArray:{doNotResize}
   set openApps to (openApps's filteredArrayUsingPredicate:thePredicates)'s valueForKey:"localizedName"
   
   repeat with anApp in openApps
       set windowData to (appData's valueForKey:anApp) as list
       if windowData = {missing value} then set windowData to (appData's valueForKey:"Default") as list
       try
           tell application "System Events" to tell process (anApp as text)
               if (visible = false) then set visible to true
               tell every window
                   set position to {item 1, item 2} of windowData
                   set size to {item 3, item 4} of windowData
               end tell
           end tell
       end try
   end repeat
end main

main()

Last edited by peavine (2022-02-10 11:18:05 am)


2018 Mac mini - macOS Monterey - Script Debugger 8

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)