Archive for the ‘Code’ Category

Move Items Contextual Menu for Snow Leopard

Wednesday, September 16th, 2009

Contextual menu plugins are dead in Snow Leopard, replaced by the revamped Services system.  A user recently contacted me because he wanted to replicate the “Move Items” contextual menu item he used to use in Leopard.  He had used Automator to create a service, but was having a few problems, namely that Default Folder X wasn’t available when he chose the destination folder.

This got me to open up Automator in Snow Leopard and take a crack at it myself.  In the process, I was reminded how cool Automator is 🙂  At any rate, here’s the automator script I put together:

So you’re obviously asking: Why go to the trouble of creating variables instead of just using the “Move Finder Items” action by itself?  I’m glad you asked!  The reason is that I want to bring up a file dialog to specify the folder where I want the items to go.  There’s not a clean way to have the “Move Finder Items” do that every time.  You can change its options to “Show this action when the workflow runs” but you still have to click on it every time you use it to ask it to show a file dialog.  If you use Default Folder X to enhance your Open dialogs, it’s faster to just have the dialog pop up and then go where you want to with DFX.

So in the image above, the workflow puts the current Finder selection into the “selection” variable.  Then it uses AppleScript to bring up a file dialog to ask for a folder, which it stores in the “path” variable.  And finally, it uses the Move Finder Items action to do the work.  Not too much more complicated, and it speeds up your workflow considerably if you’ve already got DFX installed so the Open dialogs are smart.

For you automator programmers, note that some of the actions shown in the workflow do not take inputs.  I did this by control-clicking on the action (“Get Value of Variable”, for example) and choosing “Ignore Input” from the contextual menu.  If you don’t do this, Automator will actually add the input from the previous step to the next one, which is definitely not what you want in this case.

Oh, and if you just want the automator workflow file so you can add it to your own system, you can download it here:

http://www.stclairsoft.com/download/MoveItems.zip

If you need more help with Automator and Services, Apple has some good information and tutorials here:

http://www.macosxautomation.com/services/learn/

(Once you’ve gotten through the first few steps of the tutorial, you should be able to just replicate the picture above to make the Move Items service yourself).

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"
ShowMenu
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;
#else
    cputype = CPU_TYPE_POWERPC;
#endif

    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.