You can also watch this video at YouTube (but with ads).
// 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; } } }
Gary, Great stuff. I have AppleScripts for this but look forward to trying it with JavaScript. Thanks for posting the video.
When we are doing scripts like this where do we save them so they run? I am new to this kind of thing.
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.