/home/alex/dev/finder-grid.applescript (1)

From RaySoft
#!/usr/bin/osascript
-- -----------------------------------------------------------------------------
-- finder-grid.applescript
-- =======================
--
-- Project   Automate macOS
-- Scope     macOS
-- Copyright (C) 2024 by RaySoft, Zurich, Switzerland
-- License   GNU General Public License (GPL) 2.0
--           https://www.gnu.org/licenses/gpl2.txt
--
-- -----------------------------------------------------------------------------
-- Configuration

-- Path to the user's home directory
set myHomeDir to path to home folder as text

-- Defines the paths which are opened in the Finder windows
set myPaths to {myHomeDir & "Documents", myHomeDir & "Documents"}

-- Defines the size of the grid
set myGridX to 2
set myGridY to 1

-- Defines the vertical and horizontal borders
set myBorderX to 200
set myBorderY to 100

-- Defines the vertical and horizontal spacers between the windows
set mySpacerX to 50
set mySpacerY to 50

-- Defines the height of the main menubar
set myMenubarHeight to 22

-- Defines the width of the sidebar of the Finder windows
set mySidebarWidth to 200

-- -----------------------------------------------------------------------------

-- Change the space to the first one
tell application "System Events"
  key code 18 using {control down}
end tell

-- Wait for the space change
delay 0.1

tell application "Finder"
  -- Activate Finder
  activate

  -- Close all Finder windows
  close every window

  -- Get the size of the display
  set {null, null, myDisplayX, myDisplayY} to bounds of window of desktop

  -- Calculate the width of a window
  set myWindowWidth to ( ¬
    myDisplayX - (myBorderX * 2) - (mySpacerX * (myGridX - 1)) ¬
  ) / myGridX

  -- Calculate the height of a window
  set myWindowHeight to ( ¬
    myDisplayY - (myBorderY * 2) - (mySpacerY * (myGridY - 1)) - myMenubarHeight ¬
  ) / myGridY

  -- Initialize the grid coutners
  set {myCoutnerX, myCoutnerY} to {1, 1}

  repeat with myPath in myPaths
    -- Calculate the horizontal position of the window
    set myPositionX to ((myWindowWidth + mySpacerX) * (myCoutnerX - 1)) ¬
                       + myBorderX

    -- Calculate the vertical position of the window
    set myPositionY to ((myWindowHeight + mySpacerY) * (myCoutnerY - 1)) ¬
                       + myBorderY + myMenubarHeight

    -- Make a new Finder window and open the path stored in the variable 'myPath'
    set myWindow to make new Finder window to folder myPath

    -- Configure the Finder window
    tell myWindow
      -- Set the postition and size
      set bounds to { ¬
        myPositionX, myPositionY, ¬
        myPositionX + myWindowWidth, myPositionY + myWindowHeight ¬
      }
      -- Set the width of the sidebar
      set sidebar width to mySidebarWidth
      -- Set the view to 'list view'
      set current view to list view

      -- Configure the 'list view'
      tell its list view options
        -- Set the sort column to 'name'
        set sort column to name column
        -- Set the icon size to 'small'
        set icon size to small icon
        -- Set the text size to '12'
        set text size to 12

        -- Configure the column 'name'
        tell its column name column
          -- Set the sort direction to 'normal'
          set sort direction to normal
        end tell
      end tell
    end tell

    -- Increment the vertical counter
    set myCoutnerY to myCoutnerY + 1

    -- If the vertical counter is bigger than the vertical grid
    if myCoutnerY > myGridY then
      -- Increment the horizontal counter and reset the vertical
      set {myCoutnerX, myCoutnerY} to {myCoutnerX + 1, 1}

      -- If the horizontal counter is bigger than the vertical grid
      if myCoutnerX > myGridX then
        -- Exit the repeat loop
        exit repeat
      end if
    end if
  end repeat
end tell

-- -----------------------------------------------------------------------------

Usage

Compile the script using osacompile
osacompile -x -o "${HOME}/dev/finder-grid.scpt" \
  "${HOME}/dev/finder-grid.applescript"
Execute the script using osascript
osascript "${HOME}/dev/finder-grid.scpt"