How to access the "comment" property of a file?

Deploying to Mac OS? Ask Mac OS specific questions here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

How to access the "comment" property of a file?

Post by sritcp » Sun Aug 21, 2016 1:19 pm

I need to access the "comment" field of a file from within LC.
(This is the field that is displayed when you hit Command-I to display the information panel of a file -- where you can type in descriptive information).
Is there a simple way to read in / write out to this file property?
I know very little about working with OS elements, so please provide an example.

Thank you for any help,
Sri

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: How to access the "comment" property of a file?

Post by Klaus » Sun Aug 21, 2016 4:42 pm

Hi Sri,

you can use MDLS with SHELL to get these "Finder" infos.
For the comment use something like htis:

Code: Select all

on mouseUp
   answer file "What Finder comment shall I display?"
   put it into tFile
   put shell("mdls -name kMDItemFinderComment" && QUOTE & tFile & QUOTE)
end mouseUp
Run this code and see what it will return.
Very easy to parse the result!

Try this to see what other infos the MDLS shell command can return about a file:

Code: Select all

on mouseUp
   answer file "sdsdsd"
   put it into tFile
   put shell("mdls" && QUOTE & tFile & QUOTE)
end mouseUp
Best

Klaus

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How to access the "comment" property of a file?

Post by sritcp » Sun Aug 21, 2016 7:13 pm

Hi Klaus:

Thank you so much!
Your code worked (as it always does!).

Now, how would I save an edited "comment" back into the file data?

Thanks,
Sri

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: How to access the "comment" property of a file?

Post by [-hh] » Tue Aug 23, 2016 10:24 am

Yet another option is to use applescript, works here (tested) in LC 6/7/8.

Code: Select all

-- Use Finder's "Get info" box in MacOS
-- Get and set "Comments" via applescript

on setComment fPath,fComment
  do "tell application " & qq("Finder") &cr& \
        "set fn to POSIX file " & qq(fPath) &cr& \
        "set comment of (fn as alias) to " & qq(fComment) &cr& \
        "end tell" as applescript
end setComment

function getComment fPath
  do "tell application " & qq("Finder") &cr& \
        "set fn to POSIX file " & qq(fPath) &cr& \
        "set finfo to comment of (fn as alias)" &cr& \
        "end tell" as applescript
  return the result
end getComment

-- usage example
on mouseUp
  answer file "Give me a path."
  if it is empty then exit mouseUp
  put it into fPath
  -- use \n instead of cr
  put (the internet date) & "\nThe matrix has you." into fComment
  setComment fPath,fComment
  put getComment(fPath) into rslt
  put "FILE = " & qq(fPath) &cr& "COMMENT = " & rslt into fld "info"
end mouseUp

function qq strng
  return quote&strng&quote
end qq
shiftLock happens

sritcp
Posts: 431
Joined: Tue Jun 05, 2012 5:38 pm
Location: Alexandria, Virginia

Re: How to access the "comment" property of a file?

Post by sritcp » Thu Oct 06, 2016 8:11 pm

Thank you so much, Hermann!
I apologize for the delay in acknowledging your post.
I was out of circulation for a while.
Just today I was able to run your code and check it out. Just what I needed!

Regards,
Sri

Post Reply

Return to “Mac OS”