This is not Finder’s “lock” function, but similar to Perl’s “lock”. Basically, this “lock” handler will lock a file so nobody can write to it (the simple trick is keep it opened for access). And “unlock” will unlock a file locked with “lock”.
OS version: OS X
(*
lock
Locks a file, so nobody can write to it (returns integer, which you can use yourself to close -"unlock"- the file later, or just use the companion handler "unlock").
You can use also this number to write yourself to the file, as in:
write "foo" to returnedNumber
Changes are only persistent in the current session. After a restart, files will be "unlocked" magically.
The reference number is stored in the /tmp directory, as file name "lock" + inode number of the related file
Parameters:
f: file path, alias, posix path to be locked
Example:
lock("path:to:file.txt") --> 514
*)
to lock(f) --> from jfileLib, inside kapullo's scripts, Pescados Software
set f to f as Unicode text
if f does not contain ":" then set f to POSIX file f as Unicode text
set f to alias f
if ((f as text) ends with ":") then error "Can't lock a folder"
set tname to "lock" & (do shell script "find " & quoted form of POSIX path of f & " -prune -ls | awk '{print $1}'")
--> tname = "lock" + inode number
set q to (open for access f with write permission)
do shell script "echo -n " & quoted form of (q as text) & " > /tmp/" & tname
q
end lock
(*
unlock
Unlocks a file locked with "lock".
Parameters:
f: file path, alias, posix path to be unlocked
or integer returned from a "lock" operation
Example:
unlock("path:to:file.txt")
set refNumber to lock("path:to:file.txt")
delay 5 --> do something here
unlock(refNumber)
*)
to unlock(f) --> from jfileLib, inside kapullo's scripts, Pescados Software
if class of f is integer then
try
close access f
return
on error
error "File was not locked or was already unlocked"
end try
end if
set f to f as Unicode text
if f does not contain ":" then set f to POSIX file f as Unicode text
set f to alias f
if ((f as text) ends with ":") then error "Can't unlock a folder"
set tname to "lock" & (do shell script "find " & quoted form of POSIX path of f & " -prune -ls | awk '{print $1}'")
--> tname = "lock" + inode number
try
do shell script "cat /tmp/" & tname
close access (result as integer)
on error
error "File was not locked or was already unlocked"
end try
end unlock