Search code examples
macosautomationapplescriptjavascript-automation

How to close all Finder windows using JXA


I basically want to port this code from AS to JXA:

tell application "Finder"
    close every window
    open folder "Projects" of folder "Documents" of home
    tell the front Finder window
        set the bounds to {0, 0, 1920, 1080}
        set the current view to list view
    end tell
end tell

Thanks in advance! There is so little information about JXA out there!


Solution

  • This one-liner will close every Finder window:

    with (Application('Finder')) close(windows)
    

    You could implement the full script like this:

    f = Application('Finder')
    w = f.windows.first
    
    f.close(f.windows)
    f.open(f.home.folders["Documents"].folders["Projects"])
    w.bounds = {"x":0, "y":0, "width":1920, "height":1000}
    w.currentView = 'list view'