Saving and Recalling Window Positions With Automator

You can use JavaScript in Automator to save the positions of each window in each app to a file. Then you can recall those positions later if you want to resume working with the same window locations. This script will save as many position settings as you like and then allow you to recall them easily. You can even set it up as a keyboard shortcut.


// Set this to be the folder where you save the files. Remember to create it!
var filePath = '/users/macmost/Window Positions/';

var app = Application.currentApplication();
app.includeStandardAdditions = true;

// get the list of files in the special folder
var fileList = Application("System Events").folders.byName(filePath).diskItems.name();
if (fileList.length > 0) {

	// choose a file
	var whichFile = app.chooseFromList(fileList, {withPrompt: "Which Positions?", defaultItems: fileList[0], cancelButtonName: 'Save/Cancel'});
}

if (!whichFile) {
	// hit the save/cancel button, so save
	saveWindowPositions();
} else {
	// otherwise a file was selected, so load
	loadWindowPositions(whichFile[0]);
}



function saveWindowPositions() {

	// prompt for the filename
	try {
		var prompt = app.displayDialog('Save positions under what name?', {defaultAnswer: "My Window Positions"});
	} catch (e) {
		// do nothing if cancelled
		return;
	}
	var positionsName = prompt.textReturned;
	
	var savedPositions = [];
	
	// loop through all applications
	var appList = Application("System Events").applicationProcesses.whose({visible: true}).name();

	for (appNum in appList) {
		var appName = appList[appNum];
		
		// loop through all windows for application
		var windowList = Application(appName).windows;
		
		for (windowNum in windowList) {
			var whichWindow = windowList[windowNum];
			
			// put the app name, window number and location in the list
			savedPositions.push({app: appName, window: windowNum, bounds: whichWindow.bounds()});
		}
	}
	
	// open a new file, overwrite if needed
	var file = app.openForAccess(filePath + positionsName, { writePermission: true });
	app.setEof(file, { to: 0 });
	
	// write data to file
	app.write(JSON.stringify(savedPositions), {to: file});
	app.closeAccess(file);
}



function loadWindowPositions(filename) {
	
	// get saved positions from file
	var fileText = app.read(Path(filePath + filename));
	var savedPositions = JSON.parse(fileText);

	// loop through windows in list
	for (windowNum in savedPositions) {
		var windowData = savedPositions[windowNum];
		
		// if there is a window for that app, then set its positions
		if (windowData.window < Application(windowData.app).windows.length) {
			Application(windowData.app).windows[windowData.window].bounds = windowData.bounds;
		}
	}
}

Comments: 3 Responses to “Saving and Recalling Window Positions With Automator”

    Dana Stevens
    5 years ago

    Gary, Great stuff. I have AppleScripts for this but look forward to trying it with JavaScript. Thanks for posting the video.

    Michael Martiny
    5 years ago

    When we are doing scripts like this where do we save them so they run? I am new to this kind of thing.

    5 years ago

    Michael: Depends. For this, you can save them where you like. Create a "Scripts" folder or something in your Document folder. For Quick Actions (AKA Services), they automatically go in a special folder.

Comments Closed.