-
-
Save frankrolf/c132d5c344d6c816ca54ab258f7fe8e6 to your computer and use it in GitHub Desktop.
Refireable Tiny Script Window
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# menuTitle : Tiny Script Window | |
import tempfile | |
import time | |
from pathlib import Path | |
from AppKit import NSApp | |
from vanilla import FloatingWindow, SplitView | |
from lib.scripting.scriptingWindow import PyTextEditor, OutPutEditor | |
from mojo.events import addObserver, removeObserver | |
from mojo.extensions import ( | |
setExtensionDefault, getExtensionDefault, | |
registerExtensionDefaults) | |
def current_dir_or_tmp(): | |
if __file__: | |
return Path(__file__).parent | |
else: | |
return Path(tempfile.mkdtemp()) | |
def make_new_content_file(): | |
timestamp = time.strftime('%Y-%m-%d_%H%M%S') | |
new_content_file = f'tinyContent {timestamp}.py' | |
return current_dir_or_tmp().joinpath(new_content_file) | |
def find_latest_content_file(): | |
p = current_dir_or_tmp() | |
candidates = [] | |
for script in p.glob('tinyContent*.py'): | |
candidates.append(script) | |
if len(candidates): | |
candidates = sorted(candidates, key=lambda f: -Path(f).stat().st_mtime) | |
return candidates[0] | |
return [] | |
def write_content_to_new_file(content): | |
cf_new = make_new_content_file() | |
with open(cf_new, 'w', encoding='utf-8') as cfn: | |
cfn.write(content) | |
class tinyScriptWin(object): | |
def __init__(self): | |
wtitle = 'Tiny Script Window' | |
self.prefKey = 'de.frgr.TinyScriptWindow' | |
for window in NSApp().windows(): | |
if window.title() == wtitle: | |
window.close() | |
return | |
content_file = find_latest_content_file() | |
if content_file: | |
with open(content_file, 'r', encoding='utf-8') as cf: | |
content = cf.read() | |
else: | |
content = '' | |
self.w = FloatingWindow((640, 480), minSize=(200, 200), title=wtitle) | |
self.w.editor = PyTextEditor((0, 0, -0, -0), content) | |
self.w.output = OutPutEditor((0, 0, -0, -0), readOnly=True) | |
self.w.editor.setOutputView_(self.w.output) | |
pane_descriptors = [ | |
dict(view=self.w.editor, identifier="pane1"), | |
dict(view=self.w.output, identifier="pane2"), | |
] | |
self.w.splitView = SplitView( | |
(0, 0, -0, -0), | |
pane_descriptors, | |
dividerStyle='thin', | |
isVertical=False, | |
) | |
self.w.bind('close', self.windowClose) | |
self.windowOpen() | |
def windowOpen(self): | |
initialDefaults = { | |
self.prefKey: self.w.getPosSize(), | |
} | |
registerExtensionDefaults(initialDefaults) | |
v = getExtensionDefault(self.pref) | |
w = self.w.getNSWindow() | |
w.setFrame_display_animate_(v, True, False) | |
addObserver(self, 'trigger', 'tinyScriptWindow') | |
self.w.open() | |
def save_content(self, sender): | |
modified_content = sender.editor.get() | |
content_file = find_latest_content_file() | |
if modified_content != '': | |
if content_file: | |
with open(content_file, 'r', encoding='utf-8') as cf: | |
if cf.read() != modified_content: | |
write_content_to_new_file(modified_content) | |
else: | |
pass | |
else: | |
write_content_to_new_file(modified_content) | |
def windowClose(self, sender): | |
self.save_content(sender) | |
window = sender.getNSWindow() | |
posSize = window.frame() | |
(wminx, wminy), (areaW, areaH) = window.frame() | |
posSize = (wminx, wminy), (areaW, areaH) | |
setExtensionDefault(self.prefKey + '.posSize', posSize) | |
removeObserver(self, 'tinyScriptWindow') | |
def trigger(self, notification): | |
if notification['trigger'] == 'fire': | |
self.save_content(self.w) | |
self.w.editor.run() | |
@property | |
def pref(self): | |
return self.prefKey + '.' + 'posSize' | |
tinyScriptWin() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# menuTitle : Tiny Script Fire | |
# shortCut : control+r | |
from mojo.events import postEvent | |
postEvent('tinyScriptWindow', trigger='fire') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment