<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Abhishek Sinha</title>
    <description>The latest articles on DEV Community by Abhishek Sinha (@abhisheksinhaleakyally).</description>
    <link>https://dev.to/abhisheksinhaleakyally</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3535417%2Fe0c31f51-0a22-4f24-9a14-07d1dadc7ac4.png</url>
      <title>DEV Community: Abhishek Sinha</title>
      <link>https://dev.to/abhisheksinhaleakyally</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/abhisheksinhaleakyally"/>
    <language>en</language>
    <item>
      <title>How to politely quit every open app on macOS, Linux and Windows (and the bug my tests missed) published: false</title>
      <dc:creator>Abhishek Sinha</dc:creator>
      <pubDate>Sat, 14 Nov 2026 13:06:20 +0000</pubDate>
      <link>https://dev.to/abhisheksinhaleakyally/how-to-politely-quit-every-open-app-on-macos-linux-and-windows-and-the-bug-my-tests-missed-2760</link>
      <guid>https://dev.to/abhisheksinhaleakyally/how-to-politely-quit-every-open-app-on-macos-linux-and-windows-and-the-bug-my-tests-missed-2760</guid>
      <description>&lt;p&gt;At the end of most days I have 15+ apps open. I wanted one command that closes them all, except the terminal I'm typing in, &lt;strong&gt;without&lt;/strong&gt; losing unsaved work.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;killall&lt;/code&gt; and &lt;code&gt;taskkill /f&lt;/code&gt; are out: they kill processes, and unsaved documents die with them. What I wanted was the same thing as pressing Cmd+Q or clicking the X on every app: a polite request the app can answer with "Save changes?".&lt;/p&gt;

&lt;p&gt;So I built &lt;a href="https://github.com/abhisheksinha1506/lightsout" rel="noopener noreferrer"&gt;lightsout&lt;/a&gt;, a small Go CLI. This post covers how "quit politely" works on each OS, and the bug that taught me to stop trusting green tests.&lt;/p&gt;

&lt;h2&gt;
  
  
  The rules
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Only close apps a person would see as "open": Dock apps, taskbar apps, windows on screen. No background daemons.&lt;/li&gt;
&lt;li&gt;Never force-kill. Send the normal close request, wait up to 10 seconds, and &lt;strong&gt;report&lt;/strong&gt; anything still open instead of killing it.&lt;/li&gt;
&lt;li&gt;Keep the terminal you run it from, plus any apps listed in &lt;code&gt;~/.config/lightsout/config.yml&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  macOS: NSWorkspace through JXA
&lt;/h2&gt;

&lt;p&gt;macOS already knows which apps are "regular" ones: every &lt;code&gt;NSRunningApplication&lt;/code&gt; has an &lt;code&gt;activationPolicy&lt;/code&gt;, and &lt;code&gt;0&lt;/code&gt; (&lt;code&gt;.regular&lt;/code&gt;) means it shows in the Dock. JavaScript for Automation (JXA) can read this through &lt;code&gt;osascript&lt;/code&gt; without any Automation permission:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nc"&gt;ObjC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;import&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"AppKit"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;apps&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; $&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NSWorkspace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sharedWorkspace&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;runningApplications&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;out&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;apps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;apps&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;objectAtIndex&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ObjC&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unwrap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;localizedName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;processIdentifier&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;policy&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Number&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;activationPolicy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nb"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;out&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To quit, lightsout looks the app up &lt;strong&gt;by PID&lt;/strong&gt; (two apps can share a name) and calls &lt;code&gt;terminate&lt;/code&gt;, which is exactly what Cmd+Q sends:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; $&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;NSRunningApplication&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;runningApplicationWithProcessIdentifier&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;pid&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;terminate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finder is skipped by its bundle ID and handled separately: its windows get closed, but Finder itself stays running.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linux: wmctrl + xprop
&lt;/h2&gt;

&lt;p&gt;On X11 there's no Dock to ask, so lightsout works with windows instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;wmctrl -l                                          &lt;span class="c"&gt;# every managed window&lt;/span&gt;
xprop -id &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;id&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; WM_CLASS _NET_WM_WINDOW_TYPE        &lt;span class="c"&gt;# which app, and what kind of window&lt;/span&gt;
wmctrl -i -c &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;id&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;                                  &lt;span class="c"&gt;# close it gracefully&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Windows whose type is &lt;code&gt;_NET_WM_WINDOW_TYPE_DESKTOP&lt;/code&gt; or &lt;code&gt;_DOCK&lt;/code&gt; (your wallpaper and panels) are skipped. &lt;code&gt;wmctrl -c&lt;/code&gt; sends the same close request as the window's X button. Then lightsout waits for the windows to disappear.&lt;/p&gt;

