I have a Perl script that creates files, and I want to write into their resource forks, in order to specify the default application they should be opened with (I can't just change it using get information in Finder, it keeps reverting).
I could find out about Apple's arcane resource fork stuff, but for my purposes I'm happy to read the data from an existing resource fork that opens with the right application, and hard-code it into my script. The trouble is, I'm too new to Perl to know how to read the hex numbers from the file, copy and paste it into my script, and have it written correctly as hex into my new .rsrc
files.
I want to do something like:
my $theHex="DEADBEEF" #<---this is where I would paste the hex from the existing rsrc file
open (OUT, ">$filename/rsrc"); #<--this is the resource fork for file $filename
printf OUT "%x" $theHex #<----I'm not sure that this formatting is right
Edit: Yes I know, resource forks are deprecated. Please tell that to Apple so they can stop using them in their OS to determine things like what application opens a particular file. Sheesh!
You have a couple problems in that code. First of all, you need a comma after your format string:
printf OUT "%x", $theHex
Now that we have some syntactically valid source code, we can move on to your real problem. The %x
format string for printf
expects to be given an integer but you're giving it a non-numeric string. When the string value of $theHex
is converted to an integer, you'll get zero. The fix for this is easy, you just need to properly initialize $theHex
:
my $theHex = 0xDEADBEEF;
The "0x" prefix tells Perl that you're writing the number as hex; similarly, a zero prefix (e.g. 0666
) indicates octal.
Now you have a hexadecimal string representation of your 0xDEADBEEF integer.
If you just want to copy four bytes from one resource fork to another, then you're better off using read
to read four raw bytes and then use print
to write those bytes to the other resource fork.
You might want to have a look at pack
if you're slinging bags'o'bytes around. AFAIK, the resource fork wants four bytes rather than an eight byte human readable hexadecimal representation of a four byte integer.