Sublime Forum

Auto save of project files when Sublime lose focus

#1

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:

#!/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

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

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! http://forums.disenteria.ru/style_emoticons/default/agree.gif

PS: It’s require AAALoadFirstExtensions

0 Likes

#2

Yes, need to think about it http://forums.disenteria.ru/style_emoticons/default/idea2.gif

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.

0 Likes

#3

Nice Good job :smiley:
I recommend: XRefresh, xrefresh.binaryage.com/

0 Likes