Works As a Single Command, but Not Within a Loop

I’m completely stumped by this. I have a simple script which does a CP shell command to copy the dock prefs from one user to another, however, it fails to work if I run it within a loop. It’s driving me nuts. Here’s my code:


(*
--Will work when outside of the loop...

set thePassword to "some_pass"
set thisUser to "someUser"
do shell script "cp /Users/admin/Library/Preferences/com.apple.dock.plist /Users/" & thisUser & "/Library/Preferences/" password thePassword with administrator privileges
*)

set userDir to path to users folder
set theUsers to list folder userDir without invisibles
set tempList to {}

--Get password
display dialog "Password: " default answer ""
set thePassword to the text returned of the result

--Remove "admin" & "Shared" from the list:  Let me know if there's a more direct way of doing this.
repeat with i in theUsers
	if (i as text is not "admin" and (i as text is not "Shared")) then
		set tempList to tempList & i
	end if
end repeat

--Set theUsers to correct list
set theUsers to tempList

try
	--Copy the files
	repeat with thisUser in theUsers
		do shell script "cp /Users/admin/Library/Preferences/com.apple.dock.plist /Users/" & thisUser & "/Library/Preferences/" password thePassword with administrator privileges
	end repeat
	
on error err -- Something bad happened
	display dialog err
end try

Any hints?

Thanks,
-Rob

The only thing I can think of is that on my machine, ‘list folder (path to users folder) without invisibles’ nevertheless picks up on a file called “.localized”, which is invisible to the Finder. This would cause your loop to error if not caught.

set theUsers to list folder (path to users folder) without invisibles

--Get password
display dialog "Password: " default answer ""
set thePassword to the text returned of the result

try
  -- Copy the files unless the "user name" is "admin", "Shared", or ".localized"
  repeat with thisUser in theUsers
    if thisUser is not in "admin Shared .localized" then
      do shell script "cp /Users/admin/Library/Preferences/com.apple.dock.plist /Users/" & thisUser & "/Library/Preferences/" password thePassword with administrator privileges
    end if
  end repeat
  
on error err -- Something bad happened
  display dialog err
end try

Hi,

Often when using ‘repeat with’ the variable will be a reference to a list item. For instance, the following script will work only if you use ‘contents of therepeatvariable’:

set the_list to {“a”, “b”, “c”}
repeat with this_item in the_list
if (contents of this_item) is “b” then
beep 2
exit repeat
end if
end repeat
this_item

The result of this_item is a reference to the list item “b”.

gl,

Remove the ‘try/end try’ statements around the loop. That way any error message will get reported and you’ll see why it’s not working.

As useful as try/end try blocks are for production scripts, they really screw up debugging. :slight_smile:

Thanks for all the help. I have tomorrow off, so I’ll give 'em a try on Friday and let you know what’s up.

Regards,
-Rob

Whelp, I finally got the script to work, except, I’ve come into a bit of a problem. The script works perfect, so long as I am in AppleScript and click the run button. If I save the script as an application, it no longer works. In fact, it causes all the accounts to revert back to the default OS X dock.

Any ideas why it would do this?

Here’s my code:


-- Change all docks, or just from one to another?
display dialog "Do you wish to copy dock preferences from Admin to all users?" buttons {"Yes", "No", "Cancel"} default button "No"

if button returned of result is "Yes" then
	-- Get password
	display dialog "Password: " default answer ""
	set thePassword to the text returned of the result
	
	-- Set up source directory path
	set userDir to path to users folder
	set theUsers to list folder userDir without invisibles
	
	try -- Try to copy the files
		repeat with thisUser in theUsers
			if (thisUser as text is not in "admin Shared .localized") then
				do shell script "cp /Users/admin/Library/Preferences/com.apple.dock.plist /Users/" & thisUser & "/Library/Preferences/com.apple.dock.plist" password thePassword with administrator privileges
			end if
		end repeat
		
		display dialog "All done."
	on error err -- Something bad happened
		display dialog err
	end try
else
	-- Get password
	display dialog "Password: " default answer ""
	set thePassword to the text returned of the result
	
	-- Copy which dock pref?
	set srcFolder to choose folder with prompt "Select a user folder that contains the source dock prefs (com.apple.dock.plist):" as string
	
	-- To which user?
	set tgtFolder to choose folder with prompt "Select a user folder to copy the dock prefs to:" as string
	
	try -- Try to copy from one user to the other.
		do shell script "cp " & (quoted form of POSIX path of srcFolder) & "Library/Preferences/com.apple.dock.plist " & (quoted form of POSIX path of tgtFolder) & "Library/Preferences/com.apple.dock.plist" password thePassword with administrator privileges
		
		display dialog "All done."
	on error err -- Something bad happened...
		display dialog err
	end try
end if