Skip to content

Instantly share code, notes, and snippets.

@unixzii
Created January 17, 2025 06:15
Show Gist options
  • Save unixzii/5a3aa6645c560b4e7ee2eb39d14b56dc to your computer and use it in GitHub Desktop.
Save unixzii/5a3aa6645c560b4e7ee2eb39d14b56dc to your computer and use it in GitHub Desktop.
A program that memorizes your dock size
#!/usr/bin/env python3
import sys
import subprocess
import ctypes
import argparse
hi_dll = ctypes.CDLL('/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices')
def save_dock_size():
get_size_func = hi_dll.CoreDockGetTileSize
get_size_func.restype = ctypes.c_float
cur_size = get_size_func()
subprocess.run(
['defaults', 'write', 'com.apple.dock', 'memorizedTilesize', '-float', str(cur_size)],
check=True,
stderr=subprocess.DEVNULL,
stdout=subprocess.DEVNULL)
print("saved the current dock size: %d%%" % int(cur_size * 100))
def restore_dock_size():
try:
result = subprocess.run(
['defaults', 'read', 'com.apple.dock', 'memorizedTilesize'],
check=True,
stderr=subprocess.DEVNULL,
stdout=subprocess.PIPE)
memorized_size = float(result.stdout.decode().strip())
set_size_func = hi_dll.CoreDockSetTileSize
set_size_func.argtypes = [ctypes.c_float]
set_size_func(ctypes.c_float(memorized_size))
print("restored the memorized dock size: %d%%" % int(memorized_size * 100))
except subprocess.CalledProcessError:
print("error: no memorized dock size")
sys.exit(1)
parser = argparse.ArgumentParser(
prog='dock-memory',
description='A program that memorizes your dock size')
action_group = parser.add_mutually_exclusive_group()
action_group.add_argument('-s', action='store_true', help='Save the current dock size')
action_group.add_argument('-r', action='store_true', help='Restore the saved dock size')
args = parser.parse_args()
if args.s:
save_dock_size()
elif args.r:
restore_dock_size()
else:
print('error: one of the arguments -s or -r is required')
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment