2010-03-16 115 views
1

如何以编程方式在Mac平台(雪豹)上安装字体?我需要遵循哪些步骤?我想让用户输入一个字体文件,然后我的软件安装它。以编程方式安装字体

回答

3

字体属于〜user/Library/Fonts /单个用户或/ Library/Fonts /可供所有用户访问。您需要获得权限才能写入/ Library/Fonts /,尽管有一个API使得它相对容易。 (我的代码的地方,并可以看看它,如果没有其他人知道副手)


按照要求,这里有一些API文档:

http://developer.apple.com/mac/library/documentation/Security/Reference/authorization_ref/Reference/reference.html

这是旧的代码我有在Carbon下进行更新(因此是pascal字符串)。它基于可能位于上述URL中的示例代码。我没有考虑过在Cocoa下做这个,而且这是编码的代码版本(仍然有点麻烦),所以YMMV。

int main() 
{ 
    OSStatus myStatus = -1; 
    char path[1024]; 
    char myToolPath[2048]; 
    getUpdateAppPath(myToolPath); 
    getFilePath(path); 

    if (path[0] != 0) 
    { 
     char temp[2048]; 
     FILE *f; 
     printf("Attempting to open \'%s\'\n", path); 
     f = fopen(path, "w+"); 
     if (f != 0) // we seem to have write permission 
     { 
      fclose(f); 
      SInt16 res; 
      sprintf(temp, "\'%s\' \'%s\'", myToolPath, path); 
      system(temp); 
      StandardAlert(kAlertNoteAlert, "\pUpdate Complete", "\pSuccessfully updated.", 0, &res); 
      return 0; 
     } 

     AuthorizationFlags myFlags = kAuthorizationFlagDefaults; 
     AuthorizationRef myAuthorizationRef; 
     myStatus = AuthorizationCreate(NULL, kAuthorizationEmptyEnvironment, 
             myFlags, &myAuthorizationRef); 
     if (myStatus != errAuthorizationSuccess) 
     { 
      SInt16 res; 
      StandardAlert(kAlertNoteAlert, "\pAuthorization Error", "\pCould not authorize application to update.", 0, &res); 
      return myStatus; 
     } 

     AuthorizationItem myItems = {kAuthorizationRightExecute, 0, NULL, 0}; 

     AuthorizationRights myRights = {1, &myItems}; 
     myFlags = kAuthorizationFlagDefaults | 
     kAuthorizationFlagInteractionAllowed | 
     kAuthorizationFlagPreAuthorize | 
     kAuthorizationFlagExtendRights; 

     myStatus = AuthorizationCopyRights (myAuthorizationRef, &myRights, NULL, myFlags, NULL); 

     if (myStatus != errAuthorizationSuccess) 
      break; 

     char *myArguments[] = { path, NULL }; 
     FILE *myCommunicationsPipe = NULL; 
     char myReadBuffer[128]; 


     myFlags = kAuthorizationFlagDefaults; 
     myStatus = AuthorizationExecuteWithPrivileges(myAuthorizationRef, myToolPath, myFlags, myArguments, 
                 &myCommunicationsPipe); 

     if (myStatus == errAuthorizationSuccess) 
      for(;;) 
      { 
       int bytesRead = read (fileno (myCommunicationsPipe), 
             myReadBuffer, sizeof (myReadBuffer)); 
       if (bytesRead < 1) break; 
       write (fileno (stdout), myReadBuffer, bytesRead); 
      } 

     AuthorizationFree (myAuthorizationRef, kAuthorizationFlagDefaults); // 17 
    } 
    if (myStatus) 
    { 
     printf("Status: %ld\n", myStatus); 
     SInt16 res; 
     StandardAlert(kAlertNoteAlert, "\pUpdater Error", "\pMay not have updated properly.", 0, &res); 
    } 
    else { 
     SInt16 res; 
     StandardAlert(kAlertNoteAlert, "\pUpdate Complete", "\pSuccessfully updated.", 0, &res); 
    } 
    return myStatus; 
} 
+1

是的请,api!:) – Avram 2010-03-16 22:03:56

+0

增加了更多的细节。希望这可以帮助;自从我使用这个版本已经有几年了,所以事情可能已经改变了,但它应该让你朝着正确的方向前进。 – 2010-03-16 23:17:40