Search code examples
applescriptfinder

Save and restore Finder windows list


I don't remember exactly how I did it but I used to have keyboard shortcuts mapped with Butler that ran some AppleScript I wrote thanks to multiple sources.

One script was used to save all Finder windows properties to a text file (prompt for name), and the other would read the file (prompt for name again) and restore Finder windows accordingly.

Text files would look like that:

{
    {
        folder "Desktop"
        of folder "Me"
        of folder "Users"
        of startup disk of application "Finder", {
            0, 0, 1200, 315
        }, column view, 192
    }, {
        folder "Backups"
        of disk "SAFE"
        of application "Finder", {
            0, 380, 1200, 685
        }, column view, 192
    }
}

From there, I know how to restore the windows. I just lost the script I made, that retrieves all these values...

To restore windows :

tell application "Finder"

set windowList to {
    {
        folder "Desktop"
        of folder "Me"
        of folder "Users"
        of startup disk of application "Finder", {
            0, 0, 1200, 315
        }, column view, 192
    }, {
        folder "Backups"
        of disk "SAFE"
        of application "Finder", {
            0, 380, 1200, 685
        }, column view, 192
    }
}
close every window

    repeat with i from 1 to count of windowList
        set theseProps to item i of windowList
        make new Finder window at front to item 1 of theseProps
        tell window 1
            set bounds to item 2 of theseProps
            set current view to item 3 of theseProps
            set sidebar width to item 4 of theseProps
        end tell
    end repeat
end tell

My question : How to get all Finder windows and properties with AppleScript? And how to save those to a file?

[EDIT]

Here is how to get all properties needed :

set windowsList to {}
tell application "Finder"
    set theWindows to windows
    repeat with theWindow in theWindows
        set t to target of theWindow
        set b to bounds of theWindow
        set v to current view of theWindow
        set w to sidebar width of theWindow
        copy {t, b, v, w} to end of windowsList
    end repeat
end tell

Now I need a way to save this in a file, being able to load it as a list later on...

[EDIT]

I'm able to write to a file this way :

    set prefs_folder to path to preferences folder as string
set prefs_file to prefs_folder & "finderwindows"
try
    set open_file to ¬
        open for access file prefs_file with write permission
    -- erase current contents of file:
    set eof of open_file to 0
    write windowList to open_file starting at eof
    close access open_file
on error
    try
        close access file prefs_file
    end try
end try

But the file remains empty. The script is exiting with error -10004...

My question is now :

How to convert the windowList to a file savable format I can then retrieve by reading from another script??


Solution

  • FYI: I have a program called Simple WindowSets that does exactly this... save and restore sets of Finder windows. It's very popular. So although you can do as you ask using applescript, maybe you'd be interested. Find it here.

    By the way, to answer your specific question, here's a handler I use for writing to a file. Good luck.

    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 text
            if targetFile does not contain ":" then set targetFile to POSIX file targetFile as text
            set openFile to open for access file targetFile with write permission
            if apendData is false 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 file targetFile
            end try
            return false
        end try
    end writeTo