Getting User Input
PyXA provides several ways to get user input via dialogs, menus, and other familiar means. You can still use Python’s input()
method to get keyboard input, and you can use Python’s built-in support for commandline arguments to get input in that way as well. Additional input methods (such as detecting keystrokes and/or mouseclicks in other applications) will be added in a future version of PyXA, but other packages exist that already offer such functionality.
Alerts
import PyXA
response = PyXA.XAAlert(
title = "Alert!",
message = "",
style = PyXA.XAAlertStyle.INFORMATIONAL,
buttons = ["Ok", "Cancel"]
).display()
print(response)
# 1000
Dialogs
import PyXA
response = PyXA.XADialog(
text = "This is a dialog",
title = "Notice",
buttons = ["Ok", "Cool", "Thanks"],
icon = "caution",
).display()
print(response)
# Cool
import PyXA
response = PyXA.XADialog(
text = "What is your name?",
title = "What is your name?",
buttons = ["Continue"],
default_button = "Continue",
icon = "note",
default_answer = ""
).display()
print("Your name is", response[1])
# Your name is Steven
import PyXA
response = PyXA.XADialog(
text = "Enter the secret",
title = "Super Secret",
buttons = ["Continue"],
default_button = "Continue",
icon = "note",
default_answer = "",
hidden_answer = True
).display()
print("The secret message was", response[1])
# The secret message was 42
File and Folder Pickers
import PyXA
response = PyXA.XAFilePicker(
prompt = "Choose File",
types = ["png"],
default_location = "/",
show_invisibles = False,
multiple_selections_allowed = False,
show_package_contents = False
).display()
print(response)
# <<class 'PyXA.XABase.XAPath'>file:///Users/ExampleUser/Desktop/Example.png>
import PyXA
response = PyXA.XAFolderPicker(
prompt = "Choose Folder",
default_location = "/",
show_invisibles = False,
multiple_selections_allowed = True,
show_package_contents = False
).display()
print(response)
# [<<class 'PyXA.XABase.XAPath'>file:///Applications/>, <<class 'PyXA.XABase.XAPath'>file:///Library/>]
File Name Dialogs
import PyXA
response = PyXA.XAFileNameDialog(
prompt = "Choose Folder",
default_name = "New File",
default_location = "/Users/Shared",
).display()
print(response)
# <<class 'PyXA.XABase.XAPath'>file:///Users/Shared/New%20File>
Color Pickers
import PyXA
response = PyXA.XAColorPicker(
style = PyXA.XAColorPickerStyle.CRAYONS
).display()
print(response)
# <<class 'PyXA.XABase.XAColor'>r=1.0, g=0.8323456645, b=0.4732058644, a=1.0>
Command Detectors
import PyXA
def open_google():
PyXA.XAURL("https://google.com").open()
detector = PyXA.XACommandDetector()
detector.on_detect("go to Google", open_google)
detector.on_detect("go to Bing", lambda : PyXA.XAURL("https://bing.com").open())
detector.listen()
If you want to detect an exact command and display the standard macOS Voice Control interface, use a PyXA.XABase.XACommandDetector
object. If you want to detect whether a user’s spoken input passes a certain rule, or if you want the command detection to occur without displaying an graphical interface, use PyXA.XABase.XASpeechRecognizer
.
Speech Recognizers
import PyXA
listener = PyXA.XASpeechRecognizer()
result = listener.listen()
if result == "Hi":
PyXA.speak("Hey!")
import PyXA
import re
listener = PyXA.XASpeechRecognizer({
lambda s: s.lower() == "open google": lambda _: PyXA.XAURL("https://google.com").open(),
})
regex = re.compile(r'^(Open|Go to|Site|Jump to|Show me) (b|B)ing')
listener.on_detect(lambda s: regex.match(s) != None, lambda _: PyXA.XAURL("https://bing.com").open())
result = listener.listen()
import PyXA
def detect_website_query(query: str) -> bool:
"""Detects queries of the form "Go to [website name]".
:param query: The query to assess
:type query: str
:return: True if the query matches the form "Go to [website name]" for any of the supported websites
:rtype: bool
"""
site_names = ["google", "bing", "duckduckgo", "yahoo"]
return query.startswith("Go to ") and any([query.lower().endswith(x) for x in site_names])
def go_to_website(query: str) -> PyXA.XAURL:
"""Opens a website specified in a query of the form "Go to [website name]".
:param query: The query to respond to
:type query: str
:return: The url as a PyXA XAURL object
:rtype: PyXA.XAURL
"""
site = query[6:]
url = PyXA.XAURL("https://" + site + ".com")
url.open()
return url
listener = PyXA.XASpeechRecognizer({
detect_website_query: go_to_website,
})
result = listener.listen()
print(result)
# <<class 'PyXA.XABase.XAURL'>https://Bing.com>