lock and unlock files from script? - Mac options

-hh hh at livecode.org
Wed Feb 10 11:14:15 EST 2016

Stephen B. wrote:
> > I'd be happy with an Applescript solution or something using the
> > Shell. faster would be better. thanks for any help. stephen.
> thanks guys. sqb

TMHO, such threads should end -- if available -- with a full
working solution. Perhaps somebody can use this, for example
me, in half a year :-)

======= HowTo
Switch the "locked" flag (= the check in Finder's "Get info"-dialog
box) of a Mac OSX file.
Yo have to be superuser or the owner of this file for that.

[1] using applescript (see note at end of this post)

on mouseUp
   answer file "File to unlock" -- obtain the path/to/file
   if it is not empty then
      replace slash with colon in it
      delete char 1 of it --> delete leading colon <-- BN
      do switchLocked(it) as applescript
   end if
end mouseUp

function switchLocked ff
 return "" & \
    "tell application " &quote& "Finder" &quote &CR& \
    "  set p to alias " &quote& ff &quote &CR& \
    "  set locked of p to not (locked of p)" &CR& \
    "end tell"
end switchLocked

[2] using shell

on mouseUp
   answer file "File to unlock" -- obtain the path/to/file
   if it is not empty then
     put quote&it&quote into f1
     put word 5 of shell ("ls -lO " & f1) into myFlag
     --> myFlag is "-" if not locked and "uchg" if locked
     if myFlag is "uchg" then
        put "nouchg" into myFlag --> lock
     else put "uchg" into myFlag --> unlock
     put shell ("chflags" && myFlag && f1) into nirwana
     -- put shell ("ls -lO " & f1) --> check if it's done
  end if
end mouseUp

======= Note
Bernd N. and I had a dialog about the "execution error" of the
original script [1] above and he found the culprit:
If you give an alias that starts with a colon (or a POSIX path
that starts with a slash) then the Applescript Script Editor puts
"startup disk" in front of this and it's OK). We have to delete
the leading colon or put the diskName in front of the path).


More information about the use-livecode mailing list