Launch Google Chrome Incognito from the terminal or a shortcut in OSX

Problem: Ok not really a problem, but I want to be able to launch Google Chrome straight to Incognito mode from a shortcut. This is useful when logging into multiple bank accounts, Google Apps accounts, or testing session based websites. etc etc etc.

Fix: Thankfully the Google Chrome.app can be launched with the –incognito switch to do just that. The terminal command to do this, assuming the browser is sitting in /Applications/ is this:

open -a /Applications/Google Chrome.app --args --incognito

Note: –args has to be passed to satisfy the ‘open’ command’s arguments first.

Launching it from a shortcut: If you want to create a shortcut to do this, open AppleScript Editor and enter the following:

tell application "Terminal"
	activate
	do script "open -a /Applications/Google\ Chrome.app --args --incognito;"
	delay 1
	quit
end tell

Note: The space in the Google Chrome.app name must be double-escaped with two backslashes like that to work. Also, without the delay I found that the script exits too quickly or something like that, and it doesn’t work.

Save the above script as an Application, and call it something like ‘Incognito’. Running this app will launch Terminal, Chrome Incognito, then exit Terminal. Bingo!

Giving your Incognito app an Icon: I’ve added the app to my Dock, and given it a special icon (see below for a downloadable PNG icon). To do this:

  • Open the image you want to use as the icon – it should be a 512×512 24bit PNG if possible – and copy the image to the clipboard. If you are using Preview to view the image, do cmd+a to select all, then cmd+c to copy it… this works for most other graphics packages also.
  • Locate the app you created above in Finder, then press cmd+i to bring up the info window (alternatively right-click the app then select ‘get info’).
  • In the resulting pop-up, click on the icon at the top to highlight it like so (notice the blue halo around it):
    Original Script Icon
  • Then use cmd+v to paste the new icon from the clopboard into the icon area and it should look like this:
    New Incognito icon
  • Close the info window, and it’s done. You should now have a nice looking shortcut that opens Chrome Incognito with one or two clicks!

Feel free to use this icon. It’s just the standard one with some ‘colour replace’ work to make it blue. You could paste some tacky sun glasses over it if you wished 🙂

Reboot Mac OS 10.* to BOOTCAMP from a desktop icon

Problem: I want a quick way to restart my macbook and boot to the Bootcamp partition with one or two clicks, for the next boot only. The two existing ways are:

  • Restart OS X then hold the option key after the startup sound to bring up the boot options. This can be a pain if you miss the sound, or if you want to initiate a reboot and then bog off and make a cup of tea.
  • Go to System Preferences > Startup Disk > select the Bootcamp partition > Click Restart. This sets the default boot disk, so needs to be un-set next time you want to boot back into OS X.

Solution: Mac OS can be rebooted with the shutdown command, and boot partition can be set with the bless command. like so:

sudo bless -mount "/Volumes/BOOTCAMP" -legacy -setBoot -nextonly; sudo shutdown -r now

(Assuming your bootcamp partition exists at /Volumes/BOOTCAMP)

As is probably obvious from the command, the -nextonly part means that the boot partition is being changed for the next boot only. The -r part of the shutdown command means it will reboot again, and the ‘now’ command means just that.. do it right NOW.

Running from an icon: The command above can be run with a small script like so:

  • Run AppleScript Editor.
  • type the following into the editor window:
tell application "Terminal"
	do script "sudo bless -mount "/Volumes/BOOTCAMP" -legacy -setBoot -nextonly;sudo shutdown -r now"
end tell
  • Save this script as an ‘Application’ to your desktop and give it an obvious name like ‘Reboot to Windows’.
  • Double-click the icon and Terminal should run, asking you for a password. Much quicker 🙂 (if you want to cancel the process, use CTRL+C to exit the script as you usually would)
Any comments or improvements? Please leave them below!

Convert eregi_replace to preg_replace in old class.phpmailer.php scripts

Problem: I have lots of legacy php code on old sites that uses eregi_replace to format up an HTML email body. As eregi_replace is now depreciated it can be replaced with preg_replace. This is tricky sometimes because the formatting is quite different.

Fix: Here’s a common line from class.phpmailer.php scripts:

$emailBody = eregi_replace("[]",'',$emailBody);

To convert that you’ll need to do this:

$emailBody = preg_replace("/\\/", '', $emailBody);

Wondering why there are so MANY escaping backslashes there? I asked that question and got this answer:

“..the backslash needs to be escaped once for the php string’s benefit, then again for the interpretation of the regular expression engine, as escaped characters like d indicate a digit in regular expressions. So a pattern of \d would match a digit, but \\d would match a backslash then a d character. PHP strings are lenient on backslashing when it isn’t necessary, so:
– setting a string to “d” will give it a value of d (the same as setting it to “\d”).
– but setting it to “”” will give a value of “.
– and setting it to “” will return a syntax error.

– PHP double quoted strings will consume the (first) backslash for \ n t r and “.
– PHP single quoted strings will only consume the first backslash for \ and ‘ “

There.. clear as mud!

WordPress replaces double and triple hyphens (– or —)

Problem: The other day I was trying to type two hypens (–) in a post, and when I saved/published the post, WordPress reformatted the hypens to display as an ‘en dash‘ (–).

More Info: After some searching about, and some helpful info provided in the WordPress Codex, I found out about a WP function called ‘wptexturize’ which is applied each time a post is rendered. The function – which can be found in ‘wp-includes/formatting.php’ – replaces quite a few things, like ™ to ™ etc.

