pulling out hair over conditional loop

again like it says on the tin I’m working on a cleanup script for my mac lab. I basically have a hidden folder of alises that are checked against whats on the desktop. If any thing is missing, it is copied over to the user’s desktop and if there is any clutter - it will get moved off the desktop onto the documents folder (see note it code).

I believe Im quite close - if only I could get the repeat with loop to function with a if else clause inside it - I just cant get it to function right -

can anybody see what Im doing wrong???

-- get current user id
tell application "System Events"
	set uD to (name of current user)
end tell

-- location of folder with must have desktop items
set includeFolder to ("/Users/" & uD & "/Desktop/safelist" as POSIX file)

-- List of files not to be moved off desktop
property exclusionList : {}

-- Get the list of items
tell application "Finder"
	set myfilenames to (get name of items of folder includeFolder)
	set exclusionList to myfilenames
end tell

-- test if above files exisis on desktop  
tell application "Finder"
	--set itemList to every item in exclusionList
	repeat with currentItem in exclusionList
		if exists file (("/Users/" & uD & "/Desktop/" & currentItem) as POSIX file) then
			return currentItem & " is already there"
		else
			-- copy file from safe list folder
			duplicate (("/Users/" & uD & "/Desktop/safelist/" & currentItem) as POSIX file) to desktop
			return "we've copied " & currentItem & " to desktop " & exclusionList
		end if
	end repeat
	-- go on to move everything not in the exclusionList to a dated folder in Documemts
	--http://macscripter.net/viewtopic.php?id=38016
end tell


Hi,

the problem are the return statements, which causes the script to abort.

Try this


-- location of folder with must have desktop items
set includeFolder to ((path to desktop as text) & "safelist:")

-- Get the list of items
tell application "Finder"
	set exclusionList to items of folder includeFolder
end tell

-- test if above files exisis on desktop  
tell application "Finder"
	--set itemList to every item in exclusionList
	repeat with currentItem in exclusionList
		if not (exists file (name of currentItem)) then -- the root folder of the Finder is the current desktop
			-- copy file from safe list folder
			duplicate currentItem to desktop
		end if
	end repeat
	-- go on to move everything not in the exclusionList to a dated folder in Documemts
	--http://macscripter.net/viewtopic.php?id=38016
end tell

Ah ha! - Stefan - again thank you.

Here’s what I’ve arrived at.
I’ve combined two elements I’ve been working on ( http://macscripter.net/viewtopic.php?id=38016) into one.
You’ll note that the safelist folder isn’t really on the desktop - I had thought I could simply make it hidden folder but realise its not possible without introducing a bunch of bash - and didn’t want to quite get into that (yet).

So I reintroduced current user / POSIX path -but can you tell me why ~/safelist dosen’t work?
I also added a delay to the duplicate loop as sometimes the icons item’s icons weren’t copying over.

So while this works fine for me - any refinements or better approaches would be very beneficial as I obviously have quite a bit to learn.

Thanks for taking a peak…


property exclusionListnames : {}
-- get current user id
tell application "System Events"
	set uD to (name of current user) as text
end tell

-- location of folder with must have desktop items
set includeFolder to ("/Users/" & uD & "/safelist" as POSIX file) -- why won't ~/safelist work?

-- Get the list of items
tell application "Finder"
	set exclusionList to items of folder includeFolder
	set exclusionListnames to (get name of items of folder includeFolder)
end tell

-- test if above files exisis on desktop 
tell application "Finder"
	--set itemList to every item in exclusionList
	repeat with currentItem in exclusionList
		if not (exists file (name of currentItem)) then -- the root folder of the Finder is the current desktop
			-- copy file from safe list folder
			duplicate currentItem to desktop
			delay 0.5 -- alow icons to copy over
		end if
	end repeat
	
	set itemList to count (get items of desktop whose class is not disk and name is in exclusionListnames)
	set allFiles to count (get items of desktop whose class is not disk)
	
	if itemList < allFiles then
		set foldername to ("Desktop " & ((do shell script "date '+%Y%m%d%H%M%S'") as text))
		set currentCleanupFolder to (path to documents folder as text) & foldername
		do shell script "/bin/mkdir -p " & quoted form of POSIX path of currentCleanupFolder
		set desktopclutter to (get items of desktop whose class is not disk and name is not in exclusionListnames)
		move (get items of desktop whose class is not disk and name is not in exclusionListnames) to folder currentCleanupFolder
	end if
	
	-- does this work in 10.4.11?
	set arrangement of icon view options of window of desktop to arranged by kind
end tell

the primary path specifier of AppleScript is HFS (colon separated), it doesn’t work with POSIX paths


set includeFolder to ((path to home folder as text) & "safelist:")

path to home folder points to the home folder of the current user, so the System Events part is not necessary