Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 3 down vote accepted

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.