Can I lock Desktop icons? Dock?

c23roo

macrumors regular
Original poster
May 29, 2004
148
0
Does anyone know if there's a way to lock my desktop icons (so my curious kids don't mistakenly - or not so mistakenly - move stuff around or into trash). My desktop is kept fairly simple (icons for the various HDs and folders for homework). We've also had the issue of items going *poof* from the dock - which is an easily fixable issue - but a little bit of a nuisance to have to retrieve from time to time.
Thanks!
 

Jolly Jimmy

macrumors 65816
Dec 13, 2007
1,357
3
I use this little script I made to lock/unlock the dock's icons and size.

Code:
set LockedContents to do shell script "defaults read com.apple.dock contents-immutable"
set LockedSize to do shell script "defaults read com.apple.Dock size-immutable"

if LockedContents = "1" and LockedSize = "1" then
	set ContentsLock to "defaults write com.apple.dock contents-immutable -bool false"
	set SizeLock to "defaults write com.apple.Dock size-immutable -bool no"
	display dialog "Press OK to unlock the dock"
else
	set ContentsLock to "defaults write com.apple.dock contents-immutable -bool true"
	set SizeLock to "defaults write com.apple.Dock size-immutable -bool yes"
	display dialog "Press OK to lock the dock"
end if

try
	do shell script ContentsLock
	do shell script SizeLock
	delay 0.5
	do shell script "KillAll Dock"
end try
Open it in Applescript Editor and save it as an application.
 

c23roo

macrumors regular
Original poster
May 29, 2004
148
0
Thanks for the help!
Note to self: must remember to try drinking in the morning again.

Cheers!
 

rdforbes

macrumors newbie
Sep 23, 2010
9
0
I use this little script I made to lock/unlock the dock's icons and size....Open it in Applescript Editor and save it as an application.
Hi - I'm new to Applescripts, so forgive me if this is something obvious. I copy/pasted your script into the AppleScript Editor, but when I ran it I got an error on the first line:

error "2010-09-23 23:52:26.489 defaults[6758:60f]
The domain/default pair of (com.apple.dock, contents-immutable) does not exist" number 1

Any ideas of what I did wrong?
Thanks,
Ron
 

Jolly Jimmy

macrumors 65816
Dec 13, 2007
1,357
3
10.6.4. I just wish I knew more about AppleScript to be able to interpret the error message.
I'm still learning applescript. Try this out :

Code:
try
	set LockedContents to do shell script "defaults read com.apple.dock contents-immutable"
on error
	do shell script "defaults write com.apple.dock contents-immutable -bool false"
	set LockedContents to do shell script "defaults read com.apple.dock contents-immutable"
end try

try
	set LockedSize to do shell script "defaults read com.apple.Dock size-immutable"
on error
	do shell script "defaults write com.apple.Dock size-immutable -bool no"
	set LockedSize to do shell script "defaults read com.apple.Dock size-immutable"
end try

if LockedContents = "1" and LockedSize = "1" then
	set ContentsLock to "defaults write com.apple.dock contents-immutable -bool false"
	set SizeLock to "defaults write com.apple.Dock size-immutable -bool no"
	display dialog "Press OK to unlock the dock"
else
	set ContentsLock to "defaults write com.apple.dock contents-immutable -bool true"
	set SizeLock to "defaults write com.apple.Dock size-immutable -bool yes"
	display dialog "Press OK to lock the dock"
end if

try
	do shell script ContentsLock
	do shell script SizeLock
	delay 0.5
	do shell script "KillAll Dock"
end try
 

calderone

macrumors 68040
Aug 28, 2009
3,679
86
Seattle
That script could be shortened and user interaction can be removed. And if you are going to prompt the user for action, might as well give the choice in case (for some reason) they don't know the current status of the dock.

Automated, based on the current value
Code:
-- Get the current values
try
	set lockedContents to do shell script "defaults read com.apple.dock contents-immutable"
	set lockedSize to do shell script "defaults read com.apple.Dock size-immutable"
on error
	-- Couldn't grab current status, let's assume a lock
	set toggleValue to true
end try

-- Set the value based on the current
if lockedContents = "1" and lockedSize = "1" then
	set toggleValue to false
else
	set toggleValue to true
end if

-- Lock or Unlock the Dock
toggleDockLock(toggleValue)

------------------------------------------
-- Functions --
------------------------------------------
to toggleDockLock(toggleValue)
	set contentsLock to "defaults write com.apple.dock contents-immutable -bool " & toggleValue
	set sizeLock to "defaults write com.apple.Dock size-immutable -bool " & toggleValue
	
	-- Perform the requested action
        try
	     do shell script contentsLock
	     do shell script sizeLock
	     do shell script "killall Dock"
       end try
