Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.05 KB | None | 0 0
  1. #I should reorganize this with functions, but meh, can't be stuffed
  2.  
  3. import macfs
  4. import os
  5. from Res import *
  6.  
  7. #FSpOpenResFile constants
  8. READ = 1
  9. WRITE = 2
  10.  
  11. def main():
  12.     filespec, ok = macfs.StandardGetFile()
  13.     if not ok:
  14.         exit(0)
  15.     print filespec.as_pathname()
  16.     print filespec.GetCreatorType()
  17.    
  18.     filename = filespec.as_tuple()[2]
  19.     base_folder = '%s Dumped Resources' % filename
  20.     #TODO Should handle this already existing
  21.     os.mkdir(base_folder)
  22.    
  23.     #TODO Need to do some error handling here for files with no resource fork
  24.     res_file = FSpOpenResFile(filespec, READ)
  25.  
  26.     type_count = Count1Types()
  27.     print 'There are %d types here' % type_count
  28.    
  29.     #Get1IndType etc is 1-indexed
  30.     for type_index in range(1, type_count + 1):
  31.        
  32.         res_type = Get1IndType(type_index)
  33.         print '  Type %d: %s' % (type_index, res_type)
  34.         #TODO Can a resource type have a : in the filename? Because this will break then
  35.         type_folder = os.path.join(base_folder, res_type)
  36.         #TODO Should handle this already existing
  37.         os.mkdir(type_folder)
  38.        
  39.         res_count = Count1Resources(res_type)
  40.         print '  There are %d resources of this type' % res_count
  41.        
  42.         for res_index in range(1, res_count + 1):
  43.             res = Get1IndResource(res_type, res_index)
  44.             res.LoadResource()
  45.            
  46.             #2nd item in tuple is resource type which we already have
  47.             res_id, _junk, res_name = res.GetResInfo()
  48.             print '    Resource %d: id = %d, name = %s' % (res_index, res_id, res_name)
  49.             print '    size = %d' % res.size
  50.             print '    Size on disk: %d' % res.GetResourceSizeOnDisk()
  51.             #print '    data: %s' % res.data
  52.             if len(res_name) > 0:
  53.                 res_filename = '%d - %s' % (res_id, res_name.replace(':', '_'))
  54.             else:
  55.                 res_filename = str(res_id)
  56.            
  57.             #I guess filenames can only be 31 chars? *shrug*
  58.             res_filename = res_filename[:31]
  59.            
  60.             try:
  61.                 output = open(os.path.join(type_folder, res_filename), 'wb')
  62.                 output.write(res.data)
  63.                 output.close()
  64.             except:
  65.                 print 'WTF WHY I HATE YOU %s' % res_filename
  66.            
  67.             res.DetachResource()
  68.                
  69.     CloseResFile(res_file)
  70.    
  71. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement