Thursday, 29 March 2012

Workaround for copy & paste with Inkscape in OS X

There is a bug in OS X with X11 that affects copy and paste between X11 windows when using Inkscape. Basically, vector snippets that are copied in one window paste in another as bitmap rather than vector.

Solution (workaround):

Go into the X11 preferences (click on X11 icon in your dock, then in the menu bar X11->preferences...), and go to the tab that says pasteboard and uncheck: Update Pasteboard when CLIPBOARD changes.

Thursday, 15 March 2012

Bash shell script confirmation prompt

To prompt for user confirmation before performing a dangerous action in a bash shell script, you can use the following code:

read -p "Confirm (y/n)? " -n 1 -r
if [[ $REPLY =~ ^[Yy]$ ]]
then
  # get cursor on next line (optional)
  echo
  # do dangerous stuff here
fi

Wednesday, 14 March 2012

Ignore all .svn folders with .gitignore

If you find yourself looking to use both Subversion and Git together for any reason, in order to ignore all .svn folders in a project directory tree using your .gitignore file, add the following to your .gitignore file:

.svn

This assumes that no .svn directories or files inside of them have been committed yet. Also, note that there aren't any wildcard characters, (e.g. "*"). If you are using GitX, refresh your 'commit' view to see the changes after making this change to your .gitignore file.

Tuesday, 13 March 2012

Random Symfony2 reminders

This post is basically a crib sheet of Symfony 2 reminders for myself, but you're welcomed to use it if it helps. It's a short list right now, but I'll be periodically adding to it.

List available app/console commands

$ php app/console list

Useful DQL examples


Install web assets

$ php app/console assets:install web

The optional parameter --symlink will use symbolic links instead.


Force schema update

$ php app/console doctrine:schema:update --force

Note: consider using Doctrine Migrations instead


Create tables

$ php app/console doctrine:schema:create

Note: don't run this in a production setting


Create database

$ php app/console doctrine:database:create

Generate setters and getters

$ php app/console doctrine:generate:entities Package

Generates setters and getters for all entities in Package.

ORM annotation Column() options

  • type (optional, default 'string')
  • name (optional, default to field name)
  • length (optional, default 255)
  • unique (optional, default FALSE)
  • nullable (optional, default FALSE)
  • precision (optional, default 0) The precision for a decimal column.
  • scale (optional, default 0) The scale for a decimal column.
References:

Install doctrine fixtures in Symfony 2.1

Step 1 (only if necessary): install Composer

$ curl -s http://getcomposer.org/installer | php

Step 2: Update composer.json

{
    "require": {
        "doctrine/doctrine-fixtures-bundle": "dev-master"
    }
}

Step 3 (optional): allow only stable updates in composer.json

Look for the key "minimum-stability", and change its value from "dev" to "stable", e.g.

"minimum-stability": "stable",

Step 4: update vendors library

$ php composer.phar update
Step 5: register in AppKernal.php
// ...
public function registerBundles()
{
    $bundles = array(
        // ...
        new Doctrine\Bundle\FixturesBundle\DoctrineFixturesBundle(),
        // ...
    );
    // ...
}
References:

Generate a new bundle

$ php app/console generate:bundle --namespace=Package/YourBundle --format=yml

Generates a new bundle named PackageYourBundle in the namespace named Package/YourBundle.

Friday, 9 March 2012

Full Google Calendar in Playbook (or mobile browser)

Back Story:
During the price drop of last year I got a Blackberry Playbook prior to the version 2 update (before it had a decent calendar app). When trying to use Google Calendar in its place, one of the things I ran into was that Google Calendar would default to the mobile site in a browser, whereas some friends and I really liked using the full site at the time. We ran into this issue with some other mobile devices, too.

Solution:
One way to get the full Google Calendar to display in the browser is to use the following URL:
https://www.google.com/calendar/render?gsessionid=ok 

Additional Notes:
Bookmarking this URL allows for easy access to this web app, and this workaround solution still worked as of March 2012.

Thursday, 8 March 2012

Install PHPUnit in MAMP

To install PHPUnit in MAMP 2.0.5, run the following commands in Terminal, noting that the pear.conf file needs to be deleted first. Also, ignore the premature word-wrapping in the commands as they should be one line each.

rm /Applications/MAMP/bin/php/php5.3.6/conf/pear.conf

/Applications/MAMP/bin/php/php5.3.6/bin/pear channel-update pear.php.net

/Applications/MAMP/bin/php/php5.3.6/bin/pear upgrade pear

/Applications/MAMP/bin/php/php5.3.6/bin/pear channel-discover pear.phpunit.de

/Applications/MAMP/bin/php/php5.3.6/bin/pear channel-discover pear.symfony-project.com

/Applications/MAMP/bin/php/php5.3.6/bin/pear install phpunit/PHPUnit 

To test this, you can run the following in Terminal:
/Applications/MAMP/bin/php/php5.3.6/bin/phpunit --version

Caution: before performing the next step, make sure that you won't be conflicting with another installation of PHPUnit!