end toggleDockLock
Let the user decide
Code:
-- Ask the user if they want to Lock or Unlock the Dock
set userPrompt to display dialog "Do you want to Lock or Unlock the Dock?" buttons {"Lock", "Unlock", "Cancel"} default button "Lock"
set userAction to button returned of userPrompt

-- Turn the user request into a useable value
if userAction is "Lock" then
	set toggleValue to true
else if userAction is "Unlock" then
	set toggleValue to false
end if

-- Perform the requested action
toggleDockLock(toggleValue)

------------------------------------------
-- Functions --
------------------------------------------
to toggleDockLock(toggleValue)
	set contentsLock to "defaults write com.apple.dock contents-immutable -bool " & toggleValue
	set sizeLock to "defaults write com.apple.Dock size-immutable -bool " & toggleValue
	
	-- Perform the requested action
        try
	     do shell script contentsLock
	     do shell script sizeLock
	     do shell script "killall Dock"
        end try
end toggleDockLock
 

Jolly Jimmy

macrumors 65816
Dec 13, 2007
1,357
3
I originally wrote the script with a choice of lock/unlock and cancel but found it pretty pointless seeing as it's really just an ON/OFF switch. Also, it doesn't give you any indication as to the current state and you can click something and have it do nothing. The prompt for the toggle is of course optional but at least you know exactly what's going on and you have the chance to cancel.
 

calderone

macrumors 68040
Aug 28, 2009
3,679
86
Seattle
I originally wrote the script with a choice of lock/unlock and cancel but found it pretty pointless seeing as it's really just an ON/OFF switch. Also, it doesn't give you any indication as to the current state and you can click something and have it do nothing. The prompt for the toggle is of course optional but at least you know exactly what's going on and you have the chance to cancel.
My point was that you were having the user click "Ok" to perform the action, but there really wasn't a choice. It was pointless to involve the user when you were just performing the opposite action of the current state.

In regards to it not giving the current state, the user should know what the current state is. It isn't that the script does nothing, it does exactly what the user intends, which may not make a change.

I added status to this one.

Code:
(*

	Dock Size and Contents Lock/Unlock Toggle
	
*)

-- Get the current status
set contentsLockMessage to getCurrentDockContentStatus()
set sizeLockMessage to getCurrentDockSizeStatus()

-- Ask the user if they want to Lock or Unlock the Dock
set userPrompt to (display dialog "Do you want to Lock or Unlock the Dock?" & "
" & "
" & "Current Status:" & space & contentsLockMessage & "," & space & sizeLockMessage buttons {"Lock", "Unlock", "Cancel"} default button "Lock" with title "Dock Lock/Unlock Toggle")
set userAction to button returned of userPrompt

-- Turn the user request into a useable value
if userAction is "Lock" then
	set toggleValue to true
else if userAction is "Unlock" then
	set toggleValue to false
end if

-- Perform the requested action
toggleDockLock(toggleValue)

------------------------------------------
-- Functions --
------------------------------------------
to toggleDockLock(toggleValue)
	set contentsLock to "defaults write com.apple.dock contents-immutable -bool " & toggleValue
	set sizeLock to "defaults write com.apple.Dock size-immutable -bool " & toggleValue
	
	-- Perform the requested action
	do shell script contentsLock
	do shell script sizeLock
	do shell script "killall Dock"
end toggleDockLock

to getCurrentDockContentStatus()
	try
		set contentsLockStatus to do shell script "defaults read com.apple.dock contents-immutable"
	on error
		set contentsLockStatus to ""
	end try
	
	if contentsLockStatus = "0" or contentsLockStatus = "" then
		set contentsLockMessage to "Contents unlocked"
	else if contentsLockStatus = "1" then
		set contentsLockMessage to "Contents locked"
		
	end if
	
	return contentsLockMessage
	
end getCurrentDockContentStatus

to getCurrentDockSizeStatus()
	try
		set sizeLockStatus to do shell script "defaults read com.apple.Dock size-immutable"
	on error
		set sizeLockStatus to ""
	end try
	
	if sizeLockStatus = "0" or sizeLockStatus = "" then
		set sizeLockMessage to "Size unlocked"
	else if sizeLockStatus = "1" then
		set sizeLockMessage to "Size locked"
	end if
	
	return sizeLockMessage
end getCurrentDockSizeStatus
 

Jolly Jimmy

macrumors 65816
Dec 13, 2007
1,357
3
Since when is providing user feedback pointless? Want to know what is? Buttons that do nothing. There's no need to over complicate things.