Morning Desk

@rebind / morning-deskv0.1.2Public

One key opens your work apps, places each window on its half of the screen, and starts your music.

Files

Older release. Installs get 0.1.2.

main.luau3.2 KB

-- Morning Desk: one key opens your work apps, puts each on its part of the
-- screen, and starts your music.

local IS_MAC = _REBIND.platform == "macos"
local PLACES = { "Left half", "Right half", "Full screen" }

local cfg = UI.Schema({
  key = UI.Keybind("F1", { label = "Open and arrange the day" }),
  firstApp = UI.Text("Linear", { label = "First app", tab = "Apps" }),
  firstPlace = UI.Select(
    "Left half",
    PLACES,
    { label = "First app goes", tab = "Apps" }
  ),
  firstLaunch = UI.Text("", {
    label = "First app launch command (Windows)",
    placeholder = "path to the program, if Windows cannot find it",
    tab = "Apps",
  }),
  secondApp = UI.Text("Notion", { label = "Second app", tab = "Apps" }),
  secondPlace = UI.Select(
    "Right half",
    PLACES,
    { label = "Second app goes", tab = "Apps" }
  ),
  secondLaunch = UI.Text("", {
    label = "Second app launch command (Windows)",
    placeholder = "path to the program, if Windows cannot find it",
    tab = "Apps",
  }),
  musicApp = UI.Text("Spotify", { label = "Music app", tab = "Music" }),
  musicLaunch = UI.Text("", {
    label = "Music app launch command (Windows)",
    placeholder = "path to the program, if Windows cannot find it",
    tab = "Music",
  }),
  playMusic = UI.Toggle(true, { label = "Start playing", tab = "Music" }),
})

-- App.Activate only focuses a running app, so launch it first
local function open(app, launch)
  if not App.IsRunning(app) then
    local ok, err
    if IS_MAC then
      ok, err = pcall(System.ExecDetached, "open", { "-a", app })
    elseif launch ~= "" then
      ok, err = pcall(System.ExecDetached, launch, {})
    else
      ok, err = pcall(System.ExecDetached, "cmd", { "/c", "start", "", app })
    end
    if not ok then
      UI.Notify(`Could not open {app}: {err}`, "warning")
      return false
    end
  end
  -- a cold start takes a moment: keep asking for the app for 5 seconds
  for _ = 1, 50 do
    pcall(App.Activate, app)
    if App.IsFront(app) then
      return true
    end
    Sleep(100)
  end
  UI.Notify(`{app} did not come to the front`, "warning")
  return false
end

local function area()
  if not IS_MAC then
    local ok, rect = pcall(Screen.WorkArea)
    if ok then
      return rect.x, rect.y, rect.width, rect.height
    end
  end
  local w, h = System.Screen()
  return 0, 0, w, h
end

local function place(where)
  local x, y, w, h = area()
  if where == "Left half" then
    Window.Move(nil, x, y, w // 2, h)
  elseif where == "Right half" then
    Window.Move(nil, x + w // 2, y, w - w // 2, h)
  else
    Window.Move(nil, x, y, w, h)
  end
end

Bind(
  cfg.key,
  Async(function()
    for _, slot in { "first", "second" } do
      local app = cfg[slot .. "App"]
      if app ~= "" and open(app, cfg[slot .. "Launch"]) then
        Sleep(300)
        place(cfg[slot .. "Place"])
      end
    end
    if
      cfg.musicApp ~= ""
      and open(cfg.musicApp, cfg.musicLaunch)
      and cfg.playMusic
    then
      Sleep(800)
      -- macOS software mode cannot send media keys, so ask the app itself
      if IS_MAC then
        HID.Combo("Space")
      else
        pcall(HID.Combo, "MediaPlay")
      end
    end
  end)
)

3 files, 5.6 KB.