Skip to content

Instantly share code, notes, and snippets.

View SKaplanOfficial's full-sized avatar

Stephen Kaplan SKaplanOfficial

View GitHub Profile
@SKaplanOfficial
SKaplanOfficial / finder_window_controller.py
Created June 22, 2022 06:33
Setting the bounds of a Finder window with PyXA
import PyXA
app = PyXA.application("Finder")
window = app.windows()[0]
lock = False
old_w = 0
old_h = 0
while True:
if window.position.y < 50 and lock is False:
@SKaplanOfficial
SKaplanOfficial / flashcards_from_webpage_text.py
Created July 10, 2022 11:01
Using PyXA to extract paragraphs and sentences from a webpage, then create randomized flashcards from the webpage content
import os
from pprint import pprint
import PyXA
import random
from time import sleep
textedit = PyXA.application("TextEdit")
# Open a URL and wait for it to load
safari = PyXA.application("Safari")
@SKaplanOfficial
SKaplanOfficial / safari_methods_and_properties.py
Created July 15, 2022 00:51
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
@SKaplanOfficial
SKaplanOfficial / preview_itunes_top_10.py
Created August 29, 2022 07:29
Play the previews for the top 10 songs on iTunes parsed from Apple's RSS feed using PyXA
import PyXA
reader = PyXA.RSSFeed("http://ax.itunes.apple.com/WebObjects/MZStoreServices.woa/ws/RSS/topsongs/limit=10/xml")
links = reader.items().links()
m4as = filter(lambda x: "m4a" in x.url, links)
for index, song in enumerate(m4as):
print("Now playing: " + reader.items()[index].title)
sound = PyXA.XASound(song)
sound.play()
sleep(sound.duration)
@SKaplanOfficial
SKaplanOfficial / stitch_images.py
Last active October 19, 2022 19:04
PyXA script to stitch images together vertically and horizontally
import PyXA
from random import random
# Horizontal stitching
colors = [PyXA.XAColor(random(), 0, random()) for i in range(100)]
swatches = [c.make_swatch(10, 500) for c in colors]
PyXA.XAImage.horizontal_stitch(swatches).show_in_preview()
# Vertical stitching
files = ["/Users/exampleUser/Desktop/cat1.jpeg", "/Users/exampleUser/Desktop/cat2.jpeg", "/Users/exampleUser/Desktop/cat3.jpeg"]
@SKaplanOfficial
SKaplanOfficial / shortcut_url_input.py
Last active November 12, 2022 07:31
Using PyXA to run a shortcut with the URL of a Safari tab as input
# Tested with PyXA 0.1.0
import PyXA
shortcuts = PyXA.Application("Shortcuts")
safari = PyXA.Application("Safari")
document = safari.current_document
shortcut = shortcuts.shortcuts().by_name("Save As PDF")
shortcut.run(document.url)
@SKaplanOfficial
SKaplanOfficial / authenticate_system_preferences.py
Last active November 12, 2022 08:04
Using PyXA to reveal a System Preferences pane and prompt for authorization
# Tested with PyXA 0.1.0
import PyXA
from time import sleep
app = PyXA.Application("System Preferences")
app.activate()
pane = app.panes().by_name("Date & Time")
pane.reveal()
sleep(0.5) # Wait for animation to finish
pane.authorize()
@SKaplanOfficial
SKaplanOfficial / save_textedit_documents.py
Last active November 12, 2022 08:11
Save all currently open TextEdit documents with PyXA
# Tested with PyXA 0.1.0
import PyXA
app = PyXA.Application("TextEdit")
for doc in app.documents():
doc.save()
@SKaplanOfficial
SKaplanOfficial / print_textedit_document.py
Last active November 12, 2022 08:15
Printing a TextEdit document with print settings in PyXA
# Tested with PyXA 0.1.0
import PyXA
from datetime import datetime, timedelta
app = PyXA.Application("TextEdit")
doc = app.documents()[0]
print_time = datetime.now() + timedelta(minutes=1)
properties = {
"copies": 3,
"collating": False,
"startingPage": 1,
@SKaplanOfficial
SKaplanOfficial / open_messages_to_contact.py
Last active November 15, 2022 01:26
PyXA script to open Messages.app to the chat with the given participant
#!/usr/bin/env python
# Test with PyXA 0.1.0
import PyXA
messages = PyXA.Application("Messages")
person = messages.participants().by_name("Example Person")
PyXA.XAURL("sms://" + person.handle).open()