How to Quit Finder When a Certain Folder Is Detected Open

I want the computer to automatically and immediately quit the Finder when "/Users/**/Library" is detected open.

However, only when the folder adds or removes an item will Finder quit. Can you tell me how to set up the trigger?

Or, how to write a script to achieve this goal and keep this script on to monitor it, with or without Keyboard Maestro?

File > Get Info > Sharing & Permissions is not what I want, because as an admin I can still change it; plus, apps may need to write files into this folder.

Here's a mockup that you can tinker with to get what you want. I prioritized reducing the trigger frequency (trigger surface?), which is why it's in its own Macro Group.

For convenience I left a few redundancies and logs, which you can pare down in the final product.

Library Blocker.kmmacros (7.9 KB)

Why? The answer to that will determine the best route to take. For example, @avtraino's method above will work in some cases but is trivially easy to circumvent...


I don't understand your complex setting at all, so I can't tinker with it. But your solution gives me an idea which clumsily solves my problem. Out of curiosity, I wonder if there is a more elegant and effective way.

My goal:
I don't want some folders under the folder Library to be accessed in Finder.

My current method:

I copied these actions adding the folders' names one by one.

The imperfection of this method is that, for example, I can directly go to /Users/li/Library/Focus/Data. It fails to block Focus because the window title is Data, so it will not quit finder.

Can the computer detects the path (instead of the window title)? Then, if the path contains Library, it quits finder, so we cannot open any folders under Library.

PS: My mac username is li. If you make a mockup, can you use li in the path because I don't even know where to replace the username :joy:

I bought an expensive app Focus, which blocks some other apps and websites. However, I find that users can go to Library and delete files related to Focus, making it useless.

Yes - it’s the %FinderInsertionLocation% token. See the KM wiki here token:FinderInsertionLocation [Keyboard Maestro Wiki]

As far as I can reckon, this will always be easy to circumvent without some serious sandboxing software (I'm not aware of what's floating around the "parental control"-esque space).

It seems the app in question (Focus) was designed for self-control, implementing voluntary restrictions. It was not designed to work against the user as an adversary.

Even if we could completely lock down this Finder interaction, we'd still have to box the user out of Terminal, Script Editor, Keyboard Maestro itself, and anything else that could potentially modify the file system on behalf of the user.

Yes -- but all you have to do to avoid a "Finder window title" trigger is work in List view and use the disclosure triangles. Doesn't come much more trivial than that :wink:

As @avtraino says, that's more a productivity aid than a "management" utility. If you want to continue with it, try changing its support files' permissions so the user can read them (but not write) while the "admin" group has full access (that does depend on your users not having admin rights, but so does almost anything else -- and if you can't trust them to not open certain apps/sites, you probably shouldn't trust them to be admins!).

The built-in Parental Controls will make a good job of what you are trying to do, as will the various device management solutions out there. You could block sites with the built-in firewall (take a look at Murus for a friendly interface to that) or take a look at your router's controls if you want to do that for all users of your network.

But, again, we're back to why you want to do this. There's a difference between how you'd do it for your own (or your kids') devices and how you might in a classroom or business setting.

1 Like

Thanks everyone, drawing on your ideas I have solved my problem.

step1, launch terminal and type: defaults write _FXShowPosixPathInTitle -bool true; killall Finder So the window title contains the whole path

PS: Apps like System settings, app store, activity monitor, terminal, KM, scriptors and so on have been blocked. I am not admin. I don't allow spotlight to search Library. I disabled cmd+shift+. so I cannot reveal hidden files in other apps. I disabled cmd+shift+g so I can't go to folders under library in other apps. I cannot install new apps.

I'm sure there are ways to circumvent it. But please do not tell me! Thanks very much! So I can have peace now.

So it's just an aid to "self-control"? In that case we can be a bit more lax, knowing that users won't be actively trying to work round any blocks.

But your macro is horribly inefficient, running once every second the Finder is active and spawning a new AppleScript instance every time. Killing the Finder is also rather a radical solution, which might cause problems...

It'll also stop you from going to folders called "My Library Loans" or similar :wink:

Here's a macro that should be less resource-expensive but also checks more frequently, is specific to the user's Library folder, also includes sub-folders, and simply returns them to the Desktop folder every time. Obviously it can be circumvented, but that requires a deliberate action by the user -- if you want to stop that you really need to step outside of KM.

Finder Restriction.kmmacros (2.3 KB)

1 Like