3

I can change the file locking ("read only") status in a file's context menu:

enter image description here

There's no menu item for this.

How can I create a Finder (context) menu item to set, remove, or toggle this flag more quickly?

1 Answer 1

4

You can create a new menu item that does that by creating a Service that receives files and folders as input in any application in Automator.

You have two implementation options. Choose one of the two Automator actions to build the Service workflow with:

  • Run Shell Script
  • Run AppleScript

The code below implements the Toggle command, as it's the most complex.

Run Shell Script

This variant uses stat to read the flags set for the file. These are the same values as usually displayed by running ls -lO, but stat is a cleaner solution to read the values. The locked flag, or uchg, has the value 0x2, so that's what we're checking for.

chflags is used to change the value, and growlnotify, optional part of Growl, used to display success or error messages.

enter image description here

Use the following bash script code snippet as part of the Run Shell Script action:

for f in "$@"
do
    let "$( stat -f "%f" "$f" ) & 0x2"
    if [ $? -ne 0 ] ; then
        chflags uchg "$f" || /usr/local/bin/growlnotify "Error" -m "Failed to lock $f!"
        /usr/local/bin/growlnotify "Locked File" -m "$f was locked!"
    else
        chflags nouchg "$f" || /usr/local/bin/growlnotify "Error" -m "Failed to unlock $f!"
        /usr/local/bin/growlnotify "Unlocked File" -m "$f was unlocked!"
    fi
done

Configure the action to receive input as arguments.

 Run AppleScript

Use the following AppleScript code snippet as part of the Run AppleScript action:

on run {input, parameters}
repeat with f in input
        try
            tell application "Finder" to set locked of f to (not locked of f)
        on error errmsg
            tell application "Finder" to display alert errmsg
        end try
    end repeat
end run

If the action fails, e.g. because permissions are missing, one dialog is displayed per file that failed to be changed.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .