Archive
Keyboard automation
Problem
I had a text on the clipboard and I wanted to automate the Shift+Insert (paste) keypress. That is, I wanted to paste the content of the clipboard using a script.
Solution
I have a Bash script and when I run this script, this script at the end should paste the content of the clipboard.
First I came up with a Python solution using the excellent PyAutoGUI library:
python -c "import pyautogui; pyautogui.hotkey('shift', 'insert')"
It did the job but it required the pyautogui library to be installed. Then I found a solution that doesn’t need this dependency. This one relies on the Linux command xdotool:
xdotool key Shift+Insert
That’s it. Done :)
[ C lang] reading a text file line by line
Problem
I had a text file that I was reading line by line in C. Every line was tokenized. The file looked like this:
1978 Aachen Cathedral DE C EUR 0 3 1978 City of Quito EC C LAC 70 2 1978 Galápagos Islands EC N LAC 14066514 1 ...
I converted every year to an integer. From the 2nd line I got 1978 (as int), but in the first line the string to int conversion failed.
Solution
It turned out that the token “1978” in the first line was 7 characters long, not 4. At that point I opened the file in a hex editor and there were 3 special bytes at the beginning: EF BB BF. This is a byte order mark (BOM).

As a quick fix, I removed those 3 bytes in the hex editor.
I tried it under Python too and it caused a problem there too:
>>> f = open("input.txt")
>>> s = f.readline()
>>> s
'\ufeff1978\tAachen Cathedral\tDE\tC\tEUR\t0\t3\n'
>>> s.split()
['\ufeff1978', 'Aachen', 'Cathedral', 'DE', 'C', 'EUR', '0', '3']
>>> s.split()[0]
'\ufeff1978'
>>> int(s.split()[0])
Traceback (most recent call last):
File "", line 1, in
ValueError: invalid literal for int() with base 10: '\ufeff1978'
>>>
If you know how to ignore those bytes during file read, please leave a comment. I’m interested in both C and Python solutions.
crop images easily
Problem
You have lots of images and you want to crop them. How to do it without losing your hair?
Solution
I just found a nice little hobby project for this: https://github.com/weclaw1/inbac.
Under Manjaro, I had to install tkinter too: “sudo pacman -S tk“.
Reddit discussion here.
QuickJump: bookmark your directories and switch between them easily
I made a little project that facilitates jumping between directories in the command line. You can find it here: https://github.com/jabbalaci/quickjump . Visit the GitHub page for more info.
Demo
Getting started on Twitter
Problem
You have a site and you want to create a Twitter site for it too where you want to send posts, probably in an automatic way.
Solution
If you have a gmail account, you can use it to create several Twitter accounts. The trick is to make the e-mail addresses different with a dot. Example: myself@gmail.com and my.self@gmail.com . If you send an e-mail to these addresses, they’ll arrive in the same mailbox, though Twitter treats them as two different addresses.
Then, follow the steps at https://python-twitter.readthedocs.io/en/latest/getting_started.html to register your app.
Sample Python code
Here is a simple example to post a tweet:
#!/usr/bin/env python3
import config as cfg
import tweepy
def get_api(cfg):
d = cfg.twitter_keys
auth = tweepy.OAuthHandler(d['consumer_key'], d['consumer_secret'])
auth.set_access_token(d['access_token'], d['access_token_secret'])
return tweepy.API(auth)
def main():
api = get_api(cfg)
tweet = "just a test"
status = api.update_status(status=tweet) # update_status -> send a tweet
if __name__ == "__main__":
main()
TLDR: simplified and community-driven man pages
The TLDR pages are a community effort to simplify the beloved man pages with practical examples.
Links
- web interface
- book version (in PDF)
- tldr.py, Python client (I installed this one)
- discussion @reddit
(I also have a similar project called PrimCom but it has never become popular.)
[android] Mobile C
If you want to develop a C or Python program on your Android device, try Mobile C. Write your program, press a button, and it’s compiled (or interpreted) and executed. Several languages are supported: C, C++ 11, Python 3, JavaScript, Lua.
web server on localhost
Problem
I wanted to share a 3.3 GB big zip file with my students.
Python
I love Python, so I chose this simple trick: “python3 -m http.server“. It starts a web server and makes the content of the current directory available. I shared the URL with my students (10 people) and they started to download the big file at the same time. And it turned out that Python was not a good choice here since only 1 person could download the file and the others had to wait. This solution is single-threaded :(
Node.js
I hate Node.js but this time it provided the winning solution. Node.js’s async nature was perfect for the job, it could serve several clients. The download rate was not super fast, but at least it worked (and it worked well).
The following tip is from here.
$ sudo npm install http-server -g [sudo] password for jabba: /trash/opt/node-v5.1.0-linux-x64/bin/http-server -> /trash/opt/node-v5.1.0-linux-x64/lib/node_modules/http-server/bin/http-server /trash/opt/node-v5.1.0-linux-x64/bin/hs -> /trash/opt/node-v5.1.0-linux-x64/lib/node_modules/http-server/bin/http-server /trash/opt/node-v5.1.0-linux-x64/lib ...
I couldn’t launch it with “http-server“, but the full path “/trash/opt/node-v5.1.0-linux-x64/lib/node_modules/http-server/bin/http-server” did the trick.
Update (20171123)
Following the instructions at https://nodesource.com/blog/installing-node-js-tutorial-ubuntu/, I upgraded my Node.js to version 6.12.0. Then, installing http-server (with sudo npm install http-server -g), I got a link at “/usr/bin/http-server” that pointed to “/usr/lib/node_modules/http-server/bin/http-server“. Thus, I could start “http-server” without modifying the PATH.
JSON Path
I wrote a command-line program that outputs the full path of every key / value in a JSON file.
Example
$ ./json_path.py sample.json root.a => 1 root.b.c => 2 root.b.friends[0].best => Alice root.b.friends[1].second => Bob root.b.friends[2][0] => 5 root.b.friends[2][1] => 6 root.b.friends[2][2] => 7 root.b.friends[3][0].one => 1 root.b.friends[3][1].two => 2
More information at the project’s github page.
sending e-mails from your app.
If you want to send e-mails from your application, check out https://www.mailgun.com/. You can register for free and with your free account you can also send a good number of e-mails. And if you pay for it, you can send thousands of mails if you want. Their service can be used from Bash, Python, PHP, etc.
On my VPS I used a command-line solution (see here) that sends e-mails with sendmail. Unfortunately, Gmail treats these mails as spam, so it’s not appropriate for sending messages to other people. Mailgun is a better solution.

You must be logged in to post a comment.