&lt;p&gt;The catch: on &lt;strong&gt;Wayland&lt;/strong&gt;, apps can't see other apps' windows by design, so only XWayland apps are visible. lightsout warns you about this instead of failing silently.&lt;/p&gt;

&lt;h2&gt;
  
  
  Windows: EnumWindows with the taskbar's rules
&lt;/h2&gt;

&lt;p&gt;Windows has no "list taskbar apps" API, but the taskbar follows well-known rules, so lightsout applies the same ones to every top-level window from &lt;code&gt;EnumWindows&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;onTaskbar&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt; &lt;span class="n"&gt;window&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Visible&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Title&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s2"&gt;""&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Cloaked&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OwnConsole&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;shellClasses&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Owned&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;ToolWindow&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;w&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AppWindow&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Cloaked&lt;/strong&gt; (from &lt;code&gt;DwmGetWindowAttribute&lt;/code&gt;) catches windows on other virtual desktops and suspended Store apps.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;shellClasses&lt;/strong&gt; skips the desktop and taskbar (&lt;code&gt;Progman&lt;/code&gt;, &lt;code&gt;WorkerW&lt;/code&gt;, &lt;code&gt;Shell_TrayWnd&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;OwnConsole&lt;/strong&gt; keeps the console lightsout itself is running in.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Names come from the exe's version info (&lt;code&gt;FileDescription&lt;/code&gt;, e.g. "Google Chrome"). Store apps all run inside &lt;code&gt;ApplicationFrameHost.exe&lt;/code&gt;, so for those the window title is used instead.&lt;/p&gt;

&lt;p&gt;Closing is &lt;code&gt;PostMessageW(hwnd, WM_CLOSE, 0, 0)&lt;/code&gt;, the same as clicking X, followed by polling &lt;code&gt;IsWindow&lt;/code&gt; until the window is gone or 10 seconds pass. No &lt;code&gt;TerminateProcess&lt;/code&gt; anywhere.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug my tests didn't catch
&lt;/h2&gt;

&lt;p&gt;Every unit test passed. CI was green on all three OSes. Then I ran lightsout on my Mac and &lt;strong&gt;it quit nothing&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The JXA bridge returns &lt;code&gt;activationPolicy&lt;/code&gt; as a &lt;em&gt;string&lt;/em&gt;, so my &lt;code&gt;policy === 0&lt;/code&gt; filter never matched and every app was skipped. My tests fed the parser hand-written JSON with real numbers in it, so they were testing my assumption, not macOS.&lt;/p&gt;

&lt;p&gt;The fix was one &lt;code&gt;Number()&lt;/code&gt; call. The real lesson: &lt;strong&gt;for code that talks to the OS, a mock only tests what you think the OS does.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;So CI now runs real end-to-end tests:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Windows runner:&lt;/strong&gt; start Notepad, check lightsout lists it, close it with the real code, check the window is gone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Ubuntu runner:&lt;/strong&gt; start Xvfb + openbox, open an xterm, same checks with the real wmctrl/xprop code.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each takes under half a second, and both are gated behind an env var so they never open windows on your own machine when you run &lt;code&gt;go test&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;brew install abhisheksinha1506/tap/lightsout   &lt;span class="c"&gt;# macOS / Linux&lt;/span&gt;
lightsout
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;On Windows, download the zip from &lt;a href="https://github.com/abhisheksinha1506/lightsout/releases" rel="noopener noreferrer"&gt;Releases&lt;/a&gt; and put &lt;code&gt;lightsout.exe&lt;/code&gt; on your PATH.&lt;/p&gt;

&lt;p&gt;It's MIT-licensed and a single binary. If it fails to close something on your machine, please &lt;a href="https://github.com/abhisheksinha1506/lightsout/issues" rel="noopener noreferrer"&gt;open an issue&lt;/a&gt;. Those reports are exactly what I need.&lt;/p&gt;

</description>
      <category>go</category>
      <category>cli</category>
      <category>opensource</category>
      <category>productivity</category>
    </item>
  </channel>
</rss>
