Windows batch files (.bat) can be scripted, which means you can do all sorts of things like setting variables. But if you’re having a hard time getting those variables to set, this might be why.

SET var=Holy crap, it sets the variable!

Will work fine, but…

SET var = Holy crap, the space screws it up!

… will not. If you’re used to putting spaces after equals signs like I am, you can make this mistake without even knowing it. If you do it wrong, it won’t throw any errors either. Just silently fails.


So you have a date from your database and it looks like this: 2014-01-31 01:02:03. It so happens that your server is on UTC time. You want to:

  1. Format the date into something prettier, and
  2. Offset the date so it’s adjusted for a user’s local time zone.

Ok, great. You only have to SUFFER THE PAIN OF A THOUSAND MIGRAINES AND oh wait it’s actually sort of easy. But man, this took a lot of pain and searching to find. The key is moment.js, a glorious gift to developer-kind.

So the way I’m doing this is to create an HTML5 <time> element, but it can certainly be done other ways. First, after running a DB query, I have PHP produce this:

<time datetime="2014-01-03 12:57:03">
    2014-01-03 12:57:03
</time>

Not very pleasant to look at. So then we do a little moment.js…

var date = $(time).attr('datetime');
var newformat = 'D MMM YYYY [at] h:mm A';
var nice = moment(date, 'YYYY-MM-DD HH:mm:ss').format(new_format);
$('time').html(nice);

There’s some jQuery in there, too, but it isn’t necessary.

What we just did, is we read the date from our <time> element’s datetime attribute, then told moment what format it was in, so it could then spit it back out how we want it to, using .format(), as the human-readable part of the <time> element.

The result looks like this: 3 Jan 2014 at 12:57 PM. Yay. It’s nicer. But that’s still UTC time, and no timezone data was saved in our DB. So we have to add it.

<time datetime="2014-01-03 12:57:03 +0000">
    2014-01-03 12:57:03 UTC
</time>

Note that the stuff added here didn’t come from the DB, I added them manually. The “+0000”, or an offset of 0 hours for UTC time, is what we’re giving to moment.js. The “UTC” is just there for humans to read, just in case something goes wrong or JavaScript is disabled. With those added, we inform moment.js that there’s a timezone offset in there:

var nice = moment(date,'YYYY-MM-DD HH:mm:ss ZZ').format(newformat);

See the “ZZ” we added in the middle? Now moment.js knows we have a timezone and what it is. And just like that, it’s been adjusted: 2 Jan 2014 at 5:57 PM (adjusted for America/Denver, -7:00).

It was so easy, that after all the suffering gone through before this point, I’m almost mad. But not really. But just a little bit.


Warning: This post assumes you’re already familiar with IPN, and you’re just having trouble, well, troubleshooting it.

Okay, let’s say you’re setting yourself up with PayPal IPN. You’ve set up a PHP listener not unlike this one. You’re all set, right? Ready to test it out with PayPay’s IPN Simulator! Yeah!

Except it fails. It always fails. Why?

To try and figure it out, I set up a file named paypal.txt and told my listener to write the results to that file. Like so:

// These two lines are just landmarks so you can see where I am:
while (!feof($fp)) {
   $response = fgets ($fp, 1024);

   // Here's the actual logging code:
   $file = 'paypal.txt';
   $current = file_get_contents($file);
   $current .= PHP_EOL."DAMN !fp";
   file_put_contents($file, $current);

Okay? And after hours of troubleshooting, this is the only thing I would get:

HTTP/1.0 400 Bad Request
Server: BigIP
Connection: close
Content-Length: 19

Invalid Host header

Nice, huh? Super helpful. Well, as it turns out, the problem is that the example code I linked to above, and variations of it I’ve found all over the webs only work for live IPN responses. Meaning anything from www.sandbox.paypal.com (like PayPal itself tells you to use) will fail. EVEN IF YOU CHANGED IT to use Sandbox like so: (This is line 15 of the above-linked example code)

$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30);

The problem

As near as I can tell, the problem is that the IPN responses are being sent from paypal.com, even though you want</> to — have been told by PayPal itself — to work with sandbox.paypal.com while developing.

The fix

Here it is. See line 2.

$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($postback) . "\r\n\r\n";

Done. Problem should be solved. To sum up, it’s not enough simply to set the fsockopen() to use sandbox.paypal.com, you also need to set the response header correctly, or there will be a mismatch and sad, sad failure.

Hope this helps!


So you want to do something on a mapped network drive using PHP, but it simply tells you it couldn’t find the drive? Worry not.

What’s going wrong?

The problem is that PHP runs under the SYSTEM account, and the SYSTEM account can’t access mapped drives.

The solution:

So, we need to map the drive from the SYSTEM account. You can do this from your PHP script, but if you need persistent access, we can do this:

1. Download the Windows Sysinternals Suite and unzip it somewhere you can easily get to from a command prompt.

2. Open an elevated command prompt. (Search for “cmd.exe” from the Start ball, and then right-click on it, and choose Run as Administrator.)

3. Using the command prompt, navigate to wherever you put Sysinternals.

4. Elevate yourself once again to supreme power by using:

psexec -i -s cmd.exe

A new command prompt window will open that is running as the SYSTEM account.

5. Map the network drive:

net use z: \\[IP ADDRESS HERE]\[FOLDER NAME HERE] /persistent:yes

And you’re done!

Warnings:

You can only remove this mapping the same way you created it, from the SYSTEM account. If you need to remove it, follow steps 1 -4 but change the command on step 5 to: net use z: /delete.

The newly created mapped drive will now appear for ALL users of this system but they will see it displayed as “Disconnected Network Drive (W:)”. Don’t worry though! It displays as disconnected, but will work for any user.


iStock_000003225418XSmall

And FROM_UNIXTIME(), too!

Maybe you’re like me, and you’re migrating something from MySQL over to PostgreSQL. Maybe, like me, you’re swearing a great deal and experiencing high blood pressure, too.
(Or maybe not.)

I’ve seen numerous threads that just tell you how you can change all your code to use Postgres’s epoch_something_something_aint_nobody_got_time_fo_dat() function instead, but epic hero Janusz Slota has a better way. He shows you how to, rather easily, make it possible to run those functions in PG without having to change a thing.

Check out his solution right on over here.

(Just change LANGUAGE ‘SQL’ to LANGUAGE ‘sql’ if you’re using PG 9.2+)


Skipping tracks with repeat on? Preposterous!In iTunes 10, you could skip to the next/previous track when Repeat One was turned on. In 11 & 12, they assume that by “skip ahead”, you somehow mean “rewind this track”.

This really bothers some people.
Do not judge us, we have our reasons!

And here’s one way to fix it.

It took some work, but I finally came up with some AppleScript to handle this. Basically, we’re checking to see if Repeat One is on. If it is, we quickly disable it, skip to the next (or previous) track, and then turn it back on. Apple borked the old way of doing this (same with shuffle), so we’re using menu bar items instead.

-- This script lets you skip songs in iTunes 11/12 even if repeat one is on

tell application "System Events"
	tell process "iTunes"

		-- Find out if repeat one is on
		-- This finds out if the menu item is checked
		set isRepeatOneOn to (value of attribute "AXMenuItemMarkChar" of menu item 3 of menu 1 of menu item "Repeat" of menu 1 of menu bar item "Controls" of menu bar 1 as string) ≠ ""

		if isRepeatOneOn is true then

			-- Set repeat to ALL
			click menu item 2 of menu 1 of menu item "Repeat" of menu 1 of menu bar item "Controls" of menu bar 1

			-- Skip to previous track...
			click menu item "Previous" of menu 1 of menu bar item "Controls" of menu bar 1

			-- Need this, or the next step happens too fast
			delay 0.1

			-- Reactivate Repeat One
			click menu item 3 of menu 1 of menu item "Repeat" of menu 1 of menu bar item "Controls" of menu bar 1

		else
			-- Just skip to previous track
			click menu item "Previous" of menu 1 of menu bar item "Controls" of menu bar 1
		end if

	end tell
