Tech Blog :: mac

Aug 10 '11 4:06pm

Mac shell trick: have Growl notify you when something happens

Let's say you're waiting for some event to happen, and that event is detectable in your unix terminal. Examples: a DNS record to propagate (use ping and grep), a blog post to appear in an aggregator (curl and grep), etc. Instead of checking it every minute, you can have your terminal run a loop until it finds the condition you've set, and when it does, it'll notify you with Growl.

I'll use the DNS-test example here:

To check if is mapped to, we can run,
ping -c1 | grep ""; echo $?

Or to check for the appearance of some HTML on a web page, we can do,
curl -s | grep "some content"; echo $?

The $? at the end checks the exit code - 0 being success, non-0 being error.

Now put that in a loop, running every 30 seconds, with a growl popup when it succeeds:

while true; do
  FOUND=`ping -c1 | grep ""; echo $?`; 
  if [[ "$FOUND" -eq "0" ]]; then growlnotify -t "Alert" -m "FOUND" -s; break; fi; 
  sleep 30; 

Or in one line,
while true; do FOUND=`ping -c1 | grep ""; echo $?`; if [[ "$FOUND" -eq "0" ]]; then growlnotify -t "Alert" -m "FOUND" -s; break; fi; sleep 30; done

Feb 26 '11 1:24pm

Mac tip: pipe text from the terminal to the clipboard

Via Fantastip (from a Google search): if you're in the Mac terminal and want to pipe output to the OSX clipboard, use pbcopy, e.g.
echo "copyme" | pbcopy

Jul 10 '10 12:00am

Learning AppleScript: Creating a "Do Not Disturb" Toggle

On a whim I decided to start learning AppleScript tonight. AppleScript is one of the great features of OSX, and allows scripts to communicate pretty much with any [decently built] application.

The first use case I could think of was a little toggle for quiet time, a "Do Not Disturb" toggle. It has 2 modes, Quiet and Noisy. In Quiet mode it puts Mailplane into its own "Do Not Disturb" mode, logs out of iChat, closes Skype, turns off Growl, and turns on Caffeine (assuming this quiet time also involves concentrated looking at the screen). Noisy mode is normal; flipping that switch logs back into iChat, and turns Mailplane and Growl back on. (It doesn't toggle all the same things; I don't always have Skype on and Caffeine shouldn't necessarily be turned off. More on that below.)

Here's the code:

-- figure out: how can it REMEMBER from one run to the next (e.g. were caffeine, ichat on?)
set appName to "DoNotDisturb Toggle"  -- for display later
-- register with Growl
tell application "GrowlHelperApp"
	set the allNotificationsList to {"toggle-announce"}
	copy allNotificationsList to enabledNotificationsList
	register as application ¬
		appName all notifications allNotificationsList ¬
		default notifications enabledNotificationsList
end tell
display dialog "Noisy or Quiet?" buttons {"Noisy", "Silent"}
set doMode to button returned of result
if doMode is "Noisy" then
	tell application "Mailplane" to set doNotDisturb to false
	tell application "GrowlHelperApp"
		notify with name "toggle-announce" title appName ¬
			description "Going back to Noisy mode" application name appName
	end tell
	tell application "iChat" to log in # what if it wasn't before?
else -- Quiet
	tell application "GrowlHelperApp"
		notify with name "toggle-announce" title appName ¬
			description "Going into Quiet mode" application name appName
	end tell
	tell application "Mailplane" to set doNotDisturb to true
	tell application "Caffeine" to turn on # (turn on for quiet but don't turn off on noisy)
	tell application "iChat" to log out
	tell application "Skype" to quit
end if

I turned it into an Application (File->Save As), so it's easy to run from the Dock.

What I need to figure out is how it can remember states from one run to the other: it should only log back into iChat, for instance, if it was logged in before going into Quiet mode. Does anyone know?

Jun 27 '10 11:05pm

Debugging PHP with XAMPP, MacGDPp, and Textmate

Technosophos has a great tutorial on setting up a PHP debugging environment with XDebug and MAMP. I'm using XAMPP and it works the same way, just change the path where goes.

However, the Textmate part - using the xdebug.file_link_format parameter - doesn't seem to be working. Apparently others are having the same problem, possibly Snow Leopard-related, not sure if there's a solution. It's not necessary for the debugger to work, however, just a convenient way to view the error-causing code.

Apr 16 '10 7:19am

Sign PDFs on a Mac: Formulate Pro

I needed to sign a PDF on my mac, and didn't want to waste the paper and ink to print, scan, and shred it if I didn't have to. So I found this open-source app FormulatePro, which lets you add text and images to PDFs.
It's a little clunky: Images aren't in the toolbar, they're added with File -> Place Image. And it doesn't save to PDF, it just uses OSX's built-in "Print to PDF" to export. But for basic form filling/signing, it does the job for free.

Apr 7 '10 9:20pm

Mouse and Keyboard Sharing on Mac with SynergyKM

I bought a new MacBook Pro recently as I transition into full-time freelance work, but I still need to work with my employer-provided (older) MacBook for the next few weeks. There's a great tool for helping with that scenario called Synergy, which is a [GUI-less] tool for sharing a keyboard, mouse, and clipboard across multiple computers (running multiple OS's), letting the mouse move seamlessly from one computer to the other as if it were another monitor.

There are a number of UI's to simplify Synergy's setup. For the Mac I found QuickSynergy (also works with Linux). But I couldn't get QuickSynergy to work. So I tried SynergyKM instead, and that works like a charm. (Set the computer with the mouse/keyboard you want to use as the "server" and the other(s) as clients.) I did have to turn off "Enable Bonjour" in the config panel (it's in System Preferances, not Applications) to get it to work, however - but oddly, "Enable Bonjour" needs to be on in the Tray's SynergyKM icon. (I'm not sure why they're separate, but with one off and the other on, same on each computer, they connected instantly.) SynergyKM is only for Mac, though, but there are other Linux GUIs for Synergy (I tried one once on Ubuntu with mixed results).

Nov 30 '09 12:43pm

Trouble with the OS X 10.6.2 update

I just updated my Leopard Mac with the latest 10.6.2 update, to support a new Apple "Magic Mouse" I got for the office. The mouse is great, but my terminal's going haywire - the terminal's "login" process owned by root keeps crashing, apparently whenever I run a sudo command in the terminal. I can't force-quit it or reboot, only holding down the power button to shutdown handles it. I'll post an update if I figure it out.