Skip to content

Commit

Permalink
RumpMyNap.py now kind of works! will auto nap 1 app from menu
Browse files Browse the repository at this point in the history
  • Loading branch information
omikun committed Mar 5, 2018
1 parent 9f2b351 commit 2368389
Showing 1 changed file with 20 additions and 7 deletions.
27 changes: 20 additions & 7 deletions RumpMyNap.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
launchedApps = NSWorkspace.sharedWorkspace().launchedApplications()
appNames = [ app['NSApplicationName'] for app in launchedApps ]
print(appNames)
desiredApps = set()
desiredApp = None
teststring = "hello world"

def name_of(app):
Expand All @@ -35,16 +35,28 @@ def name_of(app):
app_name = app_name.encode("utf8", "ignore")
return app_name

menuStates = {}

def clearOtherStates(appName):
for k, v in menuStates.items():
if k == appName:
continue
v.state = False
rumpsClass = \
'''class AwesomeStatusBarApp(rumps.App):
'''
menuItemString =\
''' @rumps.clicked(%s)
def onoff%d(self, sender):
global desiredApp
sender.state = not sender.state
print(%s)
appName = %s
print(\'clicked on \',appName)
if sender.state:
desiredApps.add(%s)
desiredApp = appName
menuStates[appName] = sender
clearOtherStates(appName)
print(desiredApp)
'''
quit_menu =\
''' @rumps.clicked('my quit')
Expand All @@ -57,9 +69,9 @@ def myquit(self, sender):
for i, launchedApp in enumerate(launchedApps):
if name_of(launchedApp) in DONT_SUSPEND_NAME:
continue
appStr = 'launchedApp[%d]' % i
#appStr = 'launchedApp[%d]' % i
appNameStr = '\'%s\'' % name_of(launchedApp)
rumpsClass += menuItemString % (appNameStr, i, appNameStr, appStr)
rumpsClass += menuItemString % (appNameStr, i, appNameStr)

rumpsClass += quit_menu
#print(rumpsClass)
Expand Down Expand Up @@ -88,14 +100,15 @@ def otherThread():
app = NSWorkspace.sharedWorkspace().activeApplication()
if prev_app != app:
print(app['NSApplicationName'])
if app['NSApplicationName'] == 'Messages':
print('checking if %s is %s' % (app['NSApplicationName'], desiredApp))
if app['NSApplicationName'] == desiredApp:
print("Continuing ", app['NSApplicationName'])
pids = get_pids(app)
for pid in pids:
os.kill(int(pid), signal.SIGCONT)
for pid in pids:
os.kill(int(pid), signal.SIGCONT)
if prev_app and prev_app['NSApplicationName'] == 'Messages':
if prev_app and prev_app['NSApplicationName'] == desiredApp:
print("Stopping ", prev_app['NSApplicationName'])
pids = get_pids(prev_app)
for pid in pids:
Expand Down

0 comments on commit 2368389

Please sign in to comment.