jjEnsureAssets

April 23, 2008 in Mac OS X by chip

PROBLEM: What if your application depends on a bunch of auxiliary files in various places around the user’s hard drive- maybe these are pictures, movies, text files, settings, spreadsheets, who knows? You know you can make an installer that initially, will put all these files in all the right places. But what if the user accidentally deletes or moves some of them?

Instead of an installer, here is a function you can run when starting up that will replace any missing asset files, “repairing” an installation if necessary on each launch.

STEP ONE:

Put all the asset files where they need to be. Then use this loop (during development) to locate each all the important files and import them into custom properties:

ON importAssetsIntoCustomProps
    set the custompropertyset of me to "myAssetFiles"
    REPEAT
        answer file "choose file to import (cancel when done)"
        IF it is empty THEN exit to top
        set the myAssetFiles[it] of me to URL ("binfile:" & it)
    END repeat
END importAssetsIntoCustomProp

(You can do this repeatedly, as much as you want, it will update the binary data if you import the same filename that has been updated)

The key of the custom property is the path, and the value is the binary data of the file.

STEP TWO:

To call the function, use something like

ON openStack
   jjEnsureAssets
END openStack

STEP THREE:

Include this handler in your stack script:

ON jjEnsureAssets
    set the custompropertyset of me to "myAssetFiles"
    put the customproperties of me into tAssets
    REPEAT FOR each line pFile in the keys of tAssets
        IF there is no file pFile THEN
            makeFoldersIfNecessary pathFromPathAndFile(pFile)
            put the myAssetFiles[pFile] of me into URL ("binfile:" & pFile)
        END IF
    END repeat
END jjEnsureAssets

You will also need these handlers:

makeFoldersIfNecessary pathFromPathAndFile

TO DO: Put in a merge or something to accommodate files in the user’s folder.