Category Archives: Tips/techinques

Workrave, with no fake microbreaks

I’ve been using Workrave to help me remember to take breaks from the computer. I always end up not using it after a while though.

That’s because I get irritated by it because it gets frustrating after a while. I’ve always been a bit too optimistic about my breaks. Specifically because the program comes with an insane setting as standard.

However, this time I’ve set the settings to:

  • One two-minute microbreak every hour.
  • One ten minute break every three hours.
  • Eight hour PC-time, give me one extra hour three times if I ask for it.

Hope I’ll use it for a longer time now.

A key press is an activity!

I quickly found another of my common irritations after using it. Workrave is not detecting my keypresses properly, so even though I am sitting at the computer, it thinks I’m not.

This especially happens with the microbreaks, they never seem to actually work. I’m very often reading stuff on the screen, only pressing “Page down” button once every 10 seconds.

The reason Workrave behaves so strangely is because it uses an algorithm like this:

  • Get keyinput / mousemove or whatever
  • If mode is MODE_IDLE:
    • Set mode MODE_NOISE
    • Set last_time to now
  • Else if mode is MODE_NOISE:
    • If last_time is more than ACTIVITY ms ago, set mode to MODE_ACTIVE.

So, as you see, there’s a config setting ACTIVITY that messes things up. The code is not exactly like that, but close enough.

Anyway, when I found out that, I was happy. Easy fix!

But not so easy anyway. I saw there was a config system going on there, and tried finding the workrave.ini file residing in .workrave/. Sadly, I couldn’t find it, so I made it, with:

[monitor]
activity=0

Ok, that worked. It dumped a whole lot of new config options into that file, and when restarting Workrave, it stopped working. That was probably because many of the options were truly bogus.

Instead of fixing it, — I found out that my default config provider was gconf, so I rather did it that way.

The real fix

Opening up gconf-editor, I browsed to apps/workrave and inputted "monitor/activity" as an integer with value 0.

Restarting Workrave, and lo! It resets the counter when you press one button, just as I wanted!

Other irritations

It also doesn’t react on the mouse when it’s moved less than 3 pixels from where it was. That’s hard coded, so you’d have to change the source and compile it.

Another thing is that with the settings I have, which is 2 minute microbreak every hour, it’s still way too easy for it to register my 2 minute break when I did in fact not use it. It’s very possible for me to read a full screenful of a text (and think hard about it), or watch a video for over 2 minute.

Workrave has it’s own «reading mode» button for that. However, I don’t like that because

  1. I have to remember it
  2. It’s not easily accessible
  3. I have to remember turning it off

I could fix something with point 2 and Workrave’s dbus integration. But, well, it’s still a drag.

I think maybe we could raise the IDLE-time limit. These are the default limits:

NOISE = 9 seconds
ACTIVITY = 1 second
IDLE = 5 seconds

So we know what ACTIVITY is, I’ve set that to 0, so that it doesn’t need 2 events 1 second apart in order to register activity.

NOISE is how long until it waits until it sees a new keyboard input resets its ACTIVITY-lookup. Having that as 9 seconds is OK for me now that I’ve set ACTIVITY to 0.

IDLE is the time it waits until it starts counting. I’m not too happy with it at 5 seconds. I’m using Workrave with much bigger time lapses, so it should be really be upped.

I’ll put IDLE at 60 seconds, so that it won’t start counting IDLE time unless I’ve been off the keyboard for 60 seconds. So this will still make me take my 2 minute microbreak when I’m not touching the computer for 3 minutes, but it won’t happen as often.

So opening up gconf-editor and going to apps/workrave/monitor, and then inputting idle with integer value 0 will fix it.

And then Workrave is better to use!

Nicer pdf2png, with poppler

Updated: so poppler now includes pdftocairo which does this. No need to do this anymore! Blog post here for reference.

