0

I hope this is not a repeat question—searching mostly turned up various versions of people having the opposite problem.

Is there a way to manually open a file as read-only?

The use case would be a file that I generally want write access to, but on a particular occasion want to be very confident I won't accidentally modify. For example, a shared document that I sometimes need to edit but currently only want to view. As a compulsive/instinctive saver, this would be a useful way to protect myself from myself.

I'm running Big Sur 11.2.1 on an early-2014 Macbook Air.

Obvious workarounds:

  • Get info, change my permissions to read-only, open; once finished, close, change my permissions back: This is a hassle.
  • Open, immediately Save As: Ideally I want to lock myself out before I even have a chance to break the file.
  • Duplicate the file, open the 'scratch' version; generally, delete the scratch version once I'm done: Still sort of a hassle, but the best Plan B I can think of.

Final thoughts:

  • I imagine this can be done from Terminal, and am happy to learn how, but any sort of GUI method would be best.
  • I imagine there's a way to create a service for this? Not sure where to start.

Thanks!!

3
  • Someone here will most likely know of some terminal or AppleScript command, however, I still think you need to execute that by pressing some sort of button or program. Hitting cmd+D to duplicate and then opening a file seems like basically the same amount of steps and hassle. – X_841 Feb 22 at 17:02
  • Something like f=/path/to/your/file; chmod -w "$f"; open -Wn "$f"; chmod +w "$f" maybe, but it would require some more logic to cover all cases (e.g. files which are read-only already, files which open doesn't know how to open etc). – nohillside Feb 22 at 17:13
  • 1
    The immutable bit can also be set and unset using chflags uchg filename and chflags nouchg filename. The immutable bit essentially sets the file to be not subject to change, thus it can't be edited or deleted. An attempt to edit such a file will result in a pop-up which asks if the user wants to Duplicate and open, Cancel or Unlock. The uchg flag is the same mechanism used in locking and unlocking files through the Get Info command. – IconDaemon Feb 22 at 17:52
1

You can do this in Finder with a simple keystroke and one click.

Before opening your file do this:

  • Select the file in Finder
  • Press Command-I (this is the same as Get Info in the File menu)
  • Tick the box next to Lock in the General section.

This sets the immutable bit - meaning you can't change the file in any way.

Leave the Get Info window open and, when you are finished, untick the Lock box to unlock the file.

You can create Services (or Quick Actions) to lock and unlock files, and associate a keystroke with them, but I feel is hardly with the effort. If you want to create them, run Automator and create a new Quick Action. This is what you need: Lock Action

Save it with the name Lock.

Modify by changing the uchg to nouchg and save as Lock Off.

Now these appear as Services and in Finder's Quick Action submenu.

You can also associate them with keystrokes in System Preferences - Keyboard - Shortcuts - Services. I chose the keystroke combinations Control Command L and Shift Control Command L.

0

Paste this following AppleScript code into a new Script Editor.app document then save it as an application. I named my version as “Open File As Locked.app”

This AppleScript code works for me using the latest version of macOS Big Sur.

tell application "Finder"
    set selectedFinderItems to selection
    if (count of items of selectedFinderItems) is not 1 ¬
        or class of item 1 of selectedFinderItems is not document file then
        activate
        display alert "Make Sure Only 1 FILE Is Selected" giving up after 4 ¬
            buttons {"OK"} default button "OK"
        return
    end if
    if locked of item 1 of selectedFinderItems is false then
        set locked of item 1 of selectedFinderItems to true
        open item 1 of selectedFinderItems
    else
        set locked of item 1 of selectedFinderItems to false
        activate
        display alert "Your File Is Now Unlocked" giving up after 3 ¬
            buttons {"OK"} default button "OK"
    end if
end tell

After your new applet is saved, you will need to grant appropriate privileges in System Preferences for both “Open File As Locked.app” and Finder.app as shown in this following image…

enter image description here

I added a custom icon of a lock, to my version of “Open File As Locked.app". In Finder, you can add your app to the tool bar for easy access holding the ‘command’ key while dragging the app to the tool bar.

enter image description here

Now anytime you have a single file selected in Finder, click on your new app in Finder’s toolbar. That file will be set as locked and then will be opened in the default application for files of that type.

enter image description here

That file will remain locked. To unlock the file, simply make sure it is selected in Finder and once again click on “Open File As Locked.app" in Finder’s toolbar (as demonstrated in this following .gif).

enter image description here

You must log in to answer this question.

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