Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save SKaplanOfficial/8473fd8bf0a47468277688c1c481a552 to your computer and use it in GitHub Desktop.
Save SKaplanOfficial/8473fd8bf0a47468277688c1c481a552 to your computer and use it in GitHub Desktop.
Using PyXA to access Safari methods and properties
import PyXA
# Open URL in new tab
safari = PyXA.application("Safari")
# Get open windows, documents, and tabs
window1 = safari.front_window()
window2 = safari.windows()[1]
documents = safari.documents()
current_doc = safari.current_document
tabs = window1.tabs()
current_tab = window1.current_tab
# Get properties of documents
urls = documents.url()
names = documents.name()
html = current_doc.source()
# Get properties of tabs
urls = tabs.url()
texts = tabs.text()
name = current_tab.name()
# Filter documents and tabs
doc1 = documents.by_url("https://apple.com")
doc2 = documents.by_name("Apple")
tab1 = tabs.by_index(1)
tab2 = tabs.by_visible(True)
# Bulk document operations
documents.add_to_reading_list()
documents.email()
documents.do_javascript("alert('Testing 1 2 3');")
documents.search("Example")
# Bulk tab operations
tabs.reload()
tabs.add_to_reading_list()
tabs.email()
tabs.do_javascript("alert('Hello!');")
tabs.search("Example")
tabs.move_to(window2)
tabs.duplicate_to(window2)
# Sub-array operations
some_tabs = tabs[3:5]
some_tabs.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment