Skip to content

Instantly share code, notes, and snippets.

View SKaplanOfficial's full-sized avatar

Stephen Kaplan SKaplanOfficial

View GitHub Profile
@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 / 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 / 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 / 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 / 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 / sort_desktop_files.py
Last active January 18, 2023 22:52
PyXA script to sort files on the desktop into appropriate category folders
import PyXA # Version 0.2.1
app = PyXA.Application("System Events")
desktop_files = app.desktop_folder.files()
desktop_folders = app.desktop_folder.folders()
# Create sorting bin folders
images_folder = app.make("folder", {"name": "Images"})
videos_folder = app.make("folder", {"name": "Videos"})
audio_folder = app.make("folder", {"name": "Audio"})
@SKaplanOfficial
SKaplanOfficial / run_flow_when_notes_open.py
Last active January 18, 2023 21:54
PyXA script to run a Flow session only when a specific application (Notes) is open. Session stops when Notes is closed and restarts if the app is opened again.
import PyXA # Version 0.2.0
from time import sleep
flow = PyXA.Application("Flow")
in_session = False
while True:
apps = PyXA.running_applications().localized_name()
if "Notes" in apps and not in_session:
flow.start()
@SKaplanOfficial
SKaplanOfficial / junk_email_classifier.py
Last active January 18, 2023 21:56
PyXA script to classify email subject lines as junk or other using Latent Semantic Mapping
import PyXA # Version 0.2.0
app = PyXA.Application("Mail")
dataset = {
"junk": app.accounts()[0].mailboxes().by_name("Junk").messages().subject(),
"other": app.accounts()[0].mailboxes().by_name("INBOX").messages().subject()
}
queries = [
"Amazon Web Services Billing Statement Available",
@SKaplanOfficial
SKaplanOfficial / simple_sentiment_tagging.py
Last active January 18, 2023 21:57
Sentiment tagging with PyXA
import PyXA # Version 0.2.0
# Use the default sentiment scale
text = PyXA.XAText("This sucks.\nBut this is great!")
print(text.tag_sentiments())
# [('This sucks.\n', 'Negative'), ('But this is great!', 'Positive')]
# Use a custom sentiment scale
text = PyXA.XAText("This sucks.\nBut this is good!\nAnd this is great!")
print(text.tag_sentiments(sentiment_scale=["Very Negative", "Negative", "Somewhat Negative", "Neutral", "Somewhat Positive", "Positive", "Very Positive"]))