Jul 22 2008

Using PHP and a Shell Script to Restart an AppleTV remotely — via the web

Published by Jonathan Wise under Hacks

As a follow up to my previous post, here’s how to do the same thing, but from a web browser (say, on your iPhone) — it assumes you’ve already written a shell script, as discussed earlier.

  • Configure Apache to run PHP on your Mac (these instructions are for Leopard, but similar ones are out there for Tiger)
  • Configure Apache to run as your own user, instead of the WWW user — a serious security risk. Only do this on an internal network! The WWW user won’t have adequate permissions to do what we need to.
  • Put this .php page in your /Library/WebServer/Documents/ folder as RestartMedia.php — you’ll need to supply your own image file in the same path as well.
  • Visit http://yourcomputer.local/RestartMedia.php from a browser. Clicking on the text or image will execute a shell script located at: /Applications/Automation/RebootMediaServers.sh

Obviously you can customize this at will, including file locations. This was just a quick hack that does the job for me — and the webpage is optimized for Mobile Safari.

No responses yet

Jul 21 2008

Using AppleScript and a Shell Script to Restart an AppleTV remotely

Published by Jonathan Wise under Hacks

Both my media serving devices (a NAS, and a Mac Mini) are using TwonkyVision Media Server to share media using uPnP. It works great, but as I mentioned earlier, I needed a script to make it start back up in the morning.

Unfortunately, I’ve found that my AppleTV freaks out a bit when this happens, and as a result, needs its own reboot. Restarting the Finder helps, but streaming still gets weird, so a full, daily reboot is in order. Using the same iCal-alarm-firing-an-AppleScript trick, I updated my Twonky restart script to also tell the AppleTV to restart. But it wasn’t that easy.

  • First of all, you need to exchange keypairs with the AppleTV so that you can login over ssh without a password. However, the process is a little different, since the AppleTV only supports ssh1. This wiki page explains the process, and the slight tweak to it for ssh1.
  • Second, you need to set the AppleTV up to allow you to sudo without a password. This is harder than it sounds, and requires modifying the /etc/sudoers file on the AppleTV. The only way I found to do that was to sudo cat > ~/sudoers the file (which dumps the contents of one file to another, which you can access more easily), copy it over to my Mac, edit it, copy it back, set the permissions, and sudo mv it back over-top the original. You’ll need to add this line to the bottom of the sudoers file:
    frontrow ALL=(ALL) NOPASSWD: ALL
  • Make sure you change the permissions (0440) and ownership (root:wheel) on your new sudoers before you replace the original, or you’ll screw yourself out of sudo!

To test those things, you could go to Terminal on your Mac and try something like:
ssh -1 frontrow@appletv.local 'sudo ls-l'
If you’ve setup everything right, you shouldn’t get prompted for a password to login OR to get a directory listing. Once that works, the AppleScript is easy and looks like this:

do shell script "ssh -1 frontrow@appletv.local 'sudo reboot' &> /dev/null &"

At some point, I’m also going to attach this to a PHP script (somehow) so I can use Safari on my iPhone to do a reboot from the couch if needed. I’ve only found two other ways to reboot the AppleTV — yanking the power cord, or using the remote to put it in recovery mode. Neither seems as elegant as my solution.

No responses yet

Jul 09 2008

Restart TwonkyMedia after scheduled sleep

Published by Jonathan Wise under Hacks

In our house, a Mac Mini serves at the content library and retreival machine. It runs a Torrent client, iTunes, and TVShows to collect and organize all our media. At night it runs a number of scheduled tasks to keep things orderly and fresh, and when its done (at about 4:00am) it goes to sleep until 8:00am when my wife will want to use it again.

TwonkyMedia server, which is delivering our non-iTunes content onto the AppleTV (via the MediaCloud uPnP client) has a problem with this. It doesn’t recover well from sleep.

The solution is to run an AppleScript every morning at 8:01 to re-start TwonkyMedia. This little AppleScript will access Twonky’s built-in shell script to shut down any running instances, and start-up a fresh one:

do shell script "/Applications/MediaServer.app/Contents/MacOS/twonkymedia.sh &> /dev/null &"

The “&> /dev/null &” sends the shell script’s output to oblivion so that AppleScript doesn’t hang waiting for the server to start-up.

The only annoying part about this is that every time TwonkyMedia starts up, it wants to take you to its home page in your browser. Follow these directions to make TwonkyMedia start-up silently (last post on the page.)

No responses yet

Jun 27 2008

Learning to Develop for the Mac - Historically

Published by Jonathan Wise under Articles

Going back to the beginning, and learning Project Builder, Interface Builder and ObjectiveC on NextStep. Can you believe this is the technology that NeXT had the same year Windows 3.1 came out? And its the same stuff that’s used to build Mac and iPhone apps today…

Click for a bigger picture.

In fact, the technology is so close that I used a tutorial for developing a sample Mac app, to develop a sample NextStep app. The notion of “FAT Binaries” that work on multiple hardware architectures came from NeXT as well, since it ran on any number of platforms by the end.

No responses yet

Jun 26 2008

Installing NextStep 3.3 on VMWare 6

Published by Jonathan Wise under Articles, Hacks

I’ve been hankering to learn more about the origins of OS X, so I figured the best place to start would be the OS at its core — NextStep (later named OpenStep.)

When Steve Jobs was ousted from Apple, he set out to create a new company that would deliver elite hardware, and a revolutionary operating system. NeXT was started to build powerful hardware and software that would leapfrog the Mac (and everyone else on the market.) They eventually had to kill off the hardware line (after making some very nice, uber-high-end machines) but the software they made changed the industry. It was on a NextStation computer, using the NextStep OS that Tim Berners Lee would invent the World Wide Web, and its the true Object Oriented approach of the OS that many companies have imitated or borrowed.

By the mid-90s Apple was in truly rough shape. Their archaic OS, once the greatest thing on the market, was showing its age, and Apple had tried, and failed (mostly due to mismanagement) to replace it. It was determined that they should buy an existing next-gen OS, and re-build on top of it. After flirting with IBM and BeOS, they settled on a deal with NeXT. A deal that grew until Apple acquired the company, re-acquiring Steve Jobs in the process. He became the iCEO (interim CEO) and the de-facto leader of the company. Existing ideas were rapidly brought over from the Mac onto NextStep/OpenStep, layered on top of its rock-solid BSD Mach microkernel, Object Oriented underpinnings, and a re-vamped version of its graphics layer, was a new Interface, and a Classic emulator to allow old Mac apps to run.

The end product, after many variations, pre-releases, betas, and one or two fairly ugly general releases, was what we know today as Mac OS X. Much of NextStep is still intact, including low-level OS APIs, visual and development concepts, and culture.

If you’re interested in getting NextStep up and running in VMWare (I’m using a Windows Vista 64-Bit Host OS, but these steps should work with most versions of VMWare — including Fusion) you’ll need to find an ISO of the NextStep OS somewhere, and maybe these steps will help you…

Continue Reading »

One response so far

Mar 18 2008

Adventures with a Hackintosh

Published by Jonathan Wise under Articles, Hacks

What I really want out of my job, aside from the pay check, the potential for advancement, and a regular challenge for my problem solving skills, is to be able to use a Mac as my primary machine. Being a Windows developer, though, that’s not very likely.
For awhile I had a MacBook Pro, which ran Windows via Parallels Workstation like a champ, and let me have the best of both worlds. Alas, that machine is no longer available to me. Replacing it would be too costly, and my needs dictate that I’d require a high-end Mac to be able to keep up with me, so a cheap little Mac Mini wouldn’t do the job.

It occurred to me then, that maybe I could try (again) to build myself a Hackintosh, using the latest hacked release of OSX86 10.5. I do run a Mac VM image on my PC, that lets me use my favorite mail client, but it’d be nice if I could reverse the situation. I figured I’d be more than happy to purchase an additional Leopard license, to make my use (mostly) legit. So I went out and got a new hard drive to dedicate to the experiment. Here are my findings…

At work I have a Dell Optiplex 745. A nice machine, well equipped with a Core2Duo and 4GB of RAM. I checked the OSX86 Hardware compatibility wiki and verified that most of my hardware was on the list with either a “working” or “working with patch” status. I figured at a minimum I would need network and video card drivers on a stable OS capable of running VMWare Fusion for the project to be worthwhile. Things looked promising, so I began the download.

OSX86 Hacked install discs can be found on PirateBay and other fine Torrent websites. Again the legality of it is questionable. While you’re required to have an OS license for each machine, the OS X EULA does state that it can only be run on Apple branded hardware. But that sounds more like a challenge than a rule to me.
Downloading should be fairly easy — Kalyway seems to have the best builds right now — but I did find a couple suggestions on community websites. Use WinRAR to unpack the archive, as there have been some problems with other .Zip programs not opening the file correctly. The ISO itself should be burned on a Mac, or on a PC using TransMac, so as not to corrupt the files with any traces of Windows… or something. Burn it slowly! I’m not sure why people recommend these things, but I know I tried this with a 10.4.6 hack install and did it the easy way, and ended up with a coaster. This time I was super careful.

The install disc booted right off the bat for me, and following the suggestions on the Wiki I formatted my new drive as HFS Journaled, using the GUID partition scheme. I did a Vanilla install, adding the SSE3 extensions and ATI drivers for the 1×00 series cards (my Dell has a X1300 Pro)
The install completed successfully and I was ecstatic when I rebooted (don’t forget to remove the install DVD) and saw the Apple logo on the gray screen… then crushed when it promptly kernel panicked.

I rebooted with the -v switch so I could watch what was happening, and eventually realised it was the video card drivers causing the problem. I rebooted off the install DVD, formatted again and installed without the NATIT ATI kexts… only to have it kernel panic on another driver.
Finally, I tried it all again, this time with the Vanilla Kernel, SSE3 extensions, the ACPI fix, no video card drivers, and using the MBR partition scheme. And it STILL kernel panicked at loading drivers.

This time I read a little more online and figured out that I could boot into the install DVD, get to a terminal window, delete the NATIT kexts, and reboot. I tried that, and low and behold, I was into the “Welcome to OS X” wizard!

I had the well-documented problem with it getting caught in a loop after the “Import from another Mac” step, and again had to boot with the install DVD, drop to a terminal, and poke in a few commands to tell OS X that I’d already set-up my account. None of this would be so bad except booting from the DVD to a terminal window took nearly 20 minutes each time.

Finally I was able to boot into the Finder. It was glorious and looked as beautiful as a Mac should look… circa 2001. Without video card drivers I was stuck at 1024×768 mirrored on both my monitors. I poked around the ‘net a little bit, but decided my priority needed to be network drivers. My Dell has an on-board Broadcom NIC, which seemed pretty common, and although it didn’t work out-of-the-box, there were plenty of forums with solutions for hacking support for my particular DeviceID into the built-in drivers.
Alas, none of them worked. I got as far as getting OS X to recognize the card, but hit the issue with it not knowing its own MAC address. I tried the solution that basically has you telling it the MAC from the terminal at each boot, but that failed. I gave up and bought $30 D-Link card with manufacturer’s official drivers available.

Side note: that card never worked either, but thats because I had forgotten that I’d disabled my PCI slots in the BIOS, intending on turning on all on-board devices one-by-one once I was up and running. Note, that the on-board NIC WAS turned on. There were just no working drivers.

I turned my attention briefly back to the video card, hoping to get at least one thing working properly. I was able to download a bunch of drivers using the Craptop and burn them to CD for use on the Hackintosh. All of them installed, none of them worked. I finally hit upon a forum post that pointed out issues with the Radeon 1300 cards. Despite being a member of the 1X00 family, these particular cards were not supported properly by NATIT.

Sound wasn’t working either, and there was no solution, or potential solution online. And here I gave up.
I do wish I had gotten it working — it was pretty darn cool. But at > 20 hours invested and nothing to show for it except the Finder, and no hope in sight for Video card drivers, and problems reported running VMWare on a Hackintosh, I decided it wasn’t worth investing any more time in. Still it was a righteous hack, and if I had a machine with compatible hardware, it would probably have been worth it.

Oh well, I’ve been running Vista SP1 for the past week or so, and its actually not that horrible.

One response so far

Jan 27 2008

Using Mail.app with multiple users — using AppleScript: Part 2

Published by Jonathan Wise under Hacks

The previous post, about hacking multi-user support into Mail.app, seems to have struck a chord, and there have been lots of great questions about how to extend the script, or customize its behavior or appearance. Things tend to get lost in the comments, so I figured I’d create a part 2 to answer some of the questions that have popped up. If you haven’t already, check out part 1 to get caught up.

Some of these might seem a little basic to long-time users, but lets remember that not everyone has been using a Mac since OS 7 (or earlier!) and cut the newly Mac-faithful some slack…

Evan asks: Absolutely perfect, exactly what I was looking for. Well, almost. How about adding a 3rd account?

A third account is done very easily by modifying the script slightly. (Update: If you’d like to do more than 3 accounts, see the solution in the comments which changes our user interface to a listbox)

The first thing you’ll want to do is update our crude little user interface to ask about the third account. This line here decides what options show up in the dialog box:

display dialog "Choose the Mail account to use" buttons {"Jon", "Nicole"} default button 1 with icon note

You can add as many up to 3 options as you want, just by comma seperating them, so instead of {”Jon”, “Nicole”} you could have {”Jon”, “Nicole”, “Yoda”}
Then you need to modify the “if” statement to support each individual referred to. In the original we said:

if the button returned of the result is "Jon" then
-- do stuff to change account to Jon
else
-- do stuff to change account to Nicole
end if


The else is a problem now because it assumes only two conditions. Instead of an else, we can do an else if — one for each individual we want to switch between:

if the button returned of the result is "Jon" then
-- do stuff to change account to Jon
else if the button returned of the result is "Nicole" then
-- do stuff to change account to Nicole
else if the button returned of the result is "Yoda" then
-- do stuff to change account to Luke
end if


Inside each condition you’ll need to disable all the other accounts (set enabled to account X to false) and then enable the account they’ve chosen.

Eric asks: I would like to attach to the script the mail icon or a similar icon. Then when I added it to the dock, it is more obvious to click it to run.

This is one of those tricks that long time Mac users will know well, but is not very obvious for newer converts. You can copy and paste any icon from any application/document/folder to any other in the finder. Just click on the icon you want to “borrow” from and hit Apple + I for “Get Info.” In the info dialog, click on the icon and hit Apple + C to copy the icon to the clipboard. Now find your target icon (such as the script you just made) and “Get Info” on it, click the icon in the dialog and this time hit Apple + P to paste. Now you have a pretty icon!

Note: This will be limited by permissions, so if your user doesn’t have permission to ‘write’ to the target object, you won’t be able to paste.

Eric, having figured out the above on his own, then asks: My 2 problems are: 1, I can not add the script to the dock. 2, when I click the script icon, it take me to the editor where I then have to click RUN.

A script, by itself is just a document. In order to make it into a runable application, you have to save it as such in Script Editor. From the File / Save As… dialog change the File Format to “application.”

Under most circumstances you won’t want to check the box for ‘Run Only’ because once you do, you cannot edit it in Script Editor again. Also, uncheck the box for ‘Startup Screen’ to make it run a little more gracefully.

You may also find that the Dock is not the best place for your script — since it will essentially give you two Mail icons. What I did instead was to enable the AppleScript menu and use that for all my common scripts.

If you want to do this, open the Application AppleScript Utility in the AppleScript folder and check the box for ‘Show Script Menu in the menu bar.’ You may also want to uncheck the box for “Show Library scripts” to hide the example scripts Apple includes to make your menu shorter.

Note: For scripts to show up in this menu you’ll have to save them where your Mac expects your scripts to be: In your user’s Library folder you’ll find a folder called “Scripts.” Put them, or an alias to them, in there.

8 responses so far

Jan 06 2008

Using Mail.app with multiple users — using AppleScript!

Published by Jonathan Wise under Code Snippets, Hacks

OK, this is pretty brilliant — and SO simple.

Here’s the setup: my wife and I share a Mac at home. For memory reasons, among others, I don’t want to use Fast User Switching, and because of my automated tasks, I don’t want the primary account to ever be logged out. So all many of our programs need to be set-up for two different users. Firefox has user profiles, that once configured, works perfect. Mail.app has no such thing. What I decided to then, was write an AppleScript that would switch Mail.app between users for us. This example is for two users, but it could be edited for more. Here’s how to use it:

  • Setup Mail accounts for each user
  • Modify the script to prompt for each user you have, and reference their account name
  • Replace your Mail.app dock icon with a link to your AppleScript (you can even give it the Mail.app icon)
  • Whenever you launch Mail, you’ll be asked which user you want to use
  • Even better than that, you can switch users just by clicking the Mail icon in your dock again. You don’t even have to close down Mail!

The result looks like this whenever you invoke the script, and launches/reconfigures Mail within two seconds. Set the delay to longer if Mail.app takes longer to start on your Mac.

The code is dead simple, and took me only moments to put together. Note that the delays and the order in which things are done is important so that it doesn’t hang waiting for Mail to start if its not already open.

-- Mail Account Chooser, by Jonathan Wise
-- Add user profiles to Mail.app
display dialog "Choose the Mail account to use" buttons {"Jon", "Nicole"} default button 1 with icon note
if the button returned of the result is "Jon" then
  tell application "Mail"
    activate
    delay 2
    set enabled of account "Nicole Home" to false
    set enabled of account "Jon Home" to true
  end tell
else
  tell application "Mail"
    activate
    delay 2
    set enabled of account "Jon Home" to false
    set enabled of account "Nicole Home" to true
  end tell
end if

11 responses so far

Nov 05 2007

Leopard doesn’t quite stack up

Published by Jonathan Wise under Articles

I’m a Mac fan boy, and I’m very tolerant of change. When the Mac using world screamed when they replaced OS 9 with OS X, killing a lot of the nice functionality in OS 9 in favor of a brand new OS, I drank the kool-aid and made the change as soon as I could.

And I’ve been a big fan of each major revision of X — and have run all of them right out of the gate, warts and all. But I’m not sold on Leopard.

The loudest touted features are either useless, or already existed in 3rd party apps for 10.4.
- Spaces is just VirtuDesktops — and a concept I never had any use for. I’d much rather have multiple monitors then multiple “virtual” screens.
- TimeMachine is just a back-up app — granted its well integrated and looks pretty. But the method I have in place now is much less intrusive (not to mention more fire-proof) and doesn’t require me to be tethered to a local external hard drive.

But nothing bugs me more than Stacks…
Stacks kills a feature I’ve used and loved in OS X since they killed the Apple menu’s hierarchical app-launching capabilities from OS 9.

It used to be, in OS X 10.2-10.4, that you could drop any folder onto the right side of the Dock and turn it into a menu by right clicking on it. In this way I could add my very organized Applications folder to my Dock and use it to find applications in a very simple manor (I’ll admit, its kind of like a Start Menu, but come on — its a good way to organize your apps).

In 10.5 that great feature was replaced with either a silly, and surprisingly static, curved list, or a box full of icons. Now that box can contain subfolders — but if you click on them, it opens a window. That’s a window you have to close after you find your application. Unacceptably stupid.

Know what else is stupid? Apparently there’s a massive data loss bug in the new Finder. For years — literally, we’ve been complaining about how awful the Finder is (even compared to its OS 9 grandparent) and this is the fix they offer us?

Damage to the Dock and a bad upgrade for the Finder aside (I do like the new sidebar, btw) Stacks are not what they were supposed to be. A “Stack” was designed as a replacement for (or addition to) the folder concept. It was an organizational item that existed in the Finder, had a single icon that could be dragged around like a file, but expanded when clicked to show a collection of items. There are plenty of UI prototype videos on the web showing what Stacks meant to us before Apple polluted the term.

I’m ditching Leopard for the time being. The only feature I really wanted was the improved Front Row… maybe there’s a way I can run it in 10.4. If not, I’ll just hold out hope that Apple gets their crap together with upcoming updates.

Update: Yay! It looks like they fixed stacks… a little bit!

4 responses so far

Oct 14 2007

Enabling WebDAV on OS X

Published by Jonathan Wise under Links

I won’t re-write the article, but this weekend I wanted to turn on WebDAV on my Mac mini, and this article pretty much covered it for me. The article talks about using iCal, but do the steps and see the FAQ for additional tips on regular file sharing.
The only note I’d add is that Digest authentication didn’t work for me in OS X 10.4.10 — I had to use Basic authentication.

No responses yet

Next »