Ask HN: Small scripts, hacks and automations you're proud of?
I sometimes think back fondly of the small automation scripts I wrote over the years, so I wanted to ask you: Which scripts, hacks & automations did you create that your're proud of? Personally, I e.g. built a bot to automatically answer apartment ads [1], a small script to run Borg backup once per day [2], and a small automation to track the temperature in my aquarium [3].
1: https://gist.github.com/adewes/c9b2a71457c6c6f01f2f
2: https://gist.github.com/adewes/02e8a1f662d100a7ed80627801d0a...
3: https://gist.github.com/adewes/7a4c20a5a7379e19d78ba54521d3d...
334 comments
[ 4.2 ms ] story [ 238 ms ] thread[1]: http://karolis.koncevicius.lt/posts/fast_navigation_in_the_c...
This is useful for two things - 1) avoiding clashes with existing directories, 2) having a quick command to list all the bookmarks: cd @<tab>
[0] https://johnnydecimal.com/
https://til.hashrocket.com/posts/xsavbhlrz4-shortcuts-with-h...
I gave up on bookmarking so I now use an alias to quickly find what I am looking for with fzf and fd:
It sure took me ages to figure it was CDPATH, but CDPATH it is. I won't retry without making that "cd @" thing a subfunction (that might involve making its own completion, but that's easy compared to debugging make... I still don't know WHY it broke, just that it's CDPATH that broke it -- it decided to duplicate the subdir arg when invoking a submake so make was now left with a weird unknown target, but it obviously wouldn't say that and just failed weirdly)
If all these causes the build to fail then the maintainers of that build should be informed.
The solution is a simple bash script that deletes "target/" directories.
https://thealchemist.dev/clean-your-rust-clones/
https://github.com/jaysoffian/eap_proxy
But I started accumulating so many of them, it got to be a problem just remembering all the ones I had and how to trigger them. I had to start finding new ways of keeping them all organized. That led to figuring out two things that I'm still really proud of and use every day: custom right-click menus and Emacs-style keychords.
The custom right-click menu is: any time I right-click while also holding down Ctrl or Alt, it opens a right-click dialogue run by AutoHotkey, not the default one in the given context. That lets me visually select what I want to happen rather than needing an arcane key combo for every thing. All of my frequently-used Regex functions are right-click menus now.
The key chords are: if I trigger a "prep" hotkey first—my most-used one is "Alt+G" for "Go-to"—it sets up a keyboard hook that listens for the next key and then dispatches to a function based on that. So, "Alt+G H" would be really easy to remember in my head as "Go to Hacker news." And it pops open a new tab in my browser. I use that one a lot for opening desktop applications—"Alt+O C" maps in my head to "Open vs Code." Super easy to remember.
I've automated 80% of the most common navigation items at my job this way. My coworkers have seen me do it and stopped me to ask, "Wait, tell me how you did that." It looks and feels like magic.
Can you share a repo with your "custom right-click menus and Emacs-style keychords" to save me some time?
Love hacker news!
- Key chords: https://gist.github.com/CFiggers/ccb6bc911ee87f423f558368128...
- Right-click menus: https://gist.github.com/CFiggers/96bc37ca468909c9ec83252144c...
Let me know if you have questions. Enjoy!
I used the dialog box to create little 'applications' such as listing all the printers in the building (along with description) and allowing the staff to choose the printers they wanted to use (back around 2005 ish)
I had loads of these little scripts for staff to use - and some server side stuff (mainly windows) such as generating daily configs and collecting all the logs and looking for differences
I pretty much automated half my job, allowing me to do more interesting stuff
- Key chords: https://gist.github.com/CFiggers/ccb6bc911ee87f423f558368128...
- Right-click menus: https://gist.github.com/CFiggers/96bc37ca468909c9ec83252144c...
Let me know if you have questions. Enjoy!
https://askubuntu.com/a/1458448/1679924
To my knowledge, this was the first "site-specific" browser extension, so I wrote a blog post about it:
https://www.holovaty.com/writing/all-music-guide/
Aaron Boodman saw my post and decided to generalize the idea; he created Greasemonkey, which was a single browser extension that could aggregate site-specific JS customizations. In the years since, this idea of "user scripts" has further developed, and I think some browsers even support the concept natively.
I'm not aware of any browsers that support it natively, although TBH there are so many random Firefox forks that any one of them could have just bundled the extension and called it native
The way it would block ads is to analyze the files it was fetching for the browser and modifying found keywords, for a crude example all IMG tags with SRC containing the string ad.* would be replaced by gray images.
I also remember using it to "fix" a redesign of a site.
I'm pretty sure AllMusic was my source of track lengths for albums. Back in the middle of high school (so just before that redesign maybe?), I had some java utility that (I'm sure I'm imperfectly recalling) I pasted track information into. It then generated the Wikipedia markup equivalent, and I went around editing album pages on Wikipedia adding track lengths (and total album length).
And that's why I have something over 1000 wikipedia edits made a long time ago.
I'd never written a line of code in my life, so going through the iterative process of getting it functioning and fixing bugs was pretty invigorating. I could see where the generated code was going awry and describe it in natural language, and chatGPT would turn that into syntax and explain why it worked. Definitely a fun way to learn.
I'm sure it's ugly/inefficient, but here's the script:
https://pastebin.com/raw/DuAVAikD
I used to freelance when I learnt to code in college and I wrote many such small automation scripts for quite a few people. Looks like the AI is already taking away some jobs ;)
Damn right it is! Writing scrapers is definitely the gateway drug of programming. It's such an exciting feeling to step across the boundaries from "passive user at the mercy of the UI" to "I can do anything if I can conceptualise it (and break it up into steps)".
As a young person, I was on a trip to visit another ISP to learn how a VPN would work between our two companies. Their administrator, a wonderful person with 30 years of experience in the telco-turned-ISP world, had all these incredible aliases on her workstation for managing her fleet. But on the new vendor's system, a fresh install of the same OS left her almost unable to perform.
My jobs have all had the same opportunity for the same experience, and I've tried to leave it alone. I spend maybe 2/3 of my time logged into someone else's server, and I don't doubt myself, even knowing fully well it's a personal preference above all.
The vast majority of them are simple short forms. For example `tf` for `terraform` and `tfp` for `terraform plan`. My brain just naturally thknks of the full command when using the short form and, because of this, I'm not at a loss if I'm logged in somewhere without my aliases.
This one lets me drag/and drop things out of a terminal session (kind of) into applications with https://github.com/mwh/dragon and i use it way too often!
The problem: I want to know when content from one of my sites is submitted to Hacker News, and keep track of the points and comments over time. I also want to be alerted when it happens.
Solution: https://github.com/simonw/scrape-hacker-news-by-domain/
This repo does a LOT of things.
It's an implementation of my Git scraping pattern - https://simonwillison.net/2020/Oct/9/git-scraping/ - in that it runs a script once an hour to check for more content.
It scrapes https://news.ycombinator.com/from?site=simonwillison.net (scraping the HTML because this particular feature isn't supported by the Hacker News API) using shot-scraper - a tool I built for command-line browser automation: https://shot-scraper.datasette.io/
The scraper works by running this JavaScript against the page and recording the resulting JSON to the Git repository: https://github.com/simonw/scrape-hacker-news-by-domain/blob/...
That solves the "monitor and record any changes" bit.
But... I want alerts when my content shows up.
I solve that using three more tools I built: https://datasette.io/ and https://datasette.io/plugins/datasette-atom and https://datasette.cloud/
This script here runs to push the latest scraped JSON to my SQLite database hosted using my in-development SaaS platform, Datasette Cloud: https://github.com/simonw/scrape-hacker-news-by-domain/blob/...
I defined this SQL view https://simon.datasette.cloud/data/hacker_news_posts_atom which shows the latest data in the format required by the datasette-atom plugin.
Which means I can subscribe to the resulting Atom feed (add .atom to that URL) in NetNewsWire and get alerted when my content shows up on Hacker News!
I wrote a bit more about how this all works here: https://simonwillison.net/2022/Dec/2/datasette-write-api/
https://github.com/robswc/docker-essential-aliases
When deep diving into containers, I got tired of typing/tabbing out container names and docker formatting lol
Nothing revolutionary but it has saved me a ton of combined time.
https://github.com/charmbracelet/gum
If so, you just introduced me to an incredible looking project ahah.
https://charm.sh/
To automate this process, we have some ansible playbook to automate most of it: give it a query and you'll get a bunch of files with the results.
While useful, the setup and cleanup can get tidious. So, I whipped up a little shell script that creates a temporary directory and symlink it to a location somewhere in $XDG_STATE_HOME. I created a few subcommands to create the directory structure, add a query, run the query, aggregate results and destroy the state. This keeps things simple for me. Even if I forget to cleanup after myself, a reboot will make sure nothing is left behind. The symlink trick to xdg state home helps me a lot to track my state, while I work on a certain problem. It allows me to easily handle multiple request at the same time, when necessary.
AFAIK it doesn’t have an option to check a standalone CSS file.
I wrote a script to insert a CSS file into a dummy one-line HTML file, so that I can pass a CSS file to the script, and when errors are generated from the CSS, they are given using the correct line number.
I have written many more complex and perhaps more interesting Scripps, but this is the one I am, by far the most proud of.
I'm still impressed by my connect4 and 2048 solver
I probably scratched some parts together from the internet a couple of years ago.
Maybe something like this could do the same? But could also rely on that specific terminal emulator
So I automated the notification system using IFTTT.
https://github.com/ravivooda/thoughts/blob/main/docs/garage_...
https://github.com/Tade0/emergency-poncho
There are many like it, but this one is mine.
Useful when you're a front-end developer and the backend app is not easily deployable locally and the test environment is down.
Also with it you can make a blazing-fast, browsable snapshot of JIRA.
At analysis, files were containing multiple sweeps were averaged to reduce random noise (YYMMDDXX.Ann files), then these files were exported as a CSV-like series of time-voltage paires (YYMMDDXX.Tnn files). Tables containing the calculated variables were also generated, and exported (YYMMDD.Nnn files). The fun part is that these files had to be named MANUALLY. Each and every one of them. I can't stress hard enough how repetitive this got... We generated double-digit number of files every day, and analyzing each file thorougly required around 30-50 keypresses to move around in the menu and to name the files. Lucky for me, no mouse use was required, and keypresses could at least be automated.
I used DOSBox on Debian to do the analysis, and I ended up creating a bash script that could automatically analyze whole folders of these files in a few minutes. To achieve this, I generated xmacro files that would be played back while the DOSBox window was opened. Opening the file was also put in these xmacro files. The generation of the files was wrapped inside a bash script that kept track both of the files in the folder and of the files generated by the analysis. If a file was supposed to be there but some something broke inside DOSBox, it would just stop playing the macro for the next file, so it could be restarted relatively easily.
A few months later, I met the guy who wrote the software for our team, and asked him if he could write us a script to unpack the binaries into CSVs. From there, I could come up with my own completely automated solution for analysis, and everything was much-much faster. I also showed him the macro-monster I created. I'm still not sure if he was amazed or he just thought that I was impatient.
This way I can quickly boot any Pi in my house (and there are quite a few now) to any arbitrary OS image just by renaming a symlink on the server.