How to go from "get bounds" to "set bounds"

Let’s say I have opened a Finder window or two or three, sized them and arranged them the way I want them, all manually with the mouse. Now I would like to have a script that gets the bounds of these windows and remembers them so that I can recreate the same window setup in the future. It’s tedious to have to figure out the bounds of a window and then write a script just to recreate that particular window size and location. I suppose what would be ideal is to be able to create a configuration of windows by hand, and then run a script that will read the bounds of those windows, name that configuration, store it and then call up that configuration by name when needed. Even better, a script that would display a dropdown menu with all the memorized configurations and an option of adding a new one. Wait, wait, wait! Additional options of deleting or editing a memorized configuration.

All the above just came to me as I was intending only to request to learn how to go from getting the bounds of a window to using the results of that “get bounds” command to insert into a “set bounds” command. But I am apparently asking for a script that can remember a “gotten” value and store it for the future. Is this getting too complicated? I hope not.

Many thanks for your patience and indulgence.

Model: MacBook Pro
AppleScript: 2.4
Browser: Safari 537.36
Operating System: Mac OS X (10.10)

Hi Polyvoxx
and welcome to MacScripter

To get you started you could use this to get the positions and bounds of the front most windows
and then apply to the script below.

tell application "Finder" to get the bounds of the front window

https://www.macosxautomation.com/applescript/firsttutorial/11.html

tell application "Finder"
	activate
	set W_One to 1
	make new Finder window
	set bounds of (Finder window W_One) to {100, 100, 500, 500} --position & bounds
	set current view of (Finder window W_One) to list view -- icon view or column view or flow view
	set toolbar visible of (Finder window W_One) to false
	set the sidebar width of the (Finder window W_One) to 0
	set W_2 to 1
	make new Finder window
	set bounds of (Finder window W_2) to {500, 100, 500, 500} --position & bounds
	set current view of (Finder window W_2) to list view -- icon view or column view or flow view
	set toolbar visible of (Finder window W_2) to false
	set the sidebar width of the (Finder window W_2) to 0
end tell

@Polyvoxx

Here is a quick and dirty script describing a way to save the bounds values and retrieve them.

# Grabbed the original bounds
tell application "Finder"
	set {xLeft, yLeft, theWidth, theHeight} to bounds of front window
end tell
# Define the path to the file in which we will store the values
set pathToMyPrefs to (path to preferences as text) & "windowBounds.txt"
# Write the values in a text file
my writeto(pathToMyPrefs, (xLeft as text) & linefeed & yLeft & linefeed & theWidth & linefeed & theHeight, text, false)

# Read the contents of the text file
set {grabbedXLeft, grabbedYLeft, grabbedWidth, grabbedHeight} to paragraphs of (read file pathToMyPrefs)
# Convert the values from text to integer
set {grabbedXLeft, grabbedYLeft, grabbedWidth, grabbedHeight} to {grabbedXLeft as integer, grabbedYLeft as integer, grabbedWidth as integer, grabbedHeight as integer}

# For comparison, display original values and grabbed ones
{{xLeft, yLeft, theWidth, theHeight}, {grabbedXLeft, grabbedYLeft, grabbedWidth, grabbedHeight}}

#=====
(*
Handler borrowed to Regulus6633 - http://macscripter.net/viewtopic.php?id=36861
*)
on writeto(targetFile, theData, dataType, apendData)
	-- targetFile is the path to the file you want to write
	-- theData is the data you want in the file.
	-- dataType is the data type of theData and it can be text, list, record etc.
	-- apendData is true to append theData to the end of the current contents of the file or false to overwrite it
	try
		set targetFile to targetFile as «class furl»
		set openFile to open for access targetFile with write permission
		if not apendData then set eof of openFile to 0
		write theData to openFile starting at eof as dataType
		close access openFile
		return true
	on error
		try
			close access targetFile
		end try
		return false
	end try
end writeto

#=====

You may find more sophisticated scheme in Shane Stanley’s Everyday AppleScriptObjC 3ed available at : https://www.macosxautomation.com/applescript/apps/everyday_book.html

Yvan KOENIG running Sierra 10.12.1 in French (VALLAURIS, France) mardi 8 novembre 2016 09:12:24