This repository has been archived by the owner on Aug 23, 2019. It is now read-only.
/
Application.py
192 lines (148 loc) · 6.48 KB
/
Application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import os
import objc
from Foundation import NSObject, NSLog, NSAppleScript
from Foundation import NSUserDefaults, NSBundle
from Cocoa import NSInformationalAlertStyle, NSAlert, NSOpenPanel, NSOKButton
from ScriptingBridge import SBApplication
ARGV0 = os.environ['ARGVZERO']
YAYBUC = os.path.join(os.path.dirname(ARGV0), "YaybuShell")
CUSTOM_WINDOW_TITLE = u"\u2063" + "Yaybu"
def find_existing_yaybu_terminals():
term = SBApplication.applicationWithBundleIdentifier_("com.apple.Terminal")
for window in term.windows():
for tab in window.tabs():
if CUSTOM_WINDOW_TITLE == tab.customTitle():
yield window, tab
def find_best_yaybu_terminal():
for window, tab in find_existing_yaybu_terminals():
if window.frontmost() and tab.selected():
return window, tab
for window, tab in find_existing_yaybu_terminals():
if window.frontmost():
return window, tab
terminals = find_existing_yaybu_terminals()
try:
return terminals.next()
except StopIteration:
return None, None
def install_command_line_tools():
target = '/usr/local/bin/yaybu'
if os.path.exists(target):
if os.path.islink(target) and os.readlink(target) == YAYBUC:
NSLog("Command line tools already installed")
return
alert = NSAlert.alloc().init()
alert.setMessageText_("Enable command line support?")
alert.setInformativeText_("Yaybu can install a symlink at '/usr/local/bin/yaybu' allowing you to run yaybu from a terminal via the 'yaybu' command.")
alert.setAlertStyle_(NSInformationalAlertStyle)
alert.addButtonWithTitle_("Yes")
alert.addButtonWithTitle_("No")
if alert.runModal() == "No":
return
source = 'do shell script "test ! -d /usr/local/bin && mkdir -p /usr/local/bin; rm -f %s; ln -s %s %s" with administrator privileges' % (target, YAYBUC, target)
script = NSAppleScript.alloc().initWithSource_(source)
script.executeAndReturnError_(None)
class ApplicationDelegate(NSObject):
def init(self):
self = super(ApplicationDelegate, self).init()
return self
def application_openFile_(self, ns_app, path):
terminal = SBApplication.applicationWithBundleIdentifier_("com.apple.Terminal")
tab = terminal.doScript_in_("clear; %s; exit 0;" % (YAYBUC,), None)
tab.setCustomTitle_(CUSTOM_WINDOW_TITLE )
tab.setTitleDisplaysCustomTitle_(True)
tab.setTitleDisplaysDeviceName_(False)
tab.setTitleDisplaysFileName_(False)
tab.setTitleDisplaysShellPath_(False)
tab.setTitleDisplaysWindowSize_(False)
def applicationDidFinishLaunching_(self, _):
currentVersion = NSBundle.mainBundle().infoDictionary()["CFBundleVersion"]
NSLog("Starting Yaybu version %s" % currentVersion)
userDefaults = NSUserDefaults.standardUserDefaults()
lastVersion = userDefaults.stringForKey_("version")
if not lastVersion:
NSLog("Detected that this is a first run!")
self.applicationFirstRun()
if not lastVersion or lastVersion != currentVersion:
NSLog("Version changed from %s to %s" % (lastVersion, currentVersion))
self.applicationVersionChanged(lastVersion, currentVersion)
userDefaults.setObject_forKey_(currentVersion, "version")
def applicationFirstRun(self):
install_command_line_tools()
def applicationVersionChanged(self, lastVersion, currentVersion):
pass
def applicationWillTerminate_(self, sender):
pass
def applicationOpenUntitledFile_(self, _):
window, tab = find_best_yaybu_terminal()
if window and tab:
tab.setSelected_(True)
window.setFrontmost_(True)
term = SBApplication.applicationWithBundleIdentifier_("com.apple.Terminal")
term.activate()
return
self.openYaybufile_(None)
def openYaybufile_(self, notification):
panel = NSOpenPanel.openPanel()
panel.setAllowsMultipleSelection_(False)
panel.setCanCreateDirectories_(False)
panel.setCanChooseDirectories_(False)
panel.setCanChooseFiles_(True)
yaybufile = None
while not yaybufile or os.path.basename(yaybufile) != "Yaybufile":
if panel.runModal() != NSOKButton:
return
yaybufile = panel.filename()
self.application_openFile_(None, yaybufile)
def installCommandLineTools_(self, notification):
install_command_line_tools()
def quit_(self, notification):
NSLog('User request application close...')
NSApplication.sharedApplication().terminate_(None)
def setup_menus(app, delegate, updater):
mainmenu = NSMenu.alloc().init()
app.setMainMenu_(mainmenu)
appMenuItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('Yaybu', '', '')
mainmenu.addItem_(appMenuItem)
appMenu = NSMenu.alloc().init()
appMenuItem.setSubmenu_(appMenu)
# aboutItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_('About Yaybu...', 'about', '')
# aboutItem.setTarget_(delegate)
# appMenu.addItem_(aboutItem)
# appMenu.addItemWithTitle_action_keyEquivalent_('Preferences...', 'prefs', '')
openFile = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'Open...',
'openYaybufile:',
'',
)
openFile.setTarget_(delegate)
appMenu.addItem_(openFile)
iclt = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'Install command line tools...',
'installCommandLineTools:',
'',
)
iclt.setTarget_(delegate)
appMenu.addItem_(iclt)
cfu = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'Check for updates...',
'checkForUpdates:',
'')
cfu.setTarget_(updater)
appMenu.addItem_(cfu)
quitItem = NSMenuItem.alloc().initWithTitle_action_keyEquivalent_(
'Quit', 'quit:', 'q',
)
quitItem.setTarget_(delegate)
appMenu.addItem_(quitItem)
if __name__ == '__main__':
base_path = os.path.join(os.path.dirname(os.environ['RESOURCEPATH']), 'Frameworks')
bundle_path = os.path.abspath(os.path.join(base_path, 'Sparkle.framework'))
objc.loadBundle('Sparkle', globals(), bundle_path=bundle_path)
s = SUUpdater.sharedUpdater()
app = NSApplication.sharedApplication()
delegate = ApplicationDelegate.alloc().init()
app.setDelegate_(delegate)
setup_menus(app, delegate, s)
from PyObjCTools import AppHelper
AppHelper.runEventLoop()