Skip to content

Instantly share code, notes, and snippets.

View SKaplanOfficial's full-sized avatar

Stephen Kaplan SKaplanOfficial

View GitHub Profile
@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 / saved_current_url.py
Last active September 15, 2023 16:22
PyXA script to save the current Safari tab's URL to a "Saved URLs" note
#!/usr/bin/env python
# Test with PyXA 0.1.0
import PyXA
safari = PyXA.Application("Safari")
notes = PyXA.Application("Notes")
# Get info for current Safari tab
current_tab = safari.front_window.current_tab
@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()
@SKaplanOfficial
SKaplanOfficial / select_photos_for_mosaic.py
Last active September 15, 2023 16:22
Using PyXA, Automator, and PIL to create a mosaic of selected images.
import PyXA, math
from PIL import Image
# Execute Automator workflow and receive list of image paths
automator = PyXA.Application("Automator")
workflow = automator.open("/Users/exampleuser/Library/Mobile Documents/com~apple~Automator/Documents/Ask For Photos.workflow")
image_paths = workflow.execute()
# Set base dimensions of mosaic images
base_width = 400
@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 / 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 / open_new_safari_tab.py
Last active November 15, 2022 01:33
Using PyXA to open a new tab or window of Safari
# Tested with PyXA 0.1.0
import PyXA
app = PyXA.Application("Safari")
new_tab = app.make("tab", {"URL": "http://google.com"})
app.front_window().tabs().push(new_tab)
@SKaplanOfficial
SKaplanOfficial / send_sms_to_chat.py
Last active November 15, 2022 01:32
Using PyXA to send an SMS to a chat based on the chat ID
# Tested with PyXA 0.1.0
import PyXA
app = PyXA.Application("Messages")
chat = app.chats().by_id("SMS;-;+11234567891")
chat.send("Hello!")
@SKaplanOfficial
SKaplanOfficial / prepend_date_to_textedit_documents.py
Last active November 15, 2022 01:32
Using PyXA to prepend a date at the beginning of every currently open TextEdit document
# Tested with PyXA 0.1.0
import PyXA
app = PyXA.Application("TextEdit")
documents = app.documents()
date = datetime.now()
documents.prepend(str(date) + "\n\n")
@SKaplanOfficial
SKaplanOfficial / list_textedit_text_elements.py
Last active November 15, 2022 01:31
Using PyXA to list all paragraphs, words, and characters in all currently open TextEdit documents
# Test with PyXA 0.1.0
import PyXA
app = PyXA.Application("TextEdit")
documents = app.documents()
print("Paragraphs:", documents.paragraphs())
print("Words:", documents.words())
print("Characters:", documents.characters())