I’ve been using convert from ImageMagick to convert PDF-files to png files. However, they’re butt ugly, or rather fugly. So I created a pdf2png script / python program to do it better.

Just look at the text here from convert:

Rather ugly. Look at the kerning. It’s truly horrible.

Not to say poppler doesn’t have its share of problems, but it looks rather much better, don’t you agree?

So, since I had to manually edit a presentation I had to use some time making a PDF-to-PNG converter since I couldn’t find another pdf2png.

So without further ado, here is pdf2png.py (updated to use cairo as Raimund posted code for in comments, also updated with width+height extra params):

#!/usr/bin/env python
 
import poppler
import cairo
import gtk
import urllib
import sys, os
 
width = height = 0
 
if len(sys.argv) != 2 and len(sys.argv) != 4:
    print("Usage: %s <filename> [width height]")
    sys.exit()
 
if len(sys.argv) == 4:
    width = sys.argv[2]
    height = sys.argv[3]
 
input_filename = os.path.abspath(sys.argv[1])
output_filename = os.path.splitext(os.path.basename(sys.argv[1]))[0] + '-%.2d.png'
 
doc = poppler.document_new_from_file('file://%s' % \
            urllib.pathname2url(input_filename), password=None)
 
for i in xrange(doc.get_n_pages()):
    page = doc.get_page(i)
 
    if width and height:
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, int(width), int(height))
        ctx = cairo.Context(surface)
    else:
        surface = cairo.ImageSurface(cairo.FORMAT_ARGB32,
                int(page.get_size()[0] * 2), int(page.get_size()[1] * 2))
        ctx = cairo.Context(surface)
        ctx.scale(2, 2)
 
    page.render(ctx)
    ctx.set_operator(cairo.OPERATOR_DEST_OVER)
    ctx.set_source_rgb(1, 1, 1)
    ctx.paint()
    surface.write_to_png(output_filename % i)

It’s very far from perfect. Note the hard coded height and width, all of these things are possible fixes. Not anymore! The default should be sensible now, or you can force it with arguments. I didn’t find any python-poppler documentation, but I used C++-docs instead, they were helpful enough.

If you do any improvements (thanks Raimund) or just use it, it’d make me happy if you told me in a comment. :-)

Howto use subdomain WordPress Multisite 3.x without dashboard site?

So I wanted to log in to write about how to set up WordPress 3.x multisite to work with subdomains but the main site NOT running WordPress. Seriously, if you are okay with running WordPress on your main site (not only on subdomains), this guide is not for you. If you’re that girl, you’d want to check the normal network installation instructions.

WordPress is made to control the “host site”, so having this setup is speciality. I couldn’t find guides on doing this, so I’ve burned incredibly much time on it.

My setup (what I want to do)

  • I have a site; example.com, where I run a django site (or whatever else that’s NOT WordPress).
  • I want to have subdomain WordPress Multisite install, so I want myblog.example.com and yourblog.example.com.
  • I also want to have custom domains where I own it, so instead of using yourblog.example.com, I want to use yourblog.net.

So those three points should be easily doable, at least it sounds like an easy and nice requirement. Well, it was not so easy, but hopefully it’ll be easier with this guide.

Obviously you need to install WordPress, and set it up to be a Multisite-install. I’ll refer the normal steps here, but use the Create A Network instructions on the WordPress codex for a more fleshed out guide.

If you use the Codex-guide or have already set up WordPress Multiuser, add the two lines from my step 5 and start from step 8.

