-
-
Save ekinolik/ef18395b0ca60e7186623dea04d63a79 to your computer and use it in GitHub Desktop.
AppleScript to save and restore window position and sizes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- allSettings is a list of records containing {width:? height:? apps:{{win:? name:? pos:? size:?},...} | |
-- for each display setup store the apps with each window and their associated position and size | |
property allSettings : {} | |
on run argv | |
-- if ran from the CLI, check if the "Save" or "Restore" argument was provided to bypass the prompt | |
if (count of argv) > 0 then | |
set myAction to item 1 of argv | |
else | |
set myAction to "" | |
end if | |
-- create a variable for the current settings | |
set currentSettings to {} | |
try | |
set display_state to "" | |
set locked_state to "" | |
set display_state to do shell script "ioreg -n Root -d1 -a|grep -A1 kCGSSessionOnConsoleKey|grep -o 'true'" | |
set locked_state to do shell script "ioreg -n Root -d1 -a|grep -A1 CGSSessionScreenIsLocked|grep -o 'true'" | |
end try | |
if myAction is not "save" and myAction is not "restore" then | |
display dialog "Restore or save window settings?" buttons {"Restore", "Save"} default button "Restore" | |
set myAction to button returned of result | |
end if | |
tell application "Finder" | |
-- use the desktop bounds to determine display config | |
set desktopBounds to bounds of window of desktop | |
set desktopWidth to item 3 of desktopBounds | |
set desktopHeight to item 4 of desktopBounds | |
set desktopResolution to desktopWidth & "x" & desktopHeight | |
-- find the saved settings for the display config | |
repeat with i from 1 to (count of allSettings) | |
if (w of item i of allSettings is desktopWidth) and (h of item i of allSettings is desktopHeight) then | |
set currentSettings to item i of allSettings | |
end if | |
end repeat | |
if (count of currentSettings) is 0 then | |
-- add the current display settings to the stored settings | |
set currentSettings to {w:desktopWidth, h:desktopHeight, apps:{}} | |
set end of allSettings to currentSettings | |
--say "creating new config for " & desktopResolution | |
else | |
--say "found config for " & desktopResolution | |
end if | |
end tell | |
tell application "System Events" | |
if (myAction is "Save") then | |
-- say "saving" | |
repeat with p in every process | |
if background only of p is false then | |
set windowList to {} | |
tell application "System Events" to tell application process (name of p as string) | |
set appName to name of p | |
if (count of windows) > 0 then | |
repeat with w from 1 to (count of windows) | |
set currentWindow to {win:w, name:appName, size:size of window {w}, position:position of window {w}} | |
copy currentWindow to end of windowList | |
end repeat | |
set appSize to size of window 1 | |
set appPosition to position of window 1 | |
else | |
set currentWindow to {win:0, name:appName, position:0, size:0} | |
copy currentWindow to end of windowList | |
end if | |
set appSettings to {} | |
repeat with i from 1 to (count of apps of currentSettings) | |
if name of item i of apps of currentSettings is name of p then | |
set appSettings to item i of apps of currentSettings | |
end if | |
end repeat | |
repeat with w from 1 to (count of windowList) | |
copy item w of windowList to end of apps of currentSettings | |
end repeat | |
end tell | |
end if | |
end repeat | |
end if | |
if (myAction is "Restore") then | |
if (count of apps of currentSettings) is 0 then | |
-- say "no window settings were found" | |
else | |
-- say "restoring" | |
repeat with i from 1 to (count of apps of currentSettings) | |
set appSettings to item i of apps of currentSettings | |
set appName to (name of appSettings as string) | |
try | |
tell application "System Events" to tell application process appName | |
if (count of windows) > 0 then | |
set win to win of appSettings | |
set position of window win to position of appSettings | |
set size of window win to size of appSettings | |
end if | |
end tell | |
end try | |
end repeat | |
end if | |
end if | |
end tell | |
end run |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment