开发者

Programmatically Installing Fonts

开发者 https://www.devze.com 2022-12-23 06:42 出处:网络
How could I programmatically install a font on the Mac platform (Snow Leopard)? W开发者_运维百科hat steps would I need to follow? I would like for the user to input a font file, then my software insta

How could I programmatically install a font on the Mac platform (Snow Leopard)? W开发者_运维百科hat steps would I need to follow? I would like for the user to input a font file, then my software installs it.


Fonts belong in ~user/Library/Fonts/ for a single user or /Library/Fonts/ to be accessible to all users. You need to get permission in order to write to /Library/Fonts/, although there is an API for that which makes it relatively easy. (I have the code somewhere and can look it up if no one else knows offhand.)


As requested, here are some API docs:

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

This is old code I have that did the update under Carbon (hence the pascal strings). It was based on sample code which is probably somewhere in the above URL. I haven't looked into doing this under Cocoa, and this is a edited version of the code (still a bit messy), so 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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消