Skip to content

Instantly share code, notes, and snippets.

View stephancasas's full-sized avatar

Stephan Casas stephancasas

View GitHub Profile
@stephancasas
stephancasas / residecar.jxa.js
Created April 3, 2023 21:12
Restart the macOS Sidecar session being hosted by avconferenced
#!/usr/bin/env osascript -l JavaScript
const App = Application.currentApplication();
App.includeStandardAdditions = true;
const BINARY = '/usr/libexec/avconferenced';
function run(_) {
const avconferenced = App.doShellScript(
`ps -A | grep '${BINARY}' | awk '{print $1,$4}'`,
@stephancasas
stephancasas / new-file-using-app-dock-menu.jxa.js
Created April 6, 2023 00:19
AXUIElement JXA Objective-C Bridge Example
#!/usr/bin/env osascript -l JavaScript
const App = Application.currentApplication();
App.includeStandardAdditions = true;
ObjC.import('Cocoa');
ObjC.bindFunction('AXUIElementCreateApplication', ['id', ['unsigned int']]);
ObjC.bindFunction('AXUIElementCopyAttributeValue', [
'int',
@stephancasas
stephancasas / core-ax.jxa.js
Last active December 3, 2024 10:29
JXA Core AX Framework Bindings
#!/usr/bin/env osascript -l JavaScript
function run() {
const VSCode = axApp('com.microsoft.VSCode');
const window = axGet(VSCode, 'AXWindows')[0];
return axWindowSetBounds(window, 200, 200, 1200, 1400);
}
/**
@stephancasas
stephancasas / fast-user-switch.jxa.js
Created April 18, 2023 19:56
JXA macOS Automation: Fast User Switching
#!/usr/bin/env osascript -l JavaScript
const USER = 'stephan';
function run() {
const ControlCenterPID =
$.NSRunningApplication.runningApplicationsWithBundleIdentifier(
'com.apple.controlcenter',
).firstObject.processIdentifier;
@stephancasas
stephancasas / close-other-vs-code-windows.jxa.js
Created April 20, 2023 22:46
Close all other VSCode windows
#!/usr/bin/env osascript -l JavaScript
function run(_) {
const pid = $.NSRunningApplication.runningApplicationsWithBundleIdentifier(
'com.microsoft.VSCode',
).firstObject.processIdentifier;
// Bring all VSCode windows to the current Mission Control view.
$.CGSProcessAssignToAllSpaces($.CGSMainConnectionID(), pid);
@stephancasas
stephancasas / bulk-rename-eml.jxa.js
Last active December 3, 2024 10:28
AppleScript: Bulk-rename export EML messages
#!/usr/bin/env osascript -l JavaScript
const App = Application.currentApplication();
App.includeStandardAdditions = true;
function run(_) {
// Prompt for folder selection.
const dir = `${App.chooseFolder({
withPrompt: 'Select the folder containing your exported messages...',
})}`;
@stephancasas
stephancasas / InlineHostTextView.swift
Created April 30, 2023 20:53
An NSHostingView inline inside an NSTextView
//
// InlineHostTextView.swift
// InlineHostTextView
//
// Created by Stephan Casas on 4/30/23.
//
import SwiftUI;
import AppKit;
import Combine;
@stephancasas
stephancasas / axnotifications.swift
Created May 10, 2023 13:02
Attach-to/Observe Every (Documeted) AXNotification
/// Where `axApplication` is an instance of an AXUIElement application and `self` is the receiving notification class.
AXObserverAddNotification(observer, axApplication, kAXMainWindowChangedNotification as CFString,
UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()));
AXObserverAddNotification(observer, axApplication, kAXFocusedWindowChangedNotification as CFString,
UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()));
AXObserverAddNotification(observer, axApplication, kAXFocusedUIElementChangedNotification as CFString,
UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()));
AXObserverAddNotification(observer, axApplication, kAXApplicationActivatedNotification as CFString,
UnsafeMutableRawPointer(Unmanaged.passUnretained(self).toOpaque()));
@stephancasas
stephancasas / new-untitled-textmate.jxa.js
Created May 18, 2023 16:58
Create New Untitled TextMate Document from AppleScript
#!/usr/bin/env osascript -l JavaScript
const $attr = Ref();
const $windows = Ref();
const $children = Ref();
function run(_) {
// Get TextMate AXUIElement
const textmatePid =
$.NSRunningApplication.runningApplicationsWithBundleIdentifier(
@stephancasas
stephancasas / toggle-do-not-disturb-ventura.jxa.js
Last active March 5, 2025 04:31
Toggle Do Not Disturb Ventura AppleScript
#!/usr/bin/env osascript -l JavaScript
/**
* -----------------------------------------------------------------------------
* Toggle Do Not Disturb Mode from Control Center
* -----------------------------------------------------------------------------
*
* Created on May 18, 2023 by Stephan Casas
*
* Options: