Skip to content

Instantly share code, notes, and snippets.

@hsab
Created September 5, 2018 03:21
Show Gist options
  • Save hsab/2d81d9bc92710cf205c9d3e46269bcf0 to your computer and use it in GitHub Desktop.
Save hsab/2d81d9bc92710cf205c9d3e46269bcf0 to your computer and use it in GitHub Desktop.

Revisions

  1. hsab created this gist Sep 5, 2018.
    92 changes: 92 additions & 0 deletions open_text_in_external.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    # ##### BEGIN GPL LICENSE BLOCK #####
    #
    # This program is free software; you can redistribute it and/or
    # modify it under the terms of the GNU General Public License
    # as published by the Free Software Foundation; either version 2
    # of the License, or (at your option) any later version.
    #
    # This program is distributed in the hope that it will be useful,
    # but WITHOUT ANY WARRANTY; without even the implied warranty of
    # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    # GNU General Public License for more details.
    #
    # You should have received a copy of the GNU General Public License
    # along with this program; if not, write to the Free Software Foundation,
    # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
    #
    # ##### END GPL LICENSE BLOCK #####

    ############# Add-on description (use by Blender)

    bl_info = {
    "name":
    "Open Text in External Editor",
    "description":
    'Add a button to Text Editor header to open the file with the default application.',
    "author":
    "Hirad Sab",
    "version": (0, 1, 0),
    "blender": (2, 70, 0),
    "location":
    "TextEditor > Header > External",
    "warning":
    "", # used for warning icon and text in addons panel
    "wiki_url":
    "",
    "tracker_url":
    "",
    "category":
    "Text Editor"
    }

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

    import bpy
    import os,sys
    import platform
    import subprocess


    def openFile(filepath):
    print('File opened in external editor:', filepath)
    if sys.platform.startswith('darwin'):
    subprocess.call(('open', filepath))
    elif os.name == 'nt': # For Windows
    os.startfile(filepath)
    elif os.name == 'posix': # For Linux, Mac, etc.
    subprocess.call(('xdg-open', filepath))

    class OpenTextinExternal(bpy.types.Operator):
    bl_idname = "text.external_editor"
    bl_label = "Open Text in External Editor"

    @classmethod
    def poll(cls, context):
    return True

    def execute(self, context):
    st = context.space_data
    text = st.text
    if text.filepath:
    f = text.filepath
    openFile(f)

    return {'FINISHED'}

    def texteditor_view_external_button(self, context):
    layout = self.layout
    layout.operator("text.external_editor", text = "", icon = 'TEXT')


    def register():
    bpy.types.TEXT_HT_header.append(texteditor_view_external_button)
    bpy.utils.register_class(OpenTextinExternal)


    def unregister():
    bpy.types.TEXT_HT_header.remove(texteditor_view_external_button)
    bpy.utils.unregister_class(OpenTextinExternal)


    if __name__ == "__main__":
    register()