To run php unit with the command "phpunit", you can make a symlink to the phpunit binary in a directory that is defined in your PATH, such as /usr/local/bin, e.g.:

sudo ln -s /Applications/MAMP/bin/php/php5.3.6/bin/phpunit /usr/local/bin/phpunit

Sunday, 4 March 2012

Change GTK themes in OS X

There are several great, cross-platform programs out there that use GTK to handle their UI elements. Inkscape, and JPilot (for those still rocking their Palm handhelds!), are two examples. In Linux it is possible to customize the themes of these applications to make them look better, and in OS X it turns out this is also true.

While I'll summarize below, a great tutorial on how to do this can be found here:
http://blog.e-shell.org/60

This solution was tested to work in OS X 10.7.3 using MacPorts version 2.0.3.

Summary of Solution:


1) install MacPorts
the project can be found at: http://www.macports.org/

2) install gtk-theme-switch in Terminal
sudo port -v install gtk-theme-switch

3) run the theme switcher app from Terminal, and use it to change themes
switch2

Optional:
4) Install themes 
  • Some great ones can be found at http://gnome-look.org/
  • You can also search MacPorts for themes (try: port search gnome-themes)
(End Summary)

5) Additional notes
Updating themes might break some GTK-based programs. For instance, if Inkscape looks ugly rather than looking like the theme you set, it is because Inkscape only supports variants of the "Clearlooks" theme in OS X (at least for version 0.48). A solution to this would be to launch applications using their own themes, but for now this is out of the scope of this how-to.

To find out a little more on how this theme switch works, (and how to possibly do it manually), you can google up on the configuration file in your home folder named ".gtkrc-2.0".

Incorrect time on received text messages

Ever since Feb 29 (coincidentally), my smartphone had suddenly begun showing weird behavior for received text messages. For instance, if I sent a message at 8pm and immediately got a reply, it would show up as 3pm. Aside from the inconvenience factor, for some phones this can be a problem if "conversation" views are used as replies and sent messages do not appear one after another.

Diagnosis:
It turns out that recently, my service provider had a cell that had its time zone programmed wrong and because my phone was set to automatically update date, time, and time zone from the cell phone company, it updated my phone's time zone to the wrong zone.

Solution:
A quick fix was to manually set my phone's time zone and stop it from automatically updating. This seemed to fix the problem.

Final Thoughts:
This post is not really a definitive solution for all causes of this problem. Rather, it is meant to talk about one possible cause of this problem (and a solution for that cause), just in case someone out there hadn't thought of this cause while trying to debug their own phone or debugging an app for texts.

Saturday, 3 March 2012

jquery .val() after key pressed

When entering data into a text input (for purposes such as auto-suggest), the following example code seems to always be one step behind, in other words .val() is called too early, i.e. seemingly before the keystroke is done:

$("#some-form-text-input").live("keypress", function(e){
  alert($("#some-form-text-input").val());
});


An example of why this is undesirable is: if the text field already contained the string "somethin" and "g" is typed next, the alert in the callback would only display "somethin" versus the desired "something".

Solution: use the "keyup" rather than the "keypress" event

E.g.

$("#some-form-text-input").live("keyup", function(e){
  alert($("#some-form-text-input").val());
});


This waits until the latest pressed key is lifted before firing the callback.

Handling the doctrine-fixtures interface update error

Got the following error after updating doctrine-fixtures:
Fatal error: Declaration of Translator\GFBundle\DataFixtures\ORM\StatementTranslationFixtures::load() must be compatible with that of Doctrine\Common\DataFixtures\FixtureInterface::load()

It turns out that doctrine-fixtures changed their interface for the load function from:
 interface FixtureInterface
    {
        load($manager);
    }


To:
use Doctrine\Common\Persistence\ObjectManager;

    interface FixtureInterface
    {
        load(ObjectManager $manager);
    }



... as seen in the included text file named "UPGRADE".


Solution:


To update your fixture files, simply add the "use" line in your fixtures file, and change all of your load() functions to include the ObjectManager bit.


Example:


... (other use statements) ...
load($manager){
  ... (code to load stuff ) ...
}


To:
... (other use statements) ...
use Doctrine\Common\Persistence\ObjectManager;

load(ObjectManager $manager){
  ... (code to load stuff) ...
}

Symfony 2 Doctrine Fixtures TRUNCATE (rather than DELETE) hack

Symfony2 fixtures clears database tables prior to populating them with fixture data. In the (earlier) version of doctrine-fixtures we tried, tables would refuse to be cleared if there were foreign keys, or indexes would not reset due to using SQL DELETE versus TRUNCATE.

This behavior might be fixed by now, but just in case you have an older version, here's a quick hack around this problem.

Warning: don't do this hack if you're not sure what you're doing! These instructions aren't comprehensive. They're only complete enough for someone to figure out why the changes should work.

1) locate your ORM Purger
it should be in:
vendors/doctrine-fixtures/lib/Doctrine/Common/DataFixtures/Purger/ORMPurger.php

