Newbye - Lock and Unlock

Hi all!
Is this an open-minded forum which will kindly accept (pretty-STUPID) questions from a total newbye in applescripts, or must I live in the fear of what I’m gonna ask? :slight_smile:
Okay; here we go for a… test!

  • I’m trying to write a script that tells the finder to LOCK a document IF it’s UNLOCKED, or “else” UNLOCK it if it’s LOCKED.
    Here’s what I’ve tried:
tell application "Finder"
	set thePath to a reference to file "startup disk:Users:xxx:xxx:xxxx:xxx:NameoftheFile"
	if the lock of thePath is true then
		set the lock of thePath to false
		set the lock of thePath to true
	end if
end tell

But the resulting message sounds like this (I have to translate it from italian):
"Finder has founded an error: Impossible to get lock of file “startup disk:Users::xxx:xxx:xxxx:xxx:NameoftheFile”.

First of all, I would ask if the global structure is right or is a mess (as I think it is).
On a second instance I can’t find a possible definition for “lock” which Applescript or Finder would understand (if that is the problem).

TNX to all! :slight_smile:



Yes, the reference is wrong. Firstly, you probably won’t be using the ‘a reference to’ operator at this point. It is not used to coerce strings to references. Now, ‘startup disk’ is a property of finder and can’t be used in a string (within the quotation marks). If you want to get the startup disk and concatenate the rest of the path, then something like this will do:

tell application “Finder”
set sd to (startup disk) as string
set file_ref to (sd & “Users:xxx:xxx:xxxx:xxx:NameoftheFile”) as alias
end tell

Here, the variable sd is a string so you can concatenate the 2 strings. When you try to coerce the string to alias, if the file doesn’t exist, then you’ll get an error saying that the file couldn’t be found. Note that there is probably an easier way to get a path to your file then typing the path depending on where it is located.

The Finder property is ‘locked’ and not lock. You can find properties of Finder objects by opening the Finder dictionary. Here’s the correct usage:

– set file_ref to (choose file)
tell application “Finder”
set sd to (startup disk) as string
set file_ref to (sd & “Users:xxx:xxx:xxxx:xxx:NameoftheFile”) as alias
on error
beep 2
end try
if locked of file_ref then
set locked of file_ref to false
set locked of file_ref to true
end if
end tell

When I have to type in paths to files, I use a script to get the path instead of typing it in. Here’s a script that places the path in the clipboard:

property prompt0 : “Path to folder or file?”
property prompt1 : “Path to what folder?”
property prompt2 : “Path to what file?”
property button_list : {“Cancel”, “Folder”, “File”}

display dialog prompt0 buttons button_list default button “File”
set the_result to result
if button returned of the_result is “Folder” then
set item_path to (choose folder with prompt prompt1) as string
set item_path to (choose file with prompt prompt2) as string
end if
tell application “Finder”
set the clipboard to (“”" & item_path & “”")
end tell

I place this script in the Scripts Menu.


tell application "Finder"
	set myFile to file "Users:xxx:xxx:xxxx:xxx:NameoftheFile" of startup disk
	set locked of myFile to not locked of myFile
end tell

Hi hhas,

Thanks for the shortcut toggler.


I’m wondering what “xxx:xxx:xxx:” is. Maybe, you can use the ‘path to’ command to get the container of your file.


Geesh… okay; I think I understood the basic part of this (I’ll check back the “Apple Scripts 4 Absolute starters” later to get a deepest knowledge about “concatenating” and “coercing”).

I’ve been looking for it in the Finder Dictionary whith no result, before posting, and even now that you’ve told me; I can’t find it anywhere!
May I ask you where is it supposed to be, or if is there a “Search” function that scans the content of apps’ Dictionaries? (tnx)

Thanks! I’ll dare one more question… When you write “if locked of file_ref then” (10th string)… is it because you forget to put the “true” value, or if it’s because “true” is the default one?

Pretty Nice!
TNX ALOT!! :slight_smile:

TNX to you and hhas, I’ve successful compiled (“mixing” the twoes) the script, which now works like a charm!! :smiley:

tell application "Finder"
	set thePath to file "Users:(myUserName):Library:Mozilla:Profiles:ALFABreezE:izgczten.slt:MagicCookie"
of startup disk 
--used your 'path to' for getting the path! GREAT!!!
	if locked of thePath then
		set locked of thePath to false
		say "File is unlocked"
		set locked of thePath to true
		say "File is locked"
		select thePath
	end if
end tell

[Just in case you’re wonderin’ what I needed this script for, it’s for limiting the “all access” of cookies from Netscape only into the RAM, while having the file “MagicCookie” locked. But sometimes I need interesting cookies to be stored in that file (such automatic logging in this forum and so on), so I have to unlock it].

As you can see, I added a few lines such as “say 'File is unlocked/locked”.
And the “select thePath” (which is only temporary there just to be sure the file is locked again by viewing its “info”).

What if, in replace of "say “the file is unlock” I’d like to have a dialog window that shows me an alert message like “Hey! Take care; the file is UNLOCKED!” and a Button which would re-run the script (after the cookies I wanted to be stored have been stored) saying “LOCK it back” and a final alert which would tell “Relax, now; the file is LOCKED again!!”
(Little paranoic, am I not?) :smiley:

TNX again!

property statVar : {"Unlock", "Lock", "Unlock"}

tell application "Finder"
	set myFile to file "Users:jobu:Desktop:Test.txt" of startup disk
	set statIndex to ((locked of myFile) as integer) + 1
	set statCurr to (item statIndex of statVar)
	set statMod to (item (statIndex + 1) of statVar)
	set statMsg to ("The File is " & statCurr & "ed.") as string
end tell

display dialog statMsg buttons {"Cancel", statMod}
(* Anything below will execute if 'Cancel' is not selected *)
set locked of myFile to not locked of myFile
display dialog (("The File has been " & statMod & "ed.") as string) buttons {"OK"} default button {"OK"}


Works GREAT, thank you!
Now I only need to understand each step and command line.
See U in a couple of months :wink: