It's easier than ever to create background applications in Mac OS X. However, to convert a REALbasic-made application to a background application, you'll need to have a trick or two up your sleeve. This week we'll show you how to make a REALbasic application hide in the background. We'll also build a small tool to automate the process, so you don't have to think about this again.
Backgrounding Background
A background application is handy for times when:
<key> NSUIElement </key> <string> 1 </string>
Pretty simple, eh? When you build a Mac OS X application with REALbasic, however, there isn't one of these plist files to be found. All you have is an executable file. Look inside the resource fork with your favorite resource editor and you'll find the plist information in the 'plist' resource. Scroll to the end of that resource and locate this tidbit:
You could paste in the NSUIElement key listed above, but why bother. Not only do you have to poke around the resource fork and enter some text, you also have to edit the hex code of that character between the two lines in the previoes XML code to chr(13). That's too much of a pain for lazy programmers... especially when you want to debug a background application, where you might have to repeat this resource process dozens of times. To make life simple, let's create a tool to do the job for us.
Build the Project
This project is super simple to build. Launch REALbasic and use the default Window1 in the new project. To the window's Open event, add this one line of code:
me.acceptFileDrop "any"
Then, in the DropObject event of Window1, add this code:
dim r as resourceFork
dim s as string
if obj.FolderItemAvailable then
r=obj.FolderItem.openresourceFork
if r<>nil then
s=r.GetResource("plst",0)
s = replace(s,"</dict>","
<key>
NSUIElement
</key>
"+chr(13)+"
<string>
1
</string>
"+chr(13)+"</dict>")
r.AddResource s,"plst",0,""
r.close
end if
end if
That's it! Build the application and run it. Now, whenever you drag a REALbasic-made OS X application into the window, it will instantly transform into a background application. When you create a background application, don't forget to give yourself a way to quit it, since the menubar won't be available.
Conclusion
As usual, you can download the code for this week's project. See you next week!