HOWTO

  1. Setup a normal WordPress blog. You know this. Install it normally. I set mine up as wp.example.com, this will be my dashboard site (so, NOT example.com like the normal Multisite setup). Update it, if there’s updates for it (I had one, probably because I set nn_NO as language in WPLANG).

  2. When done, open up wp-config.php and add the following line just before “That’s all …”

    define('WP_ALLOW_MULTISITE', true);
    
  3. Go to Admin -> Tools -> Network. Choose subdomain, it will say like site1.wp.example.com and site2.wp.example.com, we’ll just have to fix that later.

  4. Ignore Wildcard DNS error. Make blogs.dir:

    mkdir wp-content/blogs.dir
    sudo chown www-data wp-content/blogs.dir
    
  5. Replace the WP_ALLOW_MULTISITE in your wp-config.php with the following (the last two lines are different from a normal multisite install):

    define( 'MULTISITE', true );
    define( 'SUBDOMAIN_INSTALL', true );
    $base = '/';
    define( 'DOMAIN_CURRENT_SITE', 'wp.example.com' );
    define( 'PATH_CURRENT_SITE', '/' );
    define( 'SITE_ID_CURRENT_SITE', 1 );
    define( 'BLOG_ID_CURRENT_SITE', 1 );
    
    define( 'NOBLOGREDIRECT', 'http://example.com' );
    $cookie_domain = '.example.com';
    

    PS In my current install, I did something very wrong, I didn’t make the dashboard site first, and so I had to change BLOG_ID_CURRENT_SITE to 9.

  6. If you use apache, add this to your .htaccess-file:

    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    
    # uploaded files
    RewriteRule ^files/(.+) wp-includes/ms-files.php?file=$1 [L]
    
    RewriteCond %{REQUEST_FILENAME} -f [OR]
    RewriteCond %{REQUEST_FILENAME} -d
    RewriteRule ^ - [L]
    RewriteRule . index.php [L]
    

    However, if you’re like me, and use nginx, I use this rule to enable WordPress Multisite:

    location / {
        index  index.html index.htm index.php;
        rewrite ^.*/files/(.*)$ /wp-includes/ms-files.php?file=$1 last;
        if (!-e $request_filename) {
            rewrite ^.+/?(/wp-.*) $1 last;
            rewrite ^.+/?(/.*\.php)$ $1 last;
            rewrite ^(.+)$ /index.php?q=$1 last;
        }
    }
    
    location ~* ^.+\.(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|ogv|ogg)$
    {
        rewrite ^.*/files/(.*(html|jpg|jpeg|gif|png|ico|css|zip|tgz|gz|rar|bz2|doc|xls|exe|pdf|ppt|txt|tar|mid|midi|wav|bmp|rtf|js|ogv|ogg))$ /wp-includes/ms-files.php?file=$1 last;
        expires 30d;
        break;
    }
    
  7. Press the “login again” button on the install (or just login again :P).

  8. Install my plugin, Remove Domain Part (Direct download). All it does is to remove .wp. from the domains so that site1.wp.example.com becomes site1.example.com on blog installation (when you choose “New site”).

    1. If you use something else than .wp., you need to change it in the REMOVE_DOMAIN_PART definition in the plugin.

    2. Network Activate the plugin (if you don’t use fontend signups, you may choose to only activate it on the main site).

  9. Install WordPress MU Domain Mapping, follow the install instructions. Short version:

    1. Link sunrise.php (or merge if you’ve got it from before) to the wp-content folder. > ln -s plugins/wordpress-mu-domain-mapping/sunrise.php wp-content/
    2. Add define( 'SUNRISE', 'on' ); to wp-config.php.
    3. Network Activate it…
  10. And now for the small hack that will make domain mapping work again with our setup. Open up your wp-content/sunrise.php, and add this elseif to the last if-block (all the way at the bottom):

    elseif ( isset($cookie_domain) )
        define( 'COOKIE_DOMAIN', $cookie_domain );
    

    Many people error out here, check a example paste of their new sunrise.php someone in the comments did.

  11. Log in, go into Network Admin and set up Domain Mapping. The domains themselves you can map from the site admin pages, under Tools.


And now everything should work.

Send files via SSH/SCP with Nautilus

I wanted a quick and easy way to share files, images, scripts etc. I searched for this about two years ago, but couldn’t find it. So I wrote my own, and wrote about it on my norwegian blog.

