Mac Helper Interface
1. Open the directory where the file is located and select the file
2. Get the Plist property value
3. System shutdown
4. Turn on system network settings
5. String contains comparison
6. System mount number and Mount drive letter information
//====================================================
1. Open the directory where the file is located and select the file
<1>.cocoa Nsworkspace Way
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
}
eg
----------------------------------------
Char *chstrpath;
Macopenlocatefileinwindow (Chstrpath);
<2>. Qt qprocess Mode
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. Get the Plist property value
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;
}
eg
----------------------------------------
Char chversion[200];
memset (chversion, 0, 200);
int iRet = Getinfoattribute (chversion, "cfbundleversion");
3. System shutdown
void Macshutdown ()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Nsdictionary *errordict = nil;
Nsappleeventdescriptor *returndescriptor = nil;
Nsapplescript *scriptobject = [[Nsapplescript alloc] Initwithsource:
@ "Tell application \" Finder\ "to shut"];
Returndescriptor = [ScriptObject executeandreturnerror:&errordict];
if (nil = = Returndescriptor)
{
No script result, handle error here
}
[ScriptObject release];
ScriptObject = nil;
[Pool drain];
Return
}
4. Turn on system network settings
void Macactivatenetworkpreference ()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
Nsdictionary *errordict = nil;
Nsappleeventdescriptor *returndescriptor = nil;
Nsapplescript *scriptobject = [[[Nsapplescript alloc] Initwithsource:
@ "Tell application \" System preferences\ "\ n"
@ "activate\n"
@ "Set current pane to pane \" Com.apple.preference.network\ "\ n"
@ "End tell\n"] autorelease];
Returndescriptor = [ScriptObject executeandreturnerror:&errordict];
if (nil = = Returndescriptor)
{
No script result, handle error here
}
[Pool drain];
Return
}
5. String contains comparison
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. System mount number and Mount drive letter information
typedef struct {
Char chdrivepath[256];
Char chdrivevolume[256];
}struct_drive_info;
<1>: Number of system mounts
int Macgetmountedpointnums ()
{
int imountpoint = 0;
struct Statfs *buf = NULL;
Imountpoint = Getmntinfo (&buf, 0);
return imountpoint;
}
<2> Mount Drive letter Information
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;
}
eg
----------------------------------------
int imountpoint = Macgetmountedpointnums ();
if (Imountpoint > 0)
{
Struct_drive_info Stdriveinfo[imountpoint];
int inums = Macgetmounteddriveinfo (Stdriveinfo, imountpoint);
}
Mac Helper Interface