Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vijayanandrp/eaa1e37a5e974105e4b3142d8cd6bcdd to your computer and use it in GitHub Desktop.
Save vijayanandrp/eaa1e37a5e974105e4b3142d8cd6bcdd to your computer and use it in GitHub Desktop.
How to do simple automation project using Python atomac in Mac OS X?
__author__ = 'vijay'
import os
import atomac
from atomac.AXKeyCodeConstants import *
import time
import commands
from atomac import AXKeyCodeConstants
# Open the Notes. Store the new content. Close it. Reopen and validate the stored content in the Note.app
global log_dir
global log_file
global log_fp
notes = 'com.apple.Notes' # CFBundleIdentifier
def logger(msg=''):
'''
TO Log the activites of the program
'''
try:
global log_fp
global log_dir
global log_file
if os.path.isfile(log_file):
log_fp.write("\n")
print msg
log_fp.write(msg)
else:
print "[+] Looks like the Log Directory or Log File is not found"
print "[+] Creating log file and directory for the first time"
os.system("mkdir -p "+log_dir)
os.system("touch "+log_file)
log_fp = open(log_file, 'a')
log_fp.write("\n")
print msg
log_fp.write(msg)
except Exception as er:
print "[-] Exception occurred while calling the logger "
print "[-] Error is "+str(er)
def open_app(cbundleID): # cbundleID = 'com.apple.Notes'
'''
To Open the Notes App
'''
try:
logger("[+] Opening the Notes.app")
atomac.launchAppByBundleId(cbundleID)
time.sleep(3)
notes_id = atomac.getAppRefByBundleId(cbundleID)
notes_id.activate()
except Exception as er:
logger("[-] Exception while opening the notes app")
logger("[-] Error @open_app is "+str(er))
def close_app(cbundleID): # cbundleID = 'com.apple.Notes'
'''
To Close the Notes App
'''
try:
logger("[+] Closing the Notes.app")
atomac.terminateAppByBundleId(cbundleID)
logger("Force Killing the process with signal")
os.system("ps -eaf|grep -i 'Notes'|grep -v grep|awk '{print $2}'|xargs kill ")
time.sleep(3)
except Exception as er:
logger("[-] Exception while closing the notes app")
logger("[-] Error @close_app is "+str(er))
def click_left(note):
'''
mouse left click action
'''
position = note.AXPosition
size = note.AXSize
clickpoint = ((position[0] + size[0] / 2), (position[1] + size[1] / 2))
try:
note.clickMouseButtonLeft(clickpoint)
except Exception as er:
logger("[-] Error @add_notes")
logger('[-] Error is '+str(er))
def click_right(note):
'''
mouse right click action
'''
position = note.AXPosition
size = note.AXSize
clickpoint = ((position[0] + size[0] / 2), (position[1] + size[1] / 2))
try:
note.clickMouseButtonRight(clickpoint)
except Exception as er:
logger("[-] Error @add_notes")
logger('[-] Error is '+str(er))
def add_notes():
'''
Click Add Notes button
'''
try:
notes_id = atomac.getAppRefByBundleId(notes)
note = notes_id.windows()[0].findAllR(AXRole='AXButton', AXRoleDescription='button', AXDescription='add')[0]
click_left(note)
except Exception as er:
logger("[-] Exception while adding the notes")
logger("[-] Error @add_notes is "+str(er))
def edit_notes(msg_note=''):
'''
Enter the input given to the notes
'''
try:
notes_id = atomac.getAppRefByBundleId(notes)
notes_id.activate()
note = notes_id.windows()[0].findAllR(AXRole='AXWebArea', AXRoleDescription='HTML content', AXDescription='')[0]
click_left(note)
note.sendKeys(msg_note)
time.sleep(1)
note.sendKeys('\n Vijay Anand Pandian ')
time.sleep(2)
note.sendKeyWithModifiers(";", [SHIFT])
note.sendKeyWithModifiers("0", [SHIFT])
time.sleep(3)
except Exception as er:
logger("[-] Exception while editing the notes")
logger("[-] Error @edit_notes is "+str(er))
def validate_notes(msg_validate=''):
'''
simple validation of UI function
'''
try:
notes_id = atomac.getAppRefByBundleId(notes)
notes_id.activate()
check = True
if len(msg_validate) > 1:
for i in range(len(notes_id.windows()[0].staticTextsR())):
try:
text = str(notes_id.windows()[0].staticTextsR()[i].AXValue.replace(u"\xa0", u""))
if msg_validate == text:
logger("Validation Succesfull")
print "Match is Found: "+text
check = False
break
except:
pass
if check:
logger("Validation Failed")
print "Match not Found"
else:
logger("Enter the correct value to validate")
except Exception as er:
logger("[-] Exception while validating the notes")
logger("[-] Error @validate_notes is "+str(er))
#
# def delete_notes(note_delete=''):
# try:
# notes_id = atomac.getAppRefByBundleId(notes)
# notes_id.activate()
# if len(note_delete) > 1:
# #click_right(notes_id.windows()[0].findAllR(AXRole='AXStaticText', AXRoleDescription='text', AXValue=note_delete+'\xa0')[0])
# time.sleep(1)
# click_left(notes_id.windows()[0].findAllR(AXRole='AXStaticText', AXRoleDescription='text', AXValue=note_delete+'\xa0')[0])
# time.sleep(1)
# notes_id.windows()[0].findAllR(AXRole='AXStaticText', AXRoleDescription='text', AXValue=note_delete+'\xa0')[0].sendKeyWithModifiers('q',[DELETE])
# time.sleep(1)
# # click_left(notes_id.windows()[0].findAllR(AXRole='AXMenuItem', AXRoleDescription='menu item', AXTitle='Delete')[0])
# # time.sleep(1)
# # click_left(notes_id.windows()[0].findAllR(AXRole='AXButton', AXRoleDescription='button', AXTitle='Delete Note')[0])
# # time.sleep(1)
# else:
# logger("Enter the correct value to validate")
# except Exception as er:
# logger("[-] Exception while deleting the notes")
# logger("[-] Error @deleting_notes is "+str(er))
def main():
global log_dir
global log_file
global log_fp
status, username = commands.getstatusoutput("users")
log_dir = '/Users/'+username+'/log/notes_test/'
log_file = '/Users/'+username+'/log/notes_test/notes_test.log'
try:
log_fp = open(log_file, 'a')
except:
pass
if __name__ == "__main__":
main()
open_app(notes)
time.sleep(3)
add_notes()
time.sleep(1)
msg_note="""FirstNote \n \n Hi This is My First Tutorial on ATOMac \n \n Glad You found this \n \n \n """
edit_notes(msg_note)
add_notes()
time.sleep(1)
msg_note="""SecondNote \n \n Hi This is My First Tutorial on ATOMac \n
\n Glad You found this second time \n \n \n """
edit_notes(msg_note)
validate_notes('FirstNote')
time.sleep(2)
validate_notes('SecondNote')
time.sleep(2)
# delete_notes('FirstNote')
# time.sleep(5)
# delete_notes('SecondNote')
# time.sleep(5)
close_app(notes)
logger("[*] Successfully opened and closed the Notes.app")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment