Home Download Buy Blog Forum Support

Auto save of project files when Sublime lose focus

Auto save of project files when Sublime lose focus

Postby ilya on Mon Oct 26, 2009 8:09 am

Sublimator made a very useful plugin for those who frequently switches from the editor in the browser to check the changes.
By this principle operates Web IDE. Here's a post in their blog about it: Do not bother saving files anymore


Here's the code:
Code: Select all
#!/usr/bin/env python
#coding: utf8
#################################### IMPORTS ###################################

# Std Libs
import os

from ctypes import windll
from os.path import normpath

# Sublime Modules
import sublime
import sublimeplugin

# AAA* lib
from pluginhelpers import scheduled, do_in

################################################################################

def view_is_project_file(view, if_untitled=True):
    window = view.window() or sublime.activeWindow()
    project = window.project()
    if not project: return

    mounts = project.mountPoints()
   
    if not view.fileName():
        # what context was the untitled file created in?
        fn = os.getcwdu()
    else:
        fn = unicode(view.fileName())

    fn = normpath(fn)

    return any( fn.startswith(normpath(d['path'])) for d in mounts )

def _window_changed_focus(window):
    av = window.activeView()
   
    project_files = filter(view_is_project_file, window.views())
   
    for v in project_files:
        if v.isDirty():
            window.focusView(v)
            window.runCommand('save')
   
    window.focusView(av)

if not hasattr(sublime, '_window_changed_focus'):
    # Only loads once, 5 seconds after startup
    @do_in(5000)
    @scheduled(100)
    def do():
        changed = False
   
        while 1:
            ah = windll.user32.GetForegroundWindow()
   
            if ah != sublime.activeWindow().hwnd():
                if not changed:
                    _window_changed_focus(sublime.activeWindow())
                    changed = True
            else:
                changed = False
   
            yield
   
    sublime._window_changed_focus = True

################################################################################

http://pastie.org/669695.txt?key=iajofryslortwd2o8j7eyw

Put this in some py file and it will run whenever your window loses focus. That script will make sure the file is a project file and needing to be saved.

Many thanks to Sublimator! Image

PS: It's require AAALoadFirstExtensions
ilya
 
Posts: 122
Joined: Fri Sep 11, 2009 8:32 pm

Re: Auto save of project files when Sublime lose focus

Postby ilya on Mon Oct 26, 2009 8:33 am

Yes, need to think about it Image

But this script is still useful if you need to check the changes in many browsers, one after another, then again, do small changes in the editor, and again switch to all browsers. And so on.

Image
ilya
 
Posts: 122
Joined: Fri Sep 11, 2009 8:32 pm

Re: Auto save of project files when Sublime lose focus

Postby EJ12N on Tue Oct 27, 2009 8:39 pm

Nice Good job :D
I recommend: XRefresh, http://xrefresh.binaryage.com/
EJ12N
 
Posts: 281
Joined: Tue Apr 21, 2009 5:23 pm


Return to Plugin Announcements

Who is online

Users browsing this forum: Exabot [Bot] and 7 guests