grabbing and decoding resources (was: Hex > Text)
grabbing and decoding resources (was: Hex > Text)
- Subject: grabbing and decoding resources (was: Hex > Text)
- From: Steven Majewski <email@hidden>
- Date: Wed, 17 Sep 2003 15:04:46 -0400
On Wednesday, September 17, 2003, at 12:55 AM, Walter Ian Kaye wrote:
>
At 11:15p -0400 09/16/2003, Rob Jorgensen didst inscribe upon an
>
electronic papyrus:
>
>
> At 5:21 PM -0700 9/16/03, Walter Ian Kaye wrote:
>
>> At 07:21p -0400 09/16/2003, Rob Jorgensen didst inscribe upon an
>
>> electronic papyrus:
>
>>
>
>>> I have, in hex, a "drag" resource from a clipping file. Is it
>
>>> possible to convert the hex to text via the shell or vanilla
>
>>> AppleScript? If so, sample code would be greatly appreciated as my
>
>>> search has yielded nothing useful.
>
>>
>
>> To extract from a resource, you need something beyond vanilla AS.
>
>>
>
>> There are a couple of ways to go. You could extract the raw resource
>
>> fork via a shell trick, but you'd have to parse the resource map
>
>> before parsing the data buried within. Or you could use the Extra
>
>> Suites app which has resource i/o commands.
>
>
>
> I've already used Extra Suites to read the resource and convert it to
>
> text but I was hoping for a vanilla/shell alternative. Thanks for
>
> trying! :-)
>
>
Well, there is code for parsing the resource map, but it's in a
>
language that doesn't yet exist on OS X and would have to be ported to
>
something like Perl.
>
What languages do you know? :) I could ask the author to port it, but
>
I don't know if he has the time. ::looking at code:: Dang, lots of
>
math and boolean transforms an' stuff (but nothing that couldn't be
>
done in perl).
The original poster has an easier answer to his problem, but you can
easily do this sort
of thing in Python. ( However, not the version that comes shipped in
Jaguar -- you need
to download a newer version ( 2.3.* ) from www.python.org, unless you
have Panther
which should come shipped with the Python that includes all of the mac
modules. )
Since I was getting frustrated using DeRez -- usually, what I wanted
was just to grab some
TEXT or STR resources and DeRez outputs the text as munged up comments
-- I decided
to play around with the Python's Carbon.Res module.
Here's a short version just to do the hex dump, followed by a longer
version with
some debugging output and some other resource calls + interactive
dialogs as an
alternative to command line args. ( Even the command line only version
has to run
from 'pythonw' and not 'python' -- I expect that the vanilla python
may not be able
to InitResources() )
-- Steve Majewski
#!/usr/local/bin/pythonw
from Carbon import Res,File
import sys
fs = File.FSSpec( sys.argv[1] )
fr = Res.FSpOpenResFile( fs, 0 ) # Open a resource file...
Res.UseResFile( fr )
if sys.argv[2:]: # second optional arg is resType
resType = sys.argv[2]
else:
resType = Res.Get1IndType( 1 ) # first res type is default
if resType.isdigit():
resType = Res.Get1IndType( int(resType) )
r = Res.Get1IndResource( resType, 1 )
sep = ' ' # separator character -- set to space or comma or whatever
print sep.join( [ hex(ord(x)) for x in r.data ] ) # and print out hex
repr
#------
#!/usr/local/bin/pythonw
from Carbon import Res,File
from EasyDialogs import AskFileForOpen, AskString
import sys
if sys.argv[1:]: # first optional arg is filename
fs = File.FSSpec( sys.argv[1] )
else:
fs = File.FSSpec( AskFileForOpen() )
print fs
fr = Res.FSpOpenResFile( fs, 0 ) # Open a resource file...
print 'reserror:',Res.ResError()
print fr
Res.UseResFile( fr )
print 'reserror:',Res.ResError()
ntypes = Res.Count1Types() # get the number of different types
print ntypes, "Resources in", fs
for i in range( 1, ntypes+1 ): # of resources and print them
print '\t', i, '\t',Res.Get1IndType( i )
defType = Res.Get1IndType( 1 ) # first res type is default for dialog
if sys.argv[2:]: # second optional arg is resType
resType = sys.argv[2]
else: # prompt if none supplied on cmdline
resType = AskString( 'Resource Type', defType)
if resType.isdigit():
resType = Res.Get1IndType( int(resType) )
print repr(resType)
print type(resType)
Res.UseResFile( fr ) # when run from pythonw, it seems that EasyDialog
# calls might be changing the current resource file
# That is something that is always a possibility
# but for some reason, it doesn't happen to me
# when in the IDE. ( If you give the args on the
# command line, it doesn't happen because the
# dialog is never called. )
r = Res.Get1IndResource( resType, 1 )
print r.data
sep = ' ' # separator character -- set to space or comma or whatever
print sep.join( [ hex(ord(x)) for x in r.data ] ) # and print out hex
repr
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.