-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoTasks.pyw
More file actions
491 lines (375 loc) · 17.3 KB
/
AutoTasks.pyw
File metadata and controls
491 lines (375 loc) · 17.3 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
import os
try:
from wx.lib import masked
import wx # wxPython
import wx.adv
from datatable import f
import datatable as dtb
from threading import Thread
import win32com.client
import datetime as dt
import keyboard as kb
import warnings
import time
import sys
except Exception as ex:
print('ERRO: ', ex)
os.system('pause')
global win
global path
global dt_arq
global list_dias
global thread
global exec_program
global txt_executar
global cbo_tarefas
global chk_tarefa
global cbo_dias
global msk_horario
global chk_min_telas
def exibir_sobre(event):
wx.MessageBox('Desenvolvido por Marcelo F. Delgado Horita', 'AutoTasks 3.1.0', wx.OK | wx.ICON_INFORMATION)
return event
class ClockWindow(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
self.timer = wx.Timer(self)
self.Bind(wx.EVT_TIMER, self.OnTimer)
@staticmethod
def OnTimer(event):
def ExecThread():
# print(threading.current_thread(), exec_program)
os.system(exec_program)
lst_horarios = [dt_arq[:, 'Horario'][i, 0] for i in range(dt_arq.nrows)]
hora_atual = str(time.strftime('%H:%M', time.localtime()))
if hora_atual in lst_horarios:
dt_exec = dt_arq[f.Horario == hora_atual, :]
for i in range(dt_exec.nrows):
task = dt_exec[i, :]
if task[:, 'DesabilitarTarefa'][0, 0] == 0:
if list_dias[dt.datetime.now().weekday()] == task[:, 'DiasExecucao'][0, 0]:
pass
elif dt_exec[:, 'DiasExecucao'][0, 0] == 'Segunda à Sexta' \
and dt.datetime.now().weekday() in [0, 1, 2, 3, 4]:
pass
elif task[:, 'DiasExecucao'][0, 0] == 'Todos':
pass
else:
continue
if task[:, 'MinimizarTelas'][0, 0] == 1:
kb.send('windows+m')
exec_program = task[:, 'Programa'][0, 0]
thread['thread' + str(i)] = Thread(name='thread' + str(i), target=ExecThread)
thread['thread' + str(i)].start()
return event
class CustomTaskBarIcon(wx.adv.TaskBarIcon):
def __init__(self, frame):
"""Constructor"""
wx.adv.TaskBarIcon.__init__(self)
self.frame = frame
if os.path.isfile(path + '\\' + 'autotasks.ico'):
img = wx.Image(path + '\\' + 'autotasks.ico', wx.BITMAP_TYPE_ANY)
bmp = wx.Bitmap(img)
self.icon = wx.Icon()
self.icon.CopyFromBitmap(bmp)
else:
self.icon = wx.Icon()
self.SetIcon(self.icon, "AutoTasks")
self.Bind(wx.adv.EVT_TASKBAR_LEFT_DOWN, self.OnTaskBarLeftClick)
def OnTaskBarActivate(self, event):
pass
def OnTaskBarClose(self, event):
"""
Destroy the taskbar icon and frame from the taskbar icon itself
"""
self.frame.Close()
return event
def OnTaskBarLeftClick(self, event):
"""
Create the right-click menu
"""
self.frame.Show()
self.frame.Restore()
return event
class MainFrame(wx.Frame):
def __init__(self):
"""Constructor"""
wx.Frame.__init__(
self, None, title="AutoTasks (Agenda de Tarefas)", size=(730, 250),
style=wx.DEFAULT_FRAME_STYLE | wx.STAY_ON_TOP & ~(wx.RESIZE_BORDER | wx.MAXIMIZE_BOX))
self.tbIcon = CustomTaskBarIcon(self)
self.Bind(wx.EVT_ICONIZE, self.onMinimize)
self.Bind(wx.EVT_CLOSE, self.onClose)
# self.Show()
# ----------------------------------------------------------------------
def onClose(self, _):
"""
Destroy the taskbar icon and the frame
"""
self.tbIcon.RemoveIcon()
self.tbIcon.Destroy()
self.Destroy()
sys.exit()
# ----------------------------------------------------------------------
def onMinimize(self, event):
"""
When minimizing, hide the frame so it "minimizes to tray"
"""
if self.IsIconized():
self.Hide()
return event
def fechar_tela(event):
win.onClose(event)
def minimizar_tela(event):
win.Show(False)
return event
def incluir_tarefa(event):
s_tarefa = '00'
for item in range(1, 99):
s_tarefa = f'{item:02}'
if s_tarefa not in cbo_tarefas.Items:
cbo_tarefas.Append(s_tarefa)
cbo_tarefas.SetValue(s_tarefa)
break
novo_conteudo = {'Nro': [s_tarefa], 'Programa': ['calc.exe'], 'DiasExecucao': ['Todos'],
'Horario': ['09:00'], 'DesabilitarTarefa': [0], 'MinimizarTelas': [0]}
dt_arq_novo = dtb.Frame(novo_conteudo)
dt_arq.rbind(dt_arq_novo)
salvar_tarefa('inclusão')
atualizar_tela(s_tarefa)
txt_executar.SetFocus()
return event
def excluir_tarefa(event):
s_tarefa = cbo_tarefas.GetValue()
if wx.MessageDialog(None, 'Confirmar exclusão da tarefa ' + s_tarefa + '?',
'AutoTasks: Exclusão', wx.YES_NO | wx.ICON_QUESTION).ShowModal() == wx.ID_YES:
del dt_arq[f.Nro == s_tarefa, :]
salvar_tarefa('exclusão')
return event
def salvar_tarefa(event):
if event == 'inclusão':
s_tarefa = cbo_tarefas.GetValue()
elif event == 'exclusão':
s_tarefa = ''
else:
s_tarefa = cbo_tarefas.GetValue()
del dt_arq[f.Nro == s_tarefa, :]
novo_conteudo = {'Nro': [s_tarefa], 'Programa': [txt_executar.GetValue()],
'DiasExecucao': [cbo_dias.GetValue()], 'Horario': [msk_horario.GetValue()],
'DesabilitarTarefa': [int(chk_tarefa.GetValue())],
'MinimizarTelas': [int(chk_min_telas.GetValue())]}
dt_arq_novo = dtb.Frame(novo_conteudo)
dt_arq.rbind(dt_arq_novo)
dt_arq.sort("Nro").to_pandas().to_csv(path + '\\' + 'autotasks.cfg', sep=';', index=False)
atualizar_tela(s_tarefa)
wx.MessageDialog(None, 'Alteração concluída com sucesso!', 'AutoTasks',
wx.OK_DEFAULT | wx.ICON_EXCLAMATION).ShowModal()
return event
def selecionar_tarefa(event):
atualizar_tela(cbo_tarefas.GetValue())
return event
def carregar_arquivo():
file = path + '\\' + 'autotasks.cfg'
if os.path.isfile(file):
dt_arq = dtb.fread(file, columns={'Nro': str, 'Programa': str, 'DiasExecucao': str,
'Horario': str, 'DesabilitarTarefa': int, 'MinimizarTelas': int})
if dt_arq.nrows != 0:
return dt_arq
conteudo_minimo = {'Nro': ['01'], 'Programa': ['calc.exe'], 'DiasExecucao': ['Todos'],
'Horario': ['09:00'], 'DesabilitarTarefa': [0], 'MinimizarTelas': [0]}
dt_arq = dtb.Frame(conteudo_minimo)
return dt_arq
def atualizar_tela(s_tarefa=''):
dt_arq = carregar_arquivo()
cbo_tarefas.Clear()
list_nros = [dt_arq[:, 'Nro'][i, 0] for i in range(dt_arq.nrows)]
for item in list_nros:
cbo_tarefas.Append(item)
if s_tarefa == '':
s_tarefa = dt_arq[:, 'Nro'][0, 0]
dt_arq = dt_arq[f.Nro == s_tarefa, :]
cbo_tarefas.SetValue(s_tarefa) # Seta Tarefa
txt_executar.SetValue(dt_arq[0, 1]) # Programa
chk_tarefa.SetValue(bool(dt_arq[0, 4])) # Desabilitar Tarefa
cbo_dias.SetValue(dt_arq[0, 2]) # Dias Escolhidos
msk_horario.SetValue(dt_arq[0, 3]) # Horário
chk_min_telas.SetValue(dt_arq[0, 5]) # Minimizar Telas
def executar_thread():
if int(chk_min_telas.GetValue()) == 1:
kb.send('windows+m')
os.system(txt_executar.GetValue())
def executar_tarefa(event):
thread[cbo_tarefas.GetValue()] = Thread(target=executar_thread)
thread[cbo_tarefas.GetValue()].start()
return event
def CreateShortcut(s_path, s_path_orig, s_path_atalho):
shell = win32com.client.Dispatch("WScript.Shell")
shortcut = shell.CreateShortCut(s_path_atalho)
shortcut.Targetpath = s_path_orig
shortcut.WorkingDirectory = s_path
shortcut.save()
def exec_inicwindows(event):
s_path_dest = r'C:\AutoTasks\AutoTasks.exe'
if not os.path.isfile(s_path_dest):
exec_instalacao(event)
else:
s_path_atalho = r'C:\AutoTasks\AutoTasks.lnk'
CreateShortcut(r'C:\AutoTasks', s_path_dest, s_path_atalho)
os.system('copy /Y ' + s_path_atalho + ' ' + r'%AppData%\Microsoft\Windows\"Start Menu"\Programs\Startup')
wx.MessageBox(r'Configuração realizada com sucesso!', 'AutoTasks', wx.OK | wx.ICON_INFORMATION)
def exec_instalacao(event):
s_path_dest = r'C:\AutoTasks\AutoTasks.exe'
if os.path.isfile(s_path_dest):
wx.MessageBox(r'Instalação já existente em C:\AutoTasks\ !', 'AutoTasks', wx.OK | wx.ICON_INFORMATION)
else:
if wx.MessageBox('Confirma instalação do AutoTasks?', 'AutoTasks', wx.YES_NO | wx.ICON_QUESTION) == wx.YES:
os.system(r'mkdir c:\AutoTasks')
os.system('copy ' + path + r'\AutoTasks.exe C:\AutoTasks')
os.system('copy ' + path + r'\AutoTasks.ico C:\AutoTasks')
os.system('copy ' + path + r'\AutoTasks.png C:\AutoTasks')
s_path_atalho = r'C:\AutoTasks\AutoTasks.lnk'
CreateShortcut(r'C:\AutoTasks', s_path_dest, s_path_atalho)
os.system('copy /Y ' + s_path_atalho + ' ' + r'%AppData%\Microsoft\Windows\"Start Menu"\Programs\Startup')
os.system(r'explorer.exe C:\AutoTasks')
win.tbIcon.RemoveIcon()
win.tbIcon.Destroy()
win.Destroy()
sys.exit()
return event
def main():
global win
global path
global dt_arq
global list_dias
global thread
global exec_program
global txt_executar
global cbo_tarefas
global chk_tarefa
global cbo_dias
global msk_horario
global chk_min_telas
try:
if os.path.isfile(os.getcwd() + '\\Python.exe'):
path = os.path.dirname(os.path.realpath(__file__))
else:
path = os.getcwd()
warnings.filterwarnings('ignore')
thread = dict()
app = wx.App(False)
win = MainFrame()
# Carrega o ícone
# ------------------------------------------------------------------------------------------------------------
if os.path.isfile(path + '\\' + 'autotasks.ico'):
win.SetIcon(wx.Icon(path + "\\" + "AutoTasks.ico"))
panel = wx.Panel(win)
# Carrega configurações
# ------------------------------------------------------------------------------------------------------------
dt_arq = carregar_arquivo()
list_nros = [dt_arq[:, 'Nro'][i, 0] for i in range(dt_arq.nrows)]
# Define a tarefa que será executada
# ------------------------------------------------------------------------------------------------------------
wx.StaticText(panel, label="Programa:", pos=(10, 10), style=1)
# lblTarefa.SetFont(wx.Font(8, wx.DECORATIVE, wx.BOLD, wx.NORMAL))
txt_executar = wx.TextCtrl(panel, -1, '', size=(560, -1), pos=(10, 30))
txt_executar.SetInsertionPoint(0)
# Carrega a lista de tarefas existentes
# ------------------------------------------------------------------------------------------------------------
cbo_tarefas = wx.ComboBox(panel, -1, value=list_nros[0], pos=(580, 30),
size=wx.DefaultSize, choices=list_nros, style=wx.CB_READONLY)
cbo_tarefas.Bind(wx.EVT_COMBOBOX, selecionar_tarefa)
# Define o botão de inclusão de uma nova tarefa
# ------------------------------------------------------------------------------------------------------------
btn_inclusao = wx.Button(panel, label="+", size=(30, 27), pos=(630, 28))
btn_inclusao.Bind(wx.EVT_BUTTON, incluir_tarefa)
# Define o botão de exclusão de uma tarefa
# ------------------------------------------------------------------------------------------------------------
btn_exclusao = wx.Button(panel, label="-", size=(30, 27), pos=(670, 28))
btn_exclusao.Bind(wx.EVT_BUTTON, excluir_tarefa)
# Define se tarefa está ativa / inativa
# ------------------------------------------------------------------------------------------------------------
chk_tarefa = wx.CheckBox(panel, label='Tarefa desativada', pos=(10, 60))
chk_tarefa.SetValue(0)
# Define se tarefa está ativa / inativa
# ------------------------------------------------------------------------------------------------------------
chk_min_telas = wx.CheckBox(panel, label='Minimizar telas', pos=(130, 60))
chk_min_telas.SetValue(0)
# Carrefa lista de dias de execução
# ------------------------------------------------------------------------------------------------------------
wx.StaticText(panel, label="Dias:", pos=(220, 95))
list_dias = ["Segunda-feira", "Terça-feira", "Quarta-feira", "Quinta-feira", "Sexta-feira",
"Sábado", "Domingo", "Todos", "Segunda à Sexta"]
cbo_dias = wx.ComboBox(panel, -1, value="Todos", pos=(250, 90),
size=wx.DefaultSize, choices=list_dias, style=wx.CB_READONLY)
# Carrefa horário de execução
# ------------------------------------------------------------------------------------------------------------
wx.StaticText(panel, label="Horas:", pos=(420, 95))
msk_horario = masked.TextCtrl(panel, -1, "06:00", mask='##:##', size=(50, -1), pos=(460, 90))
# Define o botão de execução da tarefa
# ------------------------------------------------------------------------------------------------------------
btn_execucao = wx.Button(panel, label="&Executar", size=(140, 37), pos=(560, 83))
btn_execucao.Bind(wx.EVT_BUTTON, executar_tarefa)
# Define o botão para aplicar alteração
# ------------------------------------------------------------------------------------------------------------
btn_aplicar = wx.Button(panel, label="&Salvar", size=(140, 37), pos=(260, 130))
btn_aplicar.Bind(wx.EVT_BUTTON, salvar_tarefa)
# Define o botão para minimizar a tela
# ------------------------------------------------------------------------------------------------------------
btn_minimizar = wx.Button(panel, label="&Minimizar", size=(140, 37), pos=(410, 130))
btn_minimizar.Bind(wx.EVT_BUTTON, minimizar_tela)
# Define o botão para fechar a tela
# ------------------------------------------------------------------------------------------------------------
btn_maximizar = wx.Button(panel, label="&Fechar", size=(140, 37), pos=(560, 130))
btn_maximizar.Bind(wx.EVT_BUTTON, fechar_tela)
# Definir imagem
# ------------------------------------------------------------------------------------------------------------
if os.path.isfile(path + '\\' + 'autotasks.png'):
img_icone = wx.Bitmap(wx.Image(path + "\\" + "AutoTasks.png", wx.BITMAP_TYPE_PNG).Scale(90, 80))
wx.StaticBitmap(panel, -1, img_icone, pos=(70, 90))
# Define o botão para fechar a tela
# ------------------------------------------------------------------------------------------------------------
tmr_cronometro = ClockWindow()
tmr_cronometro.timer.Start(60000)
# Setar configurações
# ------------------------------------------------------------------------------------------------------------
atualizar_tela(dt_arq[:, 'Nro'][0, 0])
# Menu e Sub-menu
# ------------------------------------------------------------------------------------------------------------
menu_file = wx.Menu()
menu_file.Append(1, "&Executar")
win.Bind(wx.EVT_MENU, executar_tarefa, id=1)
menu_file.Append(2, "&Salvar")
win.Bind(wx.EVT_MENU, salvar_tarefa, id=2)
menu_file.Append(3, "&Minimizar")
win.Bind(wx.EVT_MENU, minimizar_tela, id=3)
menu_file.AppendSeparator()
menu_file.Append(4, "&Fechar")
win.Bind(wx.EVT_MENU, fechar_tela, id=4)
menu_editar = wx.Menu()
menu_editar.Append(5, "&Incluir tarefa")
win.Bind(wx.EVT_MENU, incluir_tarefa, id=5)
menu_editar.Append(6, "&Excluir tarefa")
win.Bind(wx.EVT_MENU, excluir_tarefa, id=6)
menu_ajuda = wx.Menu()
menu_ajuda.Append(7, "&Instalação do AutoTasks")
win.Bind(wx.EVT_MENU, exec_instalacao, id=7)
menu_ajuda.Append(8, "I&nicialização com Windows")
win.Bind(wx.EVT_MENU, exec_inicwindows, id=8)
menu_ajuda.AppendSeparator()
menu_ajuda.Append(9, "&Sobre...")
win.Bind(wx.EVT_MENU, exibir_sobre, id=9)
menu_bar = wx.MenuBar()
menu_bar.Append(menu_file, "A&rquivo")
menu_bar.Append(menu_editar, "E&ditar")
menu_bar.Append(menu_ajuda, "&Ajuda")
win.SetMenuBar(menu_bar)
# Status Bar
# ------------------------------------------------------------------------------------------------------------
win.CreateStatusBar()
win.SetStatusText('Python ' + sys.version)
win.Show(False)
app.MainLoop()
except Exception as ex:
print(ex)
if __name__ == "__main__":
main()