Max Well
Max Well

Reputation: 337

AppleScript: Save/Restore the Bounds of Every Open Window by Path/Name

I found this solution to be useful in capturing the names and bounds of currently open windows.

AppleScript - Get the Bounds of Every open Window

However, once I have the array of window names and coordinates, I'm trying to figure out how to use Applescript to search through that data saved in a text file for each currently open window, and if a match for the name is found, return the saved window bounds for that window name, and apply the bounds to that window.

This is what I was trying, but after an hour or two, I still can't seem to get it working. I keep running into one error or another.

RestoreFW1.applescript:

-- Get the list of window names
set winNames to {}
tell application "Finder"
    repeat with win in every window
        set winName to name of win
        set end of winNames to winName
    end repeat
end tell

-- define the file path
set desktopPath to (path to desktop as text)
set filePath to desktopPath & "finderWindows.txt"

-- Iterate through the list of window names and set their size and position if a match is found in the file
repeat with winName in winNames
    set shellCommand to "awk -F',' '{if ($1==\"" & winName & "\") print $2,$3,$4,$5}' " & filePath
    set {winX, winY, winWidth, winHeight} to (do shell script shellCommand)
    if (winX is not "") and (winY is not "") and (winWidth is not "") and (winHeight is not "") then
        tell application "Finder"
            set theWindow to every window whose name is winName
            if (count of theWindow) > 0 then
                set bounds of (item 1 of theWindow) to {winX, winY, winX + winWidth, winY + winHeight}
            end if
        end tell
    end if
end repeat

And ultimately, I would want to figure out how to save the current paths of open Finder windows instead of just their name.

From there, hopefully I can figure out how to use a RegEx query to do partial path matching so that, in case I dove into a subfolder or went up to a parent folder of a window with saved bounds, I'll still be able to restore the bounds of that window from the saved data.

For reference/example: here's the script I'm using to collect window names and bounds.

GetFW.applescript:

tell application "System Events"
    set _W to a reference to windows of application "Finder"
    [_W's name, _W's size, _W's position]
end tell

This will return an array in the following format.

FWdata:

{{"windowName1", "windowName2", "windowName3", "windowName4", "windowName5", "windowName6", "windowName7", "windowName8", "windowName9"}, {}, {{1944, 144}, {2605, 66}, {1130, 559}, {0, 559}, {1130, 22}, {0, 22}, {0, 1042}, {2583, 44}, {2561, 22}}}

The RestoreFW1.applescript I generated myself with help from ChatGPT. After several attempts at error correction, I still couldn't get it working correctly.

The problem is I think the shell command awk function is trying to parse a csv list of values (winName,x,y,w,h) and the data set from the getFW.applescript isn't returning them that way.

I'm leaving out the part in the getFW.applescript to store the result to a file as I know this works okay. The problem is with not knowing how to search through the FWdata resulting from the getFW.applescript to find a the name of each currently-open window in Finder, and for any window name found in the FWdata, return the bounds stored for it.

I think the last part of the RestoreFW1.applescript works okay to restore the window bounds. The main problem I have is not knowing how to properly format the awk query to parse the FWdata, search for each windowName in the list of current Finder windows, and return the bounds stored in the FWdata for any windowName found in the data.

As written above, GetFW.applescript returns the following error:

error "Finder got an error: Can’t get item 3 of every window." number -1728 from item 3 of every window

I've Googled this error furiously to no avail. No idea what it means, or why this isn't working.

However, I am able to get a list of window names using:

getFWnames:

tell application "Finder"
    set _W to a reference to windows
    set _Wnames to a reference to [_W's name]
    return _Wnames
end tell

But that returns the list in a similar format (maybe JSON object or array?) as the getFW script, and I don't know how to parse it to search for each name in the saved FWdata.

Please help. My brain hurts. 😭

Upvotes: 0

Views: 118

Answers (0)

Related Questions