Fix: You could stop this happening a number of ways i.e. editing the function to stop it doing the replace altogether, or install a plugin that does it (maybe this one, haven’t tried it!). I decided to use WordPress’s remove_filter function to stop the reformatting taking place.

To do this, open the ‘functions.php’ file within your theme. If one does not exist, create it.

Add the following code to the bottom of the file, taking care not to end up with too many or too few <?php tags:

<?php
remove_filter('the_content', 'wptexturize');
remove_filter('the_title', 'wptexturize');
?>

Then save the file (or ‘update’ if you’re doing this via the theme editor in WP admin) and view your post. That should do it! (proof is here, you can only see those double and triple hyphens in this blog post title because I’ve just added the code above to this sites functions.php also!)

Batch rename file extensions from .jpg to .png in terminal

Problem: A client sent me 164 images with incorrect extensions on them. I needed to change them all from .jpg to .png.

Fix: Open up terminal, navigate to the directory containing all the images and type:

ls *.jpg | awk '{print("mv "$1" "$1)}' | sed 's/jpg/png/2' | /bin/sh

..that should do it! This one-liner makes use of sed and awk unix utilities.

Add ‘Drafts’ sorting to Shopp Products list

Problem: There is no ‘drafts’ view filter in the Shopp (>1.1.9) Product Manager, i.e you can’t quickly list all the products that have ‘draft’ status in one go.

Fix: I made this mod to answer a question in the Shopp Helpdesk forum, it’s not very tested, but seems to work perfectly!

Open ‘wp-content/plugins/shopp/core/flow/Warehouse.php’ and replace this:

198
$subfilters = array('f' => 'featured','p' => 'published','s' => 'onsale','i' => 'inventory');

With this:

198
$subfilters = array('f' => 'featured','p' => 'published','d' => 'draft','s' => 'onsale','i' => 'inventory');

Then in the same file replace this:

199
200
201
202
203
204
205
$subs = array(
'all' => array('label' => __('All','Shopp'),'columns' => "count(distinct pd.id) AS total",'where'=>'true'),
'published' => array('label' => __('Published','Shopp'),'total' => 0,'columns' => "count(distinct pd.id) AS total",'where'=>"pd.status='publish'",'request' => 'p'),
'onsale' => array('label' => __('On Sale','Shopp'),'total' => 0,'columns' => "count(distinct pd.id) AS total",'where'=>"pt.sale='on'",'request' => 's'),
'featured' => array('label' => __('Featured','Shopp'),'total' => 0,'columns' => "count(distinct pd.id) AS total",'where'=>"pd.featured='on'",'request' => 'f'),
'inventory' => array('label' => __('Inventory','Shopp'),'total' => 0,'columns' => "count(distinct pt.id) AS total",'where'=>"pt.inventory='on' AND pt.type!='N/A'",'grouping'=>'pt.id','request' => 'i')
);

With this:

199
200
201
202
203
204
205
206
$subs = array(
'all' => array('label' => __('All','Shopp'),'columns' => "count(distinct pd.id) AS total",'where'=>'true'),
'published' => array('label' => __('Published','Shopp'),'total' => 0,'columns' => "count(distinct pd.id) AS total",'where'=>"pd.status='publish'",'request' => 'p'),
'draft' => array('label' => __('Draft','Shopp'),'total' => 0,'columns' => "count(distinct pd.id) AS total",'where'=>"pd.status='draft'",'request' => 'd'),
'onsale' => array('label' => __('On Sale','Shopp'),'total' => 0,'columns' => "count(distinct pd.id) AS total",'where'=>"pt.sale='on'",'request' => 's'),
'featured' => array('label' => __('Featured','Shopp'),'total' => 0,'columns' => "count(distinct pd.id) AS total",'where'=>"pd.featured='on'",'request' => 'f'),
'inventory' => array('label' => __('Inventory','Shopp'),'total' => 0,'columns' => "count(distinct pt.id) AS total",'where'=>"pt.inventory='on' AND pt.type!='N/A'",'grouping'=>'pt.id','request' => 'i')
);

Hopefully this will be included in a future version of Shopp.

Swap days and months in PHP – quick and dirty MM/DD/YY to DD/MM/YY

Problem

I have a ton of date strings in the stupid US style dd/mm/yy format. i.e. 12/24/09 being 24th September 2009. I need to convert them to the UK style dd/mm/yy.

Solution 1

As a string in PHP is really just an array of characters, the simplest fix was to address the individual number characters by their positions within the sting, and rearrange them into the format I wanted:

$d = "12/24/09";
$d = $d[3].$d[4]."/".$d[0].$d[1]."/".$d[6].$d[7];
echo $d; // displays "24/12/19".

Solution 2

I posted my solution on twitter (@hutchings) and asked for comments and @londonhackspace organiser Jonty came back with the following elegant (and much cleverer) suggestion:

$d = "12/24/09";
$d = preg_replace('|(d+)/(d+)/(d+)|', '$2/$1/$3', $d);
echo $d; // displays "24/12/19".

So provided you know that your original string is in US format, both these solutions can be used. In my testing I found solution 1 executed more quickly, but isn’t as pleasing to look at 🙂 Cheers Jonty.

Cannot set alpha on dynamic text in Flash / ActionScript 2

Problem

I’m doing some freelance Flash development, and it’s been a while. I have just created a dynamic text field that I wanted to fill via actionscript, then fade in via an alpha tween. This wasn’t working, even if I set _alpha = 0 on the movieclip containing the text field.

Solution

You can only set _alpha on a Dynamic Text field if the font has been embeded (with the embed button).

Works now! wohoo! Simple stuff that I’ve known in the past, but forgot.