Monday, November 25, 2024

macos – Apple Script to shut all home windows in Finder — however maintain energetic window AND ITS TABS open?

In fundamental vanilla AppleScript there is no such thing as a property, aspect or class for a tab in a window in Finder, and as such it makes it tough to inform if a window presents with a number of tabs on the UI stage, (a number of home windows offered as tabs inside a single UI stage window).

The next instance AppleScript code is a bit kludgy as it may/will reduce and un-minimize the seen entrance UI stage window, nevertheless, is does shut all different home windows not inside the window that’s frontmost and visual on the UI stage on the time the code is run.

Instance AppleScript code:

inform utility id "com.apple.finder"
    if (rely home windows) is lower than or equal to 1 then return
    set collapsedWindows to home windows whose collapsed is true
    shut collapsedWindows
    if (rely home windows) is bigger than 1 then
        set collapsed of window 1 to true
        set uncollapsedWindows to home windows whose collapsed is fake
        shut uncollapsedWindows
        set collapsed of window 1 to false
    finish if
finish inform

Notes:

What the instance AppleScript code does:

  • Checks the variety of home windows for error dealing with functions.
  • Closes all home windows which are at the moment minimized (collapsed:true).
  • Then if multiple window, minimizes the entrance window.
  • Closes all home windows that aren’t at the moment minimized (collapsed:false).
  • Un-minimizes the one remaining window (home windows the place collapsed:true).

Notice that if there is just one window in Finder with a number of tabs and is minimized when the code is run it should shut that window, nevertheless, I take this situation to be outdoors the scope of the intent of the query, because the key phrase use within the OP was “energetic” and a minimized window shouldn’t be thought of the “energetic” window in regular context. That is additionally an unlucky consequence of there not being a tab as a property, aspect or class for a tab in a window in Finder.

Notice that phrases used within the paragraphs above proven as, e.g. window are referring to them programmatically and proven as e.g. window are referring to them on the UI stage.

The instance AppleScript code, proven above, was examined in Script Editor below each model of macOS from macOS Sierra to macOS Monterey with Language & Area settings in System Preferences set to English (US) — Main and labored for me with out situation1.

  • 1 Assumes essential and acceptable settings in System Preferences > Safety & Privateness > Privateness have been set/addressed as wanted.


Notice: The instance AppleScript code is simply that and sans any included error dealing with doesn’t include any extra error dealing with as could also be acceptable. The onus is upon the consumer so as to add any error dealing with as could also be acceptable, wanted or needed. Take a look on the strive assertion and error assertion within the AppleScript Language Information. See additionally, Working with Errors. Moreover, the usage of the delay command could also be essential between occasions the place acceptable, e.g. delay 0.5, with the worth of the delay set appropriately.

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles