Variables not setting in Windows batch file?

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.

Using Sandbox always fails with PayPal IPN (PHP)

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!

Access a mapped network drive from PHP (Windows)

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.

Make MySQL’s UNIX_TIMESTAMP() work in PostgreSQL

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+)

iTunes 11 and 12: Enable track skipping with Repeat One turned on (OSX)

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.)

 

How to downgrade to iTunes 10.7 from iTunes 11 [OSX – Even Mavericks!*]

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.

Setting up the Actiontec GT784WN for Qwest/CenturyLink

The Actiontec GT784WN is a pretty nice DSL modem/wireless N router. For most of you, it will work right out of the box — even auto-detecting all your settings.

If you’re on Qwest/CenturyLink, not so much. In fact, they’ll try to tell you that the modem isn’t even compatible. That isn’t quite true — it is compatible, it simply isn’t supported, meaning their customer service has no training for this model. And that’s okay!

But you got your Actiontec and you want Internets. Well, we may be able to help you with that! I’m going to repeat some of the normal setup steps here. If you know what you’re doing, skip to #7.

  1. Just do like the Quick Start Guide says, plug it into the phone line, then into the wall, then wait for the DSL light to turn solid.
  2. Connect to it via the Ethernet cable and use your browser to head on over to http://192.168.0.1.
  3. Skip the Quick Setup and hit the Advanced Setup link. Now find WAN IP Settings on the left.
  4. Set the ISP Protocol to PPPoE.
  5. Under #3, enter your Qwest/CenturyLink username and password. (The username will be some kind of email address. I got these directly from Qwest when I signed up.)
  6. Mash Apply at the bottom of the screen.
  7. Now, find Broadband Settings on the left menu. It can be easy to pass up because it looks like a section heading more than a link.
  8. This part is a little tricky. It changes depending on region. If you used to be in the Qwest service area before CenturyLink merged with them, set VCI to 32. If you used to be Embarq, try VPI 0, VCI = 32. If you used to be CenturyTel, try VPI = 0VCI = 35.

UPDATE: When I originally posted this, it was for the Qwest service area. CenturyLink has merged with a lot of companies since then, and other smaller areas may have different connection settings. So these settings may not apply to you!

Your mileage may vary! And good luck.

(Thanks, commenter Curtis for the regional settings update.)

IF YOU LIVE IN FLORIDA: Commenter Just Joe has supplied the settings for your area. Go to the comment. Thanks, Just Joe.

 

Delete all hidden Mac junk files in Windows (Like .DS_STORE)

If you use a Mac and Windows together, you’re likely to end up with a bunch of hidden .DS_STORE files all over your Windows drives. You can easily search and destroy them all using this command:

(Open the command line first of course. You can do this by mashing Win + R, then typing “cmd”. Or just type “cmd” in the Start Menu search box.)

del /s /q /f /a .DS_STORE

That will find every instance of this Mac resource file and delete it. Good times. But wait there’s more. In addition to DS_STORE, OSX will also put a bunch of other junk every where starting with “._”. Kill those like this.

del /s /q /f /a ._.*

(Why not use “._*'” instead? Apparently, you can sweep up legit files from other things such as Chrome by doing that. Thanks commenters.)

 

Important note! This will only search inside the folder you’re in, as well as every folder below that. So, if you wanted to search and clean an entire drive, make sure you’re in the root folder. Get there with this:

cd \

You could also put all of this into a .bat file for great automation.

Pro tip: copy-pasting into your command window

Windows 10 will let you CTRL-V paste into the command line. Friggin sweet. But did you know you can do it in other versions of Windows too? Just right-click on the command window and click Paste. That will save you a little work.

Xfce4 Power Manager Icon Missing?

Recently, I installed Lubuntu 11.10 (a netbook-optimized version of Ubuntu) on my wife’s netbook at Lifehacker’s suggestion. And wow–did it make an incredible difference. It’s like a brand new machine compared to Windows 7 running on it, which was getting unbearably slow.

While Lubuntu will look and act pretty familiar to anyone born and raised on Windows, I wanted to make sure when I handed the netbook back to her, she was as comfortable as possible. That meant configuring, among other things, the power management. (Lubuntu wasn’t putting the machine to sleep when the lid was closed by default.)

First, I had to make the power manager run on startup. To do this, you go to:

Menu (think Start Menu) > System Tools > Desktop Session Settings.

Here, you’ll see a number of items. Power Manager should be pretty close to the top. Check the Enabled box, hit OK, then restart.

Now you’ll see a familiar battery icon in the system tray. Yay! Right-click on that to find its preferences, like how long until the computer sleeps, what happens when power buttons are pressed and when the lid is closed, etc.

Back to that missing icon…

Anyhow, during my experiments, I accidentally set the power manager’s system tray icon to “Never show icon”. And there was no link to it under the System Tools or Preferences Panel. How the heck was I supposed to get it back? After too much searching, I finally discovered you can run the settings panel from the command line. Open up the terminal (in Lubuntu it’s under Menu > Accessories > LXTerminal). Now type this in and mash enter:

xfce4-power-manager-settings

The settings panel will appear. Breathe sigh of relief and continue.

Update:

Commenter Marc shares how to make the power manager’s settings panel appear in the Menu (Start Menu-esque Thing).

If you want to find an entry in the menu, you’ll have to edit /usr/share/applications/xfce4-power-manager-settings.desktop. Change:

OnlyShowIn=XFCE;

to

OnlyShowIn=XFCE;LXDE;

Add spacers to your Mac’s dock

Update: This works all the way up to 10.10 Yosemite.

Ever wanted to have some more organization on your dock? I like to keep things separated more or less by group: everyday things, development tools, and apps that just happen to be open but aren’t permanent fixtures on my dock. I also like to keep my documents and folders separated from minimized things and the trash on the right side.

A-like so

It’s fairly simple to drop some spacers into your dock. And yes, it uses Terminal, but don’t get squeamish. Just copy and paste the lines you see below. To add spacers to the left side, open up Terminal and paste this:

defaults write com.apple.dock persistent-apps -array-add '{tile-data={}; tile-type="spacer-tile";}'

Now mash Enter.

And now you’re wondering why nothing happened. We have to reset the dock to make the space appear. Now paste this:

killall Dock

And mash Enter again. Your dock will vanish and reappear with a space at the end of the left side. You can drag it around (or even off) just like any other icon. Repeat as desired for more spacers!

And for the right side…

Paste this, followed by the usual mashing of Enter afterwards.

defaults write com.apple.dock persistent-others -array-add '{tile-data={}; tile-type="spacer-tile";}'

Follow it up with the usual:

killall Dock

Now speak aloud the word “booyah” or other popular slang term designating success.

Note: I’ve confirmed this also works in OSX 10.7 Lion