Archive for the ‘Code’ Category

Popping up the Default Folder X menu with a hotkey

Monday, June 16th, 2008

Adam Aaron asked if I could make DFX pop up its menu under the mouse when he pressed a certain key combination. You can actually do this now with a simple AppleScript and one of the numerous macro utilities out there (Adam used QuickSilver to make it work on his machine).

First, write a little AppleScript like so:

tell application "Default Folder X"
end tell

Then have your macro utility of choice (QuickSilver, iKey, QuicKeys, or whatever) run the script when you hit your favorite key combination, and there you go 🙂

(Note: If you’re running Default Folder X with its “Show icons and menus in the Dock” setting turned off, the application target in the AppleScript should be “Default Folder X Helper” instead).

Adding Default Folder X to Contextual Menus

Tuesday, May 13th, 2008

If you want to include one of Default Folder X’s menus (Favorites, Recent Folders, etc) in a Finder contextual menu, it’s easy to do with Abracode’s free OnMyCommand contextual menu plugin.

To add a DFX Favorite menu, follow these steps:

  1. Download OnMyCommand.
  2. You’ll get a disk image that looks like this. Run the “Install OnMyCommandCM” script and let it relaunch the Finder after it installs the CM plugin.
  3. Open the “OMCEdit” folder and launch OMCEdit. You’ll get a message that it needs to create a new preference file. Tell it to do so.
  4. From the “Command” menu, choose “New”.
  5. Fill in the command fields like this:
  6. Once you save the command, it should appear in your contextual menus like so. Clicking on it will bring up the full Default Folder X hierarchical Favorites menu.

You can do this same thing for other DFX menus by substituting a different name for “Favorite” in the AppleScript command you use in OMCEdit. Valid options are “Disk”, “Favorite”, “Recent”, and “Finder”. Leaving off the ‘named “Favorite”‘ entirely will pop up the entire DFX menu hierarchy as it appears in the menubar.

Determining if an app is running in 64-bit mode

Wednesday, February 6th, 2008

OK, time for more geek-talk. I’ve spent a couple of late nights fussing with fat binaries, bundles, and mach-o architecture API’s to try and find a way to determine if an app is running in 64 bit mode. Surprisingly, I couldn’t just Google it and get an easy solution.

Looking at the darwin source, I found there’s a sysctl() token to get this information. This code will return the processor type (including the 64-bit flag). Note that it uses the sysctlbyname_with_pid() function in Apple’s Universal Binary Programming Guidelines:

cpu_type_t GetProcessArchitecture(pid_t pid)
    cpu_type_t cputype;
    size_t cpusz = sizeof(cputype);    

    // Default values
#if __i386__
    cputype = CPU_TYPE_X86;
    cputype = CPU_TYPE_POWERPC;

    if (sysctlbyname_with_pid("sysctl.proc_cputype", pid,
                              &cputype, &cpusz, NULL, 0) == -1)
        fprintf(stderr, "proc_cputype: sysctlbyname_with_pid failed:"
                "%sn", strerror(errno));
    return cputype;

Getting the owner of a process

Thursday, December 6th, 2007

I was a little surprised that I couldn’t find this online anywhere, so for others that may need it, here’s a function that gets the effective owner of a Mac OS X process, given the process pid:

uid_t OwnerForPID(int pid)
    struct kinfo_proc info;
    size_t length = sizeof(struct kinfo_proc);
    int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PID, pid };
    if (sysctl(mib, 4, &info, &length, NULL, 0) < 0)
        return kProcessValueUnknown;
    if (length == 0)
        return kProcessValueUnknown;
    return info.kp_eproc.e_ucred.cr_uid;

And of course, you know you can get your own process ID with getpid() and use the Process Manager’s GetProcessPID() function to get a pid if you know another process’ ProcessSerialNumber.