Saturday, November 28, 2009

Unzipping a file using PHP

The correct way is to use the unzip library but there are various problems with that approach that I won't harp on here. Another correct way is to use

exec("unzip /path/to/file.zip");

but I have found some zip files that the unix unzip command can't handle but that the mac os x built-in archive utility has no problem with. So I came up a simple, but goofy approach:

use php exec() to call an applescript which tells finder to open the file with archive utility, using a keyboard shortcut.

PHP blah.php
exec('osascript /applications/mamp/htdocs/zipfolder/asunzip.scpt');

APPLESCRIPT asunzip.scpt

tell application "Finder"
activate
end tell

tell application "System Events"
tell process "Finder"
key code 125 — down arrow
keystroke "z" using command down
end tell
end tell

KEYBOARD SHORTCUT - application-specific (system preferences)
command z is pulling file -> open with -> archive utility out of finder toolbar

———————————Addendum———————————————————

Here is a better AppleScript that does the same thing. But instead of bringing Finder to the forefront, then using down arrows to navigate to a file, and then using a keyboard shortcut to open it, it just directly opens the file by name quietly in the background:

set filepath to POSIX path of "Macintosh HD:Applications:MAMP:htdocs:zipfolder:neworig.zip"
application
try
set command to "open " & quoted form of filepath
do shell script command
end try

Sunday, November 8, 2009

PHP mail() bug? or feature?

PHP mail bug

I use the php mail() function to create lengthy html emails. The $message is a long string, up to 5000 characters, or more, long. Well, it turns that either the mail() function or the mail server at the other end has a character limit of 990 per string. If you exceed that, something automatically inserts 0D 0A 20 into the string (CR LF Space) every 990 characters, so that now the email conforms to the "rule." No crash, but every 990 characters you see a space inserted in the middle of words, or if the space lands in a tag, you have screwed up your message format.

The fix: create a function that takes your $message string, and inserts 0D 0A 20 more frequently than every 990 characters where there normally is just a space (20). (20 is hex ascii for space - used a hex editor to debug this thing). If your message is html, html ignores CR LF in strings, so inserting this causes no problems. Then, after applying that function, use

mail( $to, $subject, $message, $headers );

on the modified $message, and it goes through without the problem!

Mac Shortcut of the Day
I really like the Safari "Bookmark Bar" across the top of the toolbar for quickly clicking to favorite sites. As if that wasn't good enough already, try holding the command key down while clicking a book mark. This gives you a new tab in your Safari window AND jumps you to your favorite site in one click.

Saturday, November 7, 2009

I am a mac person now

I made the big switch about a month ago and I have never looked back. Virtually everything I used to do using computers I can do better now that I finally have a mac. I have Snow Leopard and haven't come up with any major problems yet. I am printing wirelessly three printers ( direct via bluetooth at home, direct via Wi-Fi (router attached to printer) at work, and via smb to another networked printer at work. The latter two were trickey: direct via Wi-Fi you have to find out the printer's IP address, and to find that out you have to hold down one of the printer's buttons to make it print it's identifying information, including the IP address. For the networked printer, even trickier. You have to find out the URL of the printer on the network and translate it into a format mac understands: in my case it is smb://xxx-03.yyy.phy/HPLaserJ. (That's the networked computer name/PrinterName) I needed to download Snow Leopard drivers for the first two printers; for the third, no driver needed since it is a shared printer.

Other fun things I have learned: Snow Leopard has a built-in VNC client, which means that with the free app VNC Lite I can see and control everything on the mac desktop over the Wi-Fi network on the screen of the iphone. in fact, I just typed this sentence using VNC on the iphone. It is useful if I want to dicate into MacSpeech Dictate using my wearable wireless microphone while in a different room from the Mac. I can still watch the words pop up on the iphone via VNC. Woo.

More great stuff: Mac Mail Rules and Applescript. You can define Rules for mail that act like powerful little macros and the rules can invoke Applescript scripts. Since mobile me email is "push", I can write php scripts on my MAMP server, and run the scripts remotely using the iphone, which results in pushing commands to the mac via mobile me -> via rules -> via applescript. In this manner I can print customized documents to a crisp laser printer right off of my iphone, and I can alter the content of document by entering the data on the phone via MAMP, and the php script handles the formatting (because it's html email with formatting tags). Two big helps on this: (1) Get rid of the unwanted headers (Mail->Preferences->Viewing->Show Header Details->None. (2) Rules run on the mac BEFORE the mail message that triggers the rule is delivered. So send a second mail message with the actual command in the subject line AFTER the HTML message is received (in php: sleep(1); makes the script wait just long enough to work) .

I'll mention a few more things in less detail: Finder -> Command-K is similar to Windows Key-R for connecting to servers: but instead of \\servername use smb://servername. When adding printers, remember to click the plus sign and then control-click the menu bar to get the option to customize the menu bar by adding the "Settings" icon. Built-in Automator macro app. Publishing your iCal calendar on-line for integration into your family's various calendars on their iCals (and iphones via mobile me). Time machine. Backup. The keyboard shortcuts under System Preferences -> Language and Text AND System Preferences -> Keyboard.

In short, it's a whole new world and a better one. Wow.