Home Download Buy Blog Forum Support

How to force save a readonly file

How to force save a readonly file

Postby kayakman on Sun Jul 31, 2011 11:41 pm

Hi there,

is there any way to force saving a readonly tagged file? I use ST2 on Mac and sometimes I edit files extracted from a Windows box and they are marked as readonly. Any other editor I have used before give me the option to force save but ST only say's "Unable to save file xxxxx".

Thanks in advance,

Ricardo
kayakman
 
Posts: 2
Joined: Sun Jul 31, 2011 11:36 pm

Re: How to force save a readonly file

Postby kayakman on Tue Aug 23, 2011 1:47 pm

Well, just in case any of you has the same problem ... I've done a plug-in that does the trick:

Code: Select all
import sublime, sublime_plugin
import os, stat

class ForceSaveCommand(sublime_plugin.WindowCommand):
   def run( self ):

      myFile = self.window.active_view().file_name()
      fileAtt = os.stat(myFile)[0]
      myPlatform = os.name

      if (myPlatform == 'nt'):
         if (not fileAtt & stat.S_IWRITE):
            print "Making "+myFile+" writable"
            os.chmod(myFile, stat.S_SIWRITE)
      else:
         if (fileAtt & stat.UF_IMMUTABLE):
            print "Making "+myFile+" mutable"
            os.chflags(myFile, not stat.UF_IMMUTABLE)
      
      self.window.active_view().run_command('save')


Then create a User Key Binding that supersedes the global save shortcut:

MacOS
Code: Select all
{ "keys": ["super+s"], "command":"force_save"}


Windows
Code: Select all
{ "keys": ["ctrl+s"], "command":"force_save"}


It works for both Windows XP and MacOS.
kayakman
 
Posts: 2
Joined: Sun Jul 31, 2011 11:36 pm


Return to Technical Support

Who is online

Users browsing this forum: Clams, Perkin5, Yahoo [Bot] and 27 guests