1

Testing the ability to write metadata to files in Mac OSX. Any help would be appreciated as I know little about unix syntax.

In Terminal. Why does this work;

xattr -w com.apple.metadata:kMDItemDescription 'Somedescription' /somePath/someFile.pdf

but this does not;

xattr -w com.apple.metadata:kMDItemDescription 'Some\ description' /somePath/someFile.pdf

Obviously I will sometimes need to write to the metadata tag as a string that includes spaces. I would have thought that escaping the whitespace would work.

user3118171
  • 357
  • 1
  • 2
  • 8
  • 1
    What constitutes "does not work"? Also, if you quote a string, you don't need to escape spaces. When using single quotes, everything from the opening quote until the next single quote is taken verbatim. In fact, you **can't** escape things. The backslashes are also taken verbatim. Which means you can't even escape a single quote to prevent it from ending the quoted portion. (You *can* escape a single quote *outside* of a quoted portion to prevent it from *starting* a new quoted portion, though.) – Ken Thomases Jan 01 '17 at 10:20
  • In other words I think Ken is saying "use double quotes"... – l'L'l Jan 01 '17 at 10:39
  • Thank you for the fast responses. – user3118171 Jan 01 '17 at 10:57
  • "does not work" = does not display kMDItemDescription at all using mdls to view the result. Tried double quotes with the same results. Thank you Ken for helping with your explanation but still cannot generate a result with any spaces. – user3118171 Jan 01 '17 at 11:10
  • I was not suggesting using double quotes. Using single quotes is fine and, in many ways, safer. I was just saying that, when you use single quotes, you shouldn't attempt to quote spaces or any other special characters. The thing to watch out for is if the text you want to quote itself contains a single quote. Anyway, this is beside the point. If you set a description with a space through the normal UI mechanism, how does it show when you read it? Also, there's no general guarantee that you **can** write such metadata using `xattr`. – Ken Thomases Jan 01 '17 at 14:38
  • 1
    OK figured it out (stumbled across it). For xattr you need a quote and a double quote to wrap a text string that includes spaces.
    xattr -w com.apple.metadata:kMDItemDescription '"Some description"' /somePath/someFile.pdf
    The order of the quote and double quote is not relevant. Tested with kMDItem + Description, Composer, Comment, Album, Authors, DisplayName, MusicalGenre, Title and Tempo.
    – user3118171 Jan 01 '17 at 23:56
  • @KenThomases why there isn't any guarantee that I will be able to write metadata with `xattr` ? How can I ensure that I am writing the metadata ? – mariano-daniel Nov 23 '22 at 01:48

0 Answers0