Removing Aliases from a List of Folder + Files

A while back ago, I needed help getting a list of folders and files in Leopard (what used to be “items” in Tiger). And chrys was a big help.

Well I hit a snag…I need to filter-out aliases from that list because my script is doing date checks, moves, and deletions and is choking on the alias references. Here’s the existing list-getting handlers:

-- Get a file/folder list of all items at a certain level inside a given folder
--
-- with help from chrys of MacScripter
-- http://bbs.applescript.net/viewtopic.php?pid=91191#p91191
--
on listGetter(folder_to_scan, scan_level, folder_exceptions)
	--exceptions formatted for shell find
	copy folder_exceptions to folder_exceptions
	repeat with fe_ref in folder_exceptions
		set contents of fe_ref to quoted form of contents of fe_ref
	end repeat
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " -or -name "
	set exclude_code to text 6 thru -1 of ("" & ({""} & folder_exceptions))
	set AppleScript's text item delimiters to ASTID
	--do shell find with exceptions
	do shell script "/usr/bin/find " & (quoted form of POSIX path of folder_to_scan) & " ! \\( \\( " & exclude_code & " \\) -prune \\) -maxdepth " & scan_level & " -mindepth " & scan_level & " -print0 ; true" without altering line endings
	set find0 to result
	set {ASTID, text item delimiters} to {text item delimiters, {ASCII character 0}}
	try
		set POSIX_pathnames to text items 1 through -2 of find0 -- Drop the last text item because it is always empty (find -print0 always prints a trailing null).
		set text item delimiters to ASTID
	on error m number n from o partial result r to t
		set text item delimiters to ASTID
		error m number n from o partial result r to t
	end try
	script speedHack
		property Mac_pathnames : {}
	end script
	repeat with P_pn in POSIX_pathnames
		set end of speedHack's Mac_pathnames to (POSIX file (contents of P_pn)) as text
	end repeat
	speedHack's Mac_pathnames
end listGetter


--get user folders that aren't empty
--
on nonEmptyFolders(directory_to_filter)
	set filtered_folder_list to {}
	
	repeat with e from 1 to (number of items in directory_to_filter)
		tell application "Finder"
			set folder_to_check to (item e of directory_to_filter) as alias
			set item_contents to (number of items in folder folder_to_check)
			if number of items in folder folder_to_check > 0 then
				set filtered_folder_list to filtered_folder_list & folder_to_check
			end if
		end tell
	end repeat
	
	return filtered_folder_list
end nonEmptyFolders

Hi,

replace


repeat with P_pn in POSIX_pathnames
       set end of speedHack's Mac_pathnames to (POSIX file (contents of P_pn)) as text
end repeat

with


repeat with P_pn in POSIX_pathnames
	set hfsPath to (POSIX file (contents of P_pn)) as text
	tell application "Finder" to set isAliasFile to class of item hfsPath is alias file
	if not isAliasFile then set end of speedHack's Mac_pathnames to hfsPath
end repeat

THANKS! Wasn’t sure of the syntax for “detecting” an alias. Worked like a charm. :slight_smile:

My cleanup script hasn’t been running for about a month…going to be alot of cranky, disgruntled users after this morning’s fix of the script. evil grin