summaryrefslogtreecommitdiffstats
path: root/Mac/Demo/resources/copyres.py
blob: 528ff1679465641439166e53754a60475a795099 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
from Carbon.Res import *
from Carbon.Resources import *
import MacOS

READ = 1
WRITE = 2
smAllScripts = -3

def copyres(src, dst):
    """Copy resource from src file to dst file."""

    cur = CurResFile()
    ctor, type = MacOS.GetCreatorAndType(src)
    input = FSpOpenResFile(src, READ)
    try:
        FSpCreateResFile(dst, ctor, type, smAllScripts)
    except:
        raw_input("%s already exists...  CR to write anyway! " % dst)
    output = FSpOpenResFile(dst, WRITE)
    UseResFile(input)
    ntypes = Count1Types()
    for itype in range(1, 1+ntypes):
        type = Get1IndType(itype)
        nresources = Count1Resources(type)
        for ires in range(1, 1+nresources):
            res = Get1IndResource(type, ires)
            res.LoadResource()
            id, type, name = res.GetResInfo()
            size = res.SizeResource()
            attrs = res.GetResAttrs()
            print id, type, name, size, hex(attrs)
            res.DetachResource()
            UseResFile(output)
            try:
                res2 = Get1Resource(type, id)
            except (RuntimeError, Res.Error), msg:
                res2 = None
            if res2:
                print "Duplicate type+id, not copied"
                print (res2.size, res2.data)
                print res2.GetResInfo()
                if res2.HomeResFile() == output:
                    'OK'
                elif res2.HomeResFile() == input:
                    'BAD!'
                else:
                    print 'Home:', res2.HomeResFile()
            else:
                res.AddResource(type, id, name)
                #res.SetResAttrs(attrs)
                res.WriteResource()
            UseResFile(input)
    UseResFile(cur)
    CloseResFile(output)
    CloseResFile(input)

copyres('::python.¹.rsrc', '::foo.rsrc')