Can I create a keyboard shortcut to lock a file in Finder?

Keyboard Maestro can do this with a macro such as this, which will run chflags uchg on every file in the current Finder selection:

Lock


You can actually accomplish this without any extra software by using Automator and System Preferences (at least as of macOS Catalina).

Automator

Open Automator and choose "Quick Action" as your document type (button looks like a gear). On the top you should see the following configuration settings; fill in the drop-downs as displayed below:

  • Workflow current files or folders in Finder
  • Input is entire selection

There are also options for Image and Color which I don't think are relevant.

Then you need to add in the Run Shell Script Action which you can locate via the search bar; simply drag into the workflow.

Keep Shell as /bin/bash, but change Pass Input to as arguments. This enables Automator to capture the selection and pass into the shell script code as a variable.

It will populate some sample code for you, which you can delete. For the Locking script, use this code:

chflags uchg "$@"

For unlocking:

chflags nouchg "$@"

Save the workflows with names like "Lock File" and "Unlock File" and then close.

You can right click files in Finder at this point to check that the quick actions are now available.

System Preferences

Navigate to System Preferences, then Keyboard. Hit the Shortcuts tab, then click App Shortcuts in the left side. Hit the "+" button to add a new shortcut. Fill out the next options:

  • Application: Finder
  • Menu Title: This should be the exact name of the service you created, so for instance, `Lock File'

Then click the text box next to Keyboard Shortcut, and type in whatever shortcut you want. For instance, I did Ctrl-Opt-Shift-L for Lock and Ctrl-Opt-Shift-U for Unlock.

That should do the trick!


You can also create a service like this in Automator:

Then give it a shortcut from System Preferences.

Alternatively, save this script in ~/Library/Scripts/Applications/Finder/ in AppleScript Editor:

tell application "Finder" to repeat with f in (get selection)
    set locked of f to true
end repeat

Then use FastScripts to give the script a shortcut.

You can use a script like this to toggle locking files:

for f;do [[ $(GetFileInfo -al "$f") = 0 ]]&&chflags uchg "$f"||chflags nouchg "$f";done

GetFileInfo is in /Applications/Xcode.app/Contents/Developer/usr/bin/ if you have installed Xcode and in /usr/bin/ if you have installed the command line tools package.

Or using AppleScript:

tell application "Finder" to repeat with f in (get selection)
    set locked of f to not locked of f
end repeat