2) make the purge() function truncate
There should be some loop in purge() that looks like:
foreach($orderedTables as $tbl) {
            ... (old code for deleting tables) ...
}


update this to be something like the following, commenting out old code so you can undo if you messed up:
foreach($orderedTables as $tbl) {
  // stupid hack
  $this->em->getConnection()->executeUpdate("SET foreign_key_checks = 0;");
  $this->em->getConnection()->executeUpdate($platform->getTruncateTableSQL($tbl, true));
  $this->em->getConnection()->executeUpdate("SET foreign_key_checks = 1;");
}


The disabling of the foreign key checks is done so the tables can be truncated, otherwise SQL will not allow the truncate operation if your database have foreign key constraints set.


3) how to undo
Un-comment the old code if you commented it out, or reinstall doctrine-fixtures.


4) final thoughts
This isn't the best way around this doctrine-fixtures behavior, and it's only meant as a hack for the impatient.

Friday, 2 March 2012

gedit something in OS X terminal

Trying out OS X after using Ubuntu, I often found myself typing into the terminal "gedit <something>" to do a quick edit of a file, which didn't work out of the box. If you're here and you also didn't want to give up that old habit, here's one way to get that terminal command to work again. (I got this to work with gedit 3.2.6 under OS X 10.7.3, so your mileage might vary with other setups...)

1) install the gedit app
(This can be found at their project page: http://projects.gnome.org/gedit/

Caution: if you don't know what "sudo" is, how to write shell scripts, or what file permissions are, read up on it before doing the next few steps or you could potentially screw up your system.
 
2) create a shell script named 'gedit' in a folder defined in your PATH (in my case, I just put it in /usr/bin), and have it open a file using gedit

In the terminal, create a new shell script named 'gedit' using a text editor. In this example we use the 'nano' text editor.

sudo nano /usr/bin/gedit

Type in the following script:
#!/bin/bash

open -a /Applications/gedit.app/Contents/MacOS/gedit $1


Save.


This lets you do simple "gedit something.txt" or "gedit" commands from the terminal. You can change this script as you desire for more complicated behavior.


3) make the script executable
sudo chmod +x /usr/bin/gedit


To test if this worked out, open up a file using gedit in the terminal, e.g. "gedit myfavoritefile.txt"


4) how to undo this
To undo all of this, simply delete the /usr/bin/gedit script you created and uninstall gedit.

Rookie Blogger+AdSense mistake...

In a bit of a "duh" moment, I learned the hard way that the "Earnings" link, available to new Blogger blogs, shouldn't be used to sign up for AdSense until the blog has some stuff in it. Otherwise your AdSense account will be disallowed due to a "Terms of Service violation": insufficient content!

Not really a "How-To" reminder, but a "How-Not-To" tidbit, in case any other newbies out there notice that link and attempt to try it out too early, just like I did, lol.

Thursday, 1 March 2012

HTTPS with MAMP

If you're here, you probably have an HTTPS local MAMP test site that didn't work "as is". This is how I got it to work with MAMP version 2.0.5 under OS X 10.7.3. It might work with other versions, but no guarantees.

1) backup your MAMP installation
Reminder--it's located at: /Applications/MAMP

2) un-comment the SSL config part of your httpd.conf
Open /Applications/MAMP/conf/apache/httpd.conf

Search for the lines:
# Secure (SSL/TLS) connections
# Include /Applications/MAMP/conf/apache/extra/httpd-ssl.conf

And un-comment the Include statement to make it look like:
# Secure (SSL/TLS) connections
Include /Applications/MAMP/conf/apache/extra/httpd-ssl.conf

Save.

3) point the SSL configuration to the right httpd folder
Open /Applications/MAMP/conf/apache/extra/httpd-ssl.conf

Search for the lines:
#   General setup for the virtual host
DocumentRoot "/Applications/MAMP/Library/htdocs"

Change to whatever folder it should be (note alternative instructions at the end of this section). In my case it was the default, non-ssl MAMP htdocs folder:
DocumentRoot "/Applications/MAMP/htdocs"

Save.

There are other configurations in this file that you might want to consider updating, too, but pointing the htdocs folder to your desired location should be sufficient for a lot of people.

(Alternatively, you can just create a symlink from /Applications/MAMP/Library/htdocs to wherever you want. Google "ln -s" if you're unsure how to do this.)

4) generate SSL cert/key/etc.
In the httpd-ssl.conf, you'll notice that the location for the keys/certs are listed in lines similar to this one: SSLCertificateKeyFile "/Applications/MAMP/conf/apache/server.key"

In Terminal, go to the appropriate folder:
cd /Applications/MAMP/conf/apache

Create a private key, and follow the prompts:
openssl genrsa -des3 -out server.key 1024

Remove password:
cp server.key server-pw.key
openssl rsa -in server-pw.key -out server.key

Create certificate signing request, follow prompts or accept defaults:
openssl req -new -key server.key -out server.csr

Create certificate:
openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

Restart your server, and check your log if there are errors. Your browser will request you to accept your new certificate when trying to view a secure local file for the first time (e.g. https://localhost/)