1

I'm trying to extract the icon from an xattr of a file. Using xattr to get the "com.apple.ResourceFork" in hex format i use:

xattr -px com.apple.ResourceFork file

and save the output to a variable

var="$(xattr -px com.apple.ResourceFork file)"

then using variable expansion i remove the first bytes until i reach 69 (icns magic number is 69 63 6E 73)

var=${var#*69 63 6E 73}

next i output the variable and append "69 63 6E 73" to the beginning to restore the magic number.

echo "69 63 6E 73$var" > output.txt

if i take the hex data from the output.txt and insert it into a hexeditor to save it then it works and the .icns is created.

i want to do this programmatically in bash/zsh without having to save it manually.

tried using

touch icon.icns

to create an empty file then

 echo "69 63 6E 73$var" > icon.icns

just transforms the output file into an ASCII file.

i'm not stuck to my method, any working method is acceptable to me.

asamahy
  • 15
  • 4
  • I don't have access to a Mac at the moment, so I can't see what your Resource Fork looks like, but you probably want a command containing `xxd` like `echo "YOURSTRING" | xxd -rp > icon.icns` Try running `man xxd` to see manual pages. – Mark Setchell Aug 14 '22 at 22:39
  • piping to xxd doesnt work for some reason. piping to xxd -r just creates a file with 270 bytes of gibberish data. my resource fork containg exactly 260 bytes+icns as explained here: https://superuser.com/a/298798 – asamahy Aug 15 '22 at 02:53
  • found a solution to my problem (exporting an icon from the resource fork) using an external tool https://github.com/fuzziqersoftware/resource_dasm . though i'd rather find one not relying on external tool – asamahy Aug 15 '22 at 03:26

1 Answers1

1

I have access to my Mac again... strangely (to me) it seems xxd works differently when given parameters all together rather than individually, so rather than what I suggested in the comments:

xxd -rp ...

you would need:

xxd -r -p ...

As I don't have an icon.icns file to hand, I'll take a JPEG (which is just as binary), convert it to readable hex and reconstruct it from the hex with xxd.

Here's a JPEG, converted to hex:

xxd x.jpg | more

00000000: ffd8 ffe0 0010 4a46 4946 0001 0100 0001  ......JFIF......
00000010: 0001 0000 ffdb 0043 0003 0202 0202 0203  .......C........
...
...

Then take the hex and give reconstruct the first few bytes of the JPEG:

printf "ff d8 ff e0" | xxd -r -p > recreated.jpg

And look at the recreated file:

xxd recreated.jpg 

00000000: ffd8 ffe0

So the process for a while file would be:

hex=$(xxd -p x.jpg)
printf "$hex" | xxd -r -p > recreated.jpg  
Mark Setchell
  • 191,897
  • 31
  • 273
  • 432
  • Yea splitting the xxd command solved it. I’m also tempted to try to duplicate the resource fork instead of trying extract and reapply from the example in the xattr man page which didn’t seem to work with combined arguments. – asamahy Aug 17 '22 at 14:16