mac 辅助接口

mac 辅助接口
1.打开文件所在目录并选中该文件
2.获取plist属性值
3.系统关机
4.打开系统网络设置
5.字符串包含比较
6.系统挂载数及挂载盘符信息

//====================================================
1.打开文件所在目录并选中该文件
<1>.cocoa NSWorkspace方式

void MacOpenLocateFileInWindow(const char *pChFilePath)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    
    NSString *nsStrFilePath = [[NSString alloc] initWithUTF8String:pChFilePath];
    NSURL *nsFileURL = [NSURL fileURLWithPath:nsStrFilePath];
    NSWorkspace *nsWorkSpace = [NSWorkspace sharedWorkspace];
    [nsWorkSpace selectFile:[nsFileURL path] inFileViewerRootedAtPath:nil];

    [pool drain];
    return;
}

接口测试:

char *chStrPath;
MacOpenLocateFileInWindow(chStrPath);

<2>.Qt QProcess方式

QString strFilePath;
QStringList scriptArgs;
scriptArgs << QLatin1String("-e")
                << QString::fromLatin1("tell application "Finder" to reveal POSIX file "%1"").arg(strFilePath);
QProcess::execute(QLatin1String("/usr/bin/osascript"), scriptArgs);
scriptArgs.clear();
scriptArgs << QLatin1String("-e")
                << QLatin1String("tell application "Finder" to activate");
QProcess::execute("/usr/bin/osascript", scriptArgs);

2.获取plist属性值

int MacGetInfoAttribute(char *pChAttribute, const char *pChKey)
{
    int iRet = -1;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSString *nsStrAttribute = nil;
    NSString *nsStrKey = [[NSString alloc] initWithUTF8String:pChKey];
    nsStrAttribute = [[NSBundle mainBundle] objectForInfoDictionaryKey:nsStrKey];

    if (nil != nsStrAttribute)
    {
        const char *pChAtt = [nsStrAttribute UTF8String];
        int iLen = [nsStrAttribute length];

        memset(pChAttribute, 0, 200);
        memcpy(pChAttribute, pChAtt, iLen);
        iRet = 0;
    }

    [nsStrKey release];
    nsStrKey = nil;

    [pool drain];
    return iRet;    
}

接口测试:

char chVersion[200];
memset(chVersion, 0, 200);
int iRet = GetInfoAttribute(chVersion, "CFBundleVersion");

3.系统关机

void MacShutDown()
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSDictionary *errorDict = nil;
    NSAppleEventDescriptor *returnDescriptor = nil;

    NSAppleScript *scriptObject = [[NSAppleScript alloc] initWithSource:
                                                @"tell application "Finder" to shut down"];
    returnDescriptor = [scriptObject executeAndReturnError:&errorDict];    
    if (nil == returnDescriptor)
    {
        //no script result, handle error here 
    }

    [scriptObject release];
    scriptObject = nil;

    [pool drain];
    return;
}

4.打开系统网络设置

void MacActivateNetworkPreference()
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSDictionary *errorDict = nil;
    NSAppleEventDescriptor *returnDescriptor = nil;

    NSAppleScript *scriptObject = [[[NSAppleScript alloc] initWithSource:
                                              @"tell application "System Preferences"
"
                                              @"activate
"
                                              @"set current pane to pane "com.apple.preference.network"
"
                                              @"end tell
"] autorelease];
  
    returnDescriptor = [scriptObject executeAndReturnError:&errorDict];    
    if (nil == returnDescriptor)
    {
        //no script result, handle error here 
    }

    [pool drain];
    return;
}

5.字符串包含比较

bool MacCaseStrContains(const char *pSourceStr, const char *pSearchStr)
{
    bool bRet = false;
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *nsSource = [[NSString alloc] initWithUTF8String:pSourceStr];
    int iSourceLen = [nsSource length];
    [nsSource release];
    nsSource = nil;

    NSString *nsSearch = [[NSString alloc] initWithUTF8String:pSearchStr];
    int iSearchLen = [nsSearch length];
    [nsSearch release];
    nsSearch = nil;

    if (iSourceLen<=0 || iSearchLen<=0 || iSearchLen>iSourceLen)
    {
        [pool drain];
        return bRet;
    }

    int iChSource, iChSearch;
    do{
            iChSource = tolower(*pSourceStr++);
            iChSearch = tolower(*pSearchStr++); 
    }while ((--iSearchLen>0) && iChSource==iChSearch && iChSource!=0);

    if (0 == iChSource-iChSearch)
    {
        bRet = true;
    }

    [pool drain];
    return bRet;
}

6.系统挂载数及挂载盘符信息

typedef struct {
    char chDrivePath[256];
    char chDriveVolume[256];
}STRUCT_DRIVE_INFO;

<1>.系统挂载数

int MacGetMountedPointNums()
{
    int iMountPoint = 0;
    struct statfs *buf = NULL;
    iMountPoint = getmntinfo(&buf, 0);
    return iMountPoint;
}

<2>.挂载盘符信息

int MacGetMountedDriveInfo(STRUCT_DRIVE_INFO *structDriveInfo, int iMoundtedNums)
{
    int iMountNum = 0;
    if (iMoundtedNums <= 0)
    {
        return iMountNum;
    }
    
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    DASessionRef session = DASessionCreate(kCFAllocatorDefault);
    if (session)
    {
        unsigned i, count = 0;
        struct statfs *buf = NULL;

        count = getmntinfo(&buf, 0);
        for (i=0; i<count; i++)
        {    
            DADiskRef disk = DADiskCreateFromBSDName(kCFAllocatorDefault, session, buf[i].f_mntfromname);
           if (disk)
           {
               CFDictionaryRef details = DADiskCopyDescription(disk);
               if (details)
               {
                   if (kCFBooleanTrue == CFDictionaryGetValue(details, kDADiskDescriptionMediaRemovableKey))
                   {
                       NSDictionary *dd = (NSDictionary*) DADiskCopyDescription(disk);
                       if (dd)
                       {
                           if (0==memcmp(buf[i].f_fstypename, "udf", 3) && iMountNum<iMoundtedNums)
                           {
                               NSString *nsStrVolumeName = [dd objectForKey:(NSString*)kDADiskDescriptionVolumeNameKey];
                               int iLen = [nsStrVolumeName length];
                               memset(structDriveInfo[iMountNum].chDriveVolume, 0, 256);
                               memcpy(structDriveInfo[iMountNum].chDriveVolume, [nsStrVolumeName UTF8String], iLen);

                               NSString *nsStrDrivePath = [NSString stringWithUTF8String:buf[i].f_mntonname];
                               iLen = [nsStrDrivePath length];
                               memset(structDriveInfo[iMountNum].chDrivePath, 0, 256);
                               memcpy(structDriveInfo[iMountNum].chDrivePath, [nsStrDrivePath UTF8String], iLen);                                
                               iMountNum++;
                           }

                            [dd release];    
                            dd = nil;    
                        }    
                    }
                    CFRelease(details);
                }
                CFRelease(disk);
            }
        }
        CFRelease(session);    
    }    

    [pool drain];
    return iMountNum;
}

接口测试:

int iMountPoint = MacGetMountedPointNums();
if (iMountPoint > 0)
{
    STRUCT_DRIVE_INFO stDriveInfo[iMountPoint];
    int iNums = MacGetMountedDriveInfo(stDriveInfo, iMountPoint);
}
原文地址:https://www.cnblogs.com/sz-leez/p/4501411.html