Skip to content

Instantly share code, notes, and snippets.

View ttscoff's full-sized avatar
💭
Breathing

Brett Terpstra ttscoff

💭
Breathing
View GitHub Profile
@ttscoff
ttscoff / macosDND.m
Last active September 21, 2022 14:44
Objective-C code for toggling Do Not Disturb on macOS (including Big Sur)
/// Turn Do Not Disturb on or off
/// Only handles on and off, no schedules
/// @param enabled true on, false off
- (void)setDND:(BOOL)enabled {
NSLog(@"Turning Do Not Disturb %@", (enabled ? @"on" : @"off"));
if (@available(macOS 11.0, *)) {
NSUserDefaults * defaults = [[NSUserDefaults alloc] initWithSuiteName:@"com.apple.ncprefs"];
NSData * subPlistData = [defaults objectForKey:@"dnd_prefs"];
NSMutableDictionary * propertyListDict = [[NSPropertyListSerialization propertyListWithData:subPlistData options:0 format:nil error:nil] mutableCopy];
@ttscoff
ttscoff / pins-bookmarklet.js
Created June 29, 2021 13:04
Bookmarklet for Pins.app (1.6+)
// Bookmarklet for [Pins.app](https://get-pins.app/) (requires version 1.6+)
// Removes Google Analytics strings
// Removes Product Hunt query string
// Prefers canonical url if defined
// Uses selected text for description, falling back to meta description
// Removes pipes (|) from title
// To allow use on any website in Firefox, go to about:config and set security.external_protocol_requires_permission to false
// Copy this to your URL bar:
javascript:!function(){function e(e){var n="pins://launch?screen=newPost&",i=o();""===i&&(i=r()),n+="url="+encodeURIComponent(e),n+="&",n+="title="+encodeURIComponent(t()),n+="&",n+="description="+encodeURIComponent(i),document.location.href=n}function t(){var e=document.title;return e.replace(/\|/g,"-")}function n(){for(var e=document.getElementsByTagName("link"),t=0;t<e.length;t++)if(e[t].attributes.hasOwnProperty("rel")&&"canonical"===e[t].attributes.rel.textContent.toLowerCase())return e[t].attributes.href.textContent;var n,o=document.location.href.split(/[?&]([^&]+)/
@ttscoff
ttscoff / btt_stats.rb
Last active June 25, 2021 17:19
Tools for BetterTouchTool script widgets (touch bar and menu bar)
#!/usr/bin/env ruby
# frozen_string_literal: true
# Memory and CPU stats formatted to use in a BetterTouchTool touch bar script widget
# Also works in menu bar (Other Triggers), but be sure to select "Use mono space font"
require 'optparse'
# Settings
settings = {
chart_width: 8, # character width of chart, > 2
# Colors for level indicator, can be hex or rgb(a)
@ttscoff
ttscoff / cpupercent.md
Last active June 23, 2021 23:54
Oneliner to show CPU usage as percent of total cores

Using this as a BetterTouchTool script widget:

  1. launch path: /bin/bash
  2. parameters: -c
  3. script: paste cpupercent.sh contents
@ttscoff
ttscoff / findgist.fish
Last active June 24, 2021 10:30
Fish function for using fuzzy command line searching of your gists
function findgist --description 'select gist from list and display contents'
# requires fzf (brew install fzf) and gist (brew install gist)
gist -r (gist -l | awk -F/ '{print $NF}' | fzf --layout="reverse" -q "$argv" | awk '{ print $1 }')
end
@ttscoff
ttscoff / btt_cpubar.rb
Last active June 24, 2021 11:06
A little script to put a CPU usage graph in the Touch Bar using a BetterTouchTool script widget
#!/usr/bin/env ruby
# frozen_string_literal: true
# Settings
chart_width = 8 # character width of chart, > 2
# Colors for level indicator, can be hex or rgb(a)
# max is cutoff percentage for indicator level
colors = [
{
#!/usr/bin/env ruby
# frozen_string_literal: true
chart_width = 20
cores = `sysctl -n hw.ncpu`.to_i
loads = `sysctl -n vm.loadavg`.split(/ /)[1..3]
unit = (chart_width.to_f / 100)
chart = Array.new(chart_width, '░')
if ((/^localhost/).test(document.location.host)) {
var linkChecker = (function() {
"use strict";
let links, errors = [], completed = [], errorCounter = 0;
function addStyle(styleString) {
const style = document.createElement("style");
style.textContent = styleString;
document.head.append(style);
}
@ttscoff
ttscoff / close zoom tabs.applescript
Created November 6, 2020 15:38
Close all those xxx.zoom.us tabs that got left open in Safari
use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions
set tabsToClose to {}
tell application "Safari"
-- cycle through all open windows
repeat with _window in windows
-- cycle through all tabs of given window
repeat with _tab in tabs of _window
@ttscoff
ttscoff / geticonpng.bash
Created September 12, 2020 11:37
Grab a PNG of any local Mac app icon
#!/bin/bash
# get any app's full-size icon, PNG version
function geticonpng() {
# Autocompletes app names with modified .app_completions scripts
APP=`echo $1|sed -e 's/\.app$//'`
APPDIR=''
for dir in "/Applications/" "/Applications/Setapp/" "/Applications/Utilities/" "/Users/ttscoff/Applications/" "/Developer/Applications/" "/Developer/Applications/Utilties/" "/System/Applications/"; do
if [[ -d "${dir}$APP.app" ]]; then
APPDIR="$dir"