end tell

You can copy/paste that, or you can just download the script files here.
There’s one script for skip ahead, and one for skip back.

Wait—how do I use these?

Valid question. They’re not all that useful, really—unless you assign them to hotkeys.

Head on over here to Mac OS X Tips for ways and examples to set hotkeys to run AppleScripts.

Personally, I used Quicksilver to control iTunes for years, but now I use Alfred 2 (with Powerpack.)

(Since it’s not super obvious, let me know in the comments if you’d like help setting up either of these scripts using Alfred 2.)

 



iTunes 11 is out, and most people seem to think it’s great. You and I are different, however. We hate it, and we have our reasons. (Mine happens to be the inability to skip tracks while Repeat One is on. Yup, deal-breaker for me. UPDATE: Fixed that!) So let’s make things right again.

First, you’ll need a backup of your iTunes Library.itl file, found under ~/Music/iTunes. Fortunately, I backed my library up right before installing iTunes 11. Note that any new songs, apps, etc. that you may have added since installing 11 will need to be replaced. In my case, I used the “Date Added” feature in iTunes to find which files I had added since November 28, and copied those out into a separate folder. When the downgrade was complete, I simply copied them back in.

Let’s get started:

1. Back up your iTunes Library.itl file, found under ~/Music/iTunes

2. Now you need an iTunes 10.7 dmg file. Download it from Apple here.

3. Delete iTunes 11. There are different ways to do this. One is to use an app called AppZapper. The method that worked for me was this: Open Terminal.app and run these commands, one at a time:

killall iTunes
killall "iTunes Helper"
sudo rm -rf /Applications/iTunes.app/

That last one will need your password and will probably take a minute or so.

4. Now we have to reinstall iTunes 10.7 using an app called Pacifist (shareware, free). Download Pacifist and run it. (Mavericks: download Pacifist from the link at the bottom of the post instead.)

5. Choose the Open Package option. Browse to the iTunes 10.7 dmg file.

6. You’ll get a list of files. Select “Contents of Install iTunes.pkg”, and from the top left corner of the app, choose Install.

7. Be careful here! Every time Pacifist tells you a file already exists, make sure you check the “always” box and choose Replace (not update). This should happen around three or four times.

8. You’re almost done. Before running iTunes again, make sure you have recovered your “iTunes Library.itl” from a pre-iTunes-11 backup. After that, you should be good to go. But as ever, your mileage may vary.

Mavericks (10.9) Update

I was dreading Mavericks because it automatically updates iTunes to 11, and this is frankly unacceptable for some. Thank goodness this method worked like a charm this morning! I had 10.7 back in about 5 minutes.

There was only one hiccup: Use this version of Pacifist (3.0.10)! For some reason, the latest version (3.2 as of this writing) would not install the package.

Also, if you just installed Mavericks, don’t open iTunes! If you do, it’ll update your iTunes library files, and you’ll have to restore your old ones from a backup after you install iTunes 10.7. But if you never open iTunes 11, they won’t be changed, and you won’t have to restore a thing.

*Mavericks Update #2

So it appears that after downgrading to iTunes 10.7, the Mac App Store may become borked in the process — apps will neither update nor download from it. I’m not sure if this is just because Mavericks is fresh and will get updated by Apple or what. I use the Mac App Store just about never, so I don’t really care, but you might! More details as they become available.

If you’ve already installed 10.7 and want the app store back, installing iTunes 11 again fixes the problem.


The full quote comes from Sebastian Thrun, the tenured Stanford Professor who left in order to begin exploring new teaching methods online.

During the era when universities were born, “the lecture was the most effective way to convey information. We had the industrialization, we had the invention of celluloid, of digital media, and, miraculously, professors today teach exactly the same way they taught a thousand years ago.”

You can find the full story at The Chronicle of Higher Education.

 


Threw this together on Thanksgiving. Because it’s true.

Feel free to take and use. (Click for bigger version.)

Blog Pages