

Kerem\" and last name is \"Koseoglu\""Ĭmd = 'osascript -e "tell application \\\"Contacts\\\" to get formatted address'Ĭmd += ' of address of first person whose first name is \\\"'ĭef get_phone(name: str, surname: str) -> str: Of address of first person whose first name is \"Dr. Osascript -e "tell application \"Contacts\" to get formatted address Kerem", "Koseoglu"))ĭef get_address(name: str, surname: str) -> str: Here is a sample Python script, which can return address, phone or E-Mail of the given Mac contact. All we need to do is to call the terminal command and fetch its output. Hit enter, and you will hopefully see the E-Mail value on the terminal. Open a new terminal window, and enter the AppleScript command in the following manner: osascript -e "tell application \"Contacts\" to get value of email of first person whose first name is \"Dr.
#TYPE2PHONE APPLESCRIPT EXAMPLE MAC OS#
Luckily, Mac OS has a nice command line tool called osascript, which enables us this functionality. Terminalīefore jumping to Python, we need to ensure that we can run this AppleScript command in the terminal.
#TYPE2PHONE APPLESCRIPT EXAMPLE CODE#
If the AppleScript code in question is too complicated, you can create an external AppleScript file and trigger it via Python as well. Using the simple method in this article, you can do most of those via Python as well. Depending on the app you are accessing you can create records, fetch data, do bulk operations and whatnot. This means that, you can access your contacts over one line of code!ĪppleScript (and Shortcuts) is a very comprehensive system, and the amount of things that you can do is virtually unlimited. When you run this script, you will hopefully see the E-Mail address of the given contact on the screen. Open your Script Editor app, and write the following code (obviously replacing the name + last name): tell application "Contacts" to get value of email of first person whose first name is "Dr. Here is a working example over AppleScript – but the same logic can be applied to Shortcuts as well. However, this might be too cumbersome for simple tasks such as getting the E-Mail address of a Mac contact.įor such small tasks, your Python code can simply make an AppleScript or Shortcuts call and get the result – without directly touching the API at all.

As a Python programmer if you need to access a Mac OS API, the traditional way to go is to use the PyObjC library.