Recently Omg! Ubuntu! wrote about a Nautilus Imgur-uploading script, and I wrote a small comment saying that I’ve used the same for some time, only to my own servers and using SCP. I said it also supported FTP, but I don’t know where I took that from, it doesn’t and I have no need for FTP :-)

Anyway, this is how it works:

  1. Right click file (or files!) in Nautilus
  2. Choose script -> Send to Thor (where Thor is the name of a server)
  3. Wait a bit, and you will get a small notification saying that the file is uploaded
  4. To make it easy to share, the script sets your selection clipboard to the url, so that you can just paste away

I use this directory structure, mostly:

example.com/rubbish/$name_of_computer/uploaded_file

You may download the send_via_scp script here.

I’m also pasting in this version of the script, just so that it doesn’t disappear.

[code lang=”bash”] #!/bin/bash # Send your files to another machine using SCP # REQUIRED # – Nautilus (doh!) # – scp # – notify-send (from package notify-bin, libnotify – for messages) # – xclip (to automaticaly set your clipboard to the URL of the file) # To use (for single server): # 1. Copy to ${HOME}/.gnome2/nautilus-scripts # 2. Rename to "Send to MYSERVER" and make executable (chmod a+x) # 3. Change the options below # # To use (for multiple servers): # 1. Copy to your ${HOME}/.gnome2/nautilus-scripts directory # 2. Make a file named "Send to MYSERVER" in the same directory # 3. Put this into it: # ————— # #!/bin/bash # # SCP_URL="odin@odin.s0.no:public_html/rot/`hostname`/" # HTTP_URL="http://odin.s0.no/rot/`hostname`/" # PROGNAME=`basename "$0"` # dir=`dirname "$0"` # # export SCP_URL HTTP_URL PROGNAME # source $dir/send_via_scp # ————— # # 4. Make it executable (you don’t have to make send_via_scp executable) # Author: Odin Hørthe Omdal < odin.omdal at gmail [dot] com > # Version: 1.1 # Based upon the script to make symbolic links by # Author: Jon Green < g-scripts [at] green-lines [dot] com > # Version: 1.0 if [ "x$SCP_URL" == "x" ]; then # OPTIONS, edit these SCP_URL="user@domain:public_html/folder/" HTTP_URL="http://domain/folder/" # This is the name it will use for notifications PROGNAME=`basename "$0"` fi #### You don’t have to edit below here ### warning() { notify-send -i gtk-dialog-error "$PROGNAME" "$*" } message() { notify-send -i gtk-dialog-info "$PROGNAME" "$*" } declare -a NAUTFILES export IX=0 while read FILE; do if [ "x${FILE}" != "x" -a "x${FILE}" != ‘x"’ ]; then NAUTFILES[${IX}]="${FILE}" IX=$[ ${IX} + 1 ] fi done <<EOF ${NAUTILUS_SCRIPT_SELECTED_FILE_PATHS[@]} EOF if [ 0 -eq ${IX} ]; then URI="${NAUTILUS_SCRIPT_CURRENT_URI}" METHOD="${URI:0:7}" if [ "file://" == "${METHOD}" ]; then NAUTFILES[0]="${URI:7}" IX=1 fi fi if [ 0 == "${#NAUTFILES[@]}" ]; then warning "Nothing to do" exit fi for FILE in "${NAUTFILES[@]}"; do FILE_NAME=`basename "${FILE}"`; if ! scp "${FILE}" "$SCP_URL"; then warning "Couldn’t send ${FILE_NAME}" else message "Uploaded to <a href=’${HTTP_URL}${FILE_NAME}’>${HTTP_URL}${FILE_NAME}</a>" fi # Save the URL to the clip board echo "${HTTP_URL}${FILE_NAME}" | xclip done [/code]

Installing (compiling) DVswitch on Ubuntu

