tags:

views:

53

answers:

2

Hello All,

I want to read list of applications from the Applications folder on Mac using Qt or Carbon. I am not sure how to do this. So any pointers will be appreciated.

Thanks Rahul

A: 

You can list a directory using either the opendir(3) and readdir(3) functions or the FSOpenIterator and FSGetCatalogInfoBulk functions from the Core Services File Manager.

Peter Hosey
A: 

The easiest solution is to get the Applications dir and then use the Qt helpers to iterate over it - i.e QDir, and finding bundles as directories whose names end in '.app'. Here's some code to get a QDir from a folder reference type - there are many similar constants, to get the desktop/trash/library folders. The 'domain' value is important - for many folders (eg, Library) there's a per-user version as well as global and network versions. FileVault can complicate things further.

The documentation on FSFindFolder should make things clearer, and there's examples all over the web.

static QDir applicationsDir()
{
    short domain = kOnAppropriateDisk;
    FSRef ref;
    OSErr err = FSFindFolder(domain, kApplicationsFolderType, false, &ref);
    if (err) {
        return QDir();
    }

    return QDir(getFullPath(ref));
}

/*
    Constructs a full unicode path from a FSRef.
*/
static QString getFullPath(const FSRef &ref)
{
    QByteArray ba(2048, 0);
    if (FSRefMakePath(&ref, reinterpret_cast<UInt8 *>(ba.data()), ba.size()) == noErr)
        return QString::fromUtf8(ba).normalized(QString::NormalizationForm_C);
    return QString();
}
James Turner