DVswitch is a great program, where you can connect a few laptops in a network, and have a DV-camera connected to each of them. Then, on one computer you use the main dvswitch-program and you can mix all the videosources easily. It’s really a great program! I’ll come back with a more thorough walkthrough (well, guess I’ll just make a video of it ;-) )

I just need this as a quick reminder, so I don’t have to run cmake 10.000 times to find out the dependencies.

sudo aptitude install build-essential libavformat-dev libavcodec-dev libavutil-dev \
libtheora-dev libogg-dev libvorbis-dev libavc1394-dev libasound2-dev subversion \
cmake libgtkmm-2.4-dev libboost-thread-dev libxv-dev

Then do a checkout

svn co svn://svn.debian.org/dvswitch/

Check it with cmake (and remove that cache-file between each try, — there shouldn’t be more than one now anyway)

rm CMakeCache.txt ; cmake .

That dot is important. And if it’s all good, compile it:

make

Then make the share-directory:

sudo ln -s pwd/data /usr/local/share/dvswitch

And that should be it. You might want to put the src-dir in the path as well.

Mail Merge database + PDF for free in Linux (w/o big overhead)

Update: go directly to solution

I’ve lost many hours of high quality chair sleep over a certain PDF-problem that I’ve been tasked to work on these last months. We’re actually trying to do something extremely easy, this list summarizes it:

  • mail merge a PDF

And that’s about it. Oh, there’s also this tiny would-be-nice requirement; it has to work.

There are proprietary solutions, they are surprisingly extremely expensive. This is something we’d do often so can’t pay us out of it. If something had a one-time cost we could easily do it. But alas.

I got a ready PDF with picture of a boat and some text from our graphics dude, and a big list of names. I was tasked to merge the boat and the list. So armed with my CSV-file and the PDF I started thinking how this could be done…

And there goes months of my life

I tried many things. First using my youth searching the intertubes for clues on how to best proceed. I was bogged down by a number of commercial tools, and the Java library iText. I found FPDF for PHP creation of PDF’s and a whole lot more. No half-easy method of doing this short of writing my own program.

I wasn’t interested in that.

I then tried the new OpenOffice 3.0 and the PDF-plugin. Put the PDF as a background and did mail merge on top of that. No such luck. The new 64bit ubuntu computer with a dogpile of RAM just died.

And so I gave up. I printed 6.000, I then lined up the fields from my CSV and did a mail merge in OpenOffice, so I put the 6.000 printed pages and put them into the printer again. Of course, we now paid for 12.000 pages, and it took a long time.

And there we go again, 30.000 this time

Just when I had finally forgotten the whole thing. … Let’s do it again! Only this time, no cheating.

I found pdftk (why didn’t I last time!?). I think I found a way. So I started making fields on the PDF’s. Then using FDF’s to fill the fields automatically. Formfiller.

So, just it’s just to write a script to make 30.000 PDF-files!

PROBLEM: Each file is easily 2 MB. That’ll be 60.000 MB. No way. It’ll take forever. The printer won’t like it, the computer won’t like it, the network won’t like it. They’ll all hate it.

So, solution; pdftk + background

I had only been reading “relevant” parts of the pdftk manual. I only saw what I wanted to see, and that was not background.

The merging I’ve been looking for was right there. And so the years I spent on this problem were finished! Here’s the procedure:

  1. Use OpenOffice to mail merge all the names just like blank pages.
  2. Export as PDF.
    You’ll get a big, blank document with names on each page.
  3. Open a terminal, and add the boat as a background to your 30.000 page PDF:
    pdftk names.pdf background boat_background.pdf output out.pdf

And there you go. The sweet deal about this, is that the background is only saved once, and referenced on all the other pages. Nice, just like I wanted.

Update: I trimmed this blog post a bit. Also, in the end I wrote a python-program that creates the PDF-to-be-merged with reportlab and does the nice pdftk-trick in the end. It went from being a full-day job to taking about 2 minutes to do a 30.000 page merge.