noob question: aliases

hey, i’m working on a script which copies the contents of a selected folder to another location. when the folder contains an alias i would like to have the finder copy the original files/folders instead of the alias file. could someone help me out with this?

i’m an applescript newbie btw, did some years ago under system 7 but i forgot how it all worked.

I’m writing a solution. Just wanted to clear something up: is the selected folder being duplicated, such that we only need to resolve aliases in that one folder, or are you copying the contents of folder A to folder B, such that we need to worry about items in one folder having the same name as in the other folder?

The only thing that makes someone a “newbie” is when they put the word “newbie” in the subject line of their posting. We are all “students” of the AppleScript language. :slight_smile:

Here is solution 1:


--	Copy the items within folderA to folderB,
--	resolving alias files as we go.
--
set folderA to choose folder
set folderB to choose folder

CopyItemsResolvingAliases(folderA, folderB)

on CopyItemsResolvingAliases(fromFolder, toFolder)

	tell application "Finder"

		set fromFolder to folder (fromFolder as string)
		set toFolder to folder (toFolder as string)

		set everyItem to every item of fromFolder

		repeat with i from 1 to everyItem's length

			set thisItem to everyItem's item i

			--	This resolves an alias file to the thing it's pointing
			--	to. It does so repeatedly, such that an alias to an
			--	alias to an alias, etc., is handled.
			--
			repeat while (thisItem's class = alias file)

				set thisItem to thisItem's original item

			end repeat

			if (thisItem's class = folder) then

				--	If a folder of the same name does not already exist,
				--	then we create it:
				--
				if not (exists folder (thisItem's name) of toFolder) then

					make new folder at toFolder with properties ¬
						{name:thisItem's name as string}

					--	Note: the "as string" coercion above seems to be nessesary
					--	for some reason that I can't figure out. Probably a
					--	Unicode thing.

				end if

				--	Now we recurse:
				--
				my CopyItemsResolvingAliases(thisItem, folder (thisItem's name) of toFolder)

			else -- thisItem is some kind of file

				--	This will overwrite an item in the target folder
				--	that has the same name. Let me know if you need
				--	to avoid this.
				--
				duplicate thisItem to toFolder replacing (true)

			end if
		end repeat
	end tell
end CopyItemsResolvingAliases

Here is solution 2:


--	Given a folder:
--
set originalFolder to choose folder

--	We want to copy it to a new location,
--	and resolve any alias files within:
--
tell application "Finder" to set copiedFolder to duplicate originalFolder to (choose folder)

ResolveAliasFiles(copiedFolder)

on ResolveAliasFiles(thisFolder)

	tell application "Finder"

		set thisFolder to folder (thisFolder as string)

		set everyItem to every item of thisFolder

		repeat with i from 1 to everyItem's length

			set thisItem to item i of everyItem

			--	If we encounter an alias file, we want to
			--	replace it with its original item
			--
			if (thisItem's class = alias file) then

				--	Save the alias file to variable
				--
				set aliasFile to thisItem

				--	Resolve thisItem to its original item
				--
				repeat while (thisItem's class = alias file)

					set thisItem to thisItem's original item

				end repeat

				--	Replace
				--
				delete aliasFile
				set thisItem to duplicate thisItem to thisFolder

				--	Note: Because alias files do not have to have the same
				--	name as the thing they are aliasing, a name conflict is
				--	possible, ie: the original item may have the same name
				--	as some other item in thisFolder. Let me know if you need
				--	a solution to this.

			end if

			if (thisItem's class = folder) then

				--	Find nested alias files:
				--
				my ResolveAliasFiles(thisItem)
				
			end if
		end repeat
	end tell
end ResolveAliasFiles

thank you very much. just what i needed.

I know this topic is veeery old but I think the script is great (I am refering ot the second script in my case). It would be terribly useful to me if only it had one more little thing : giving the copies of the originals (that replace the aliases) the names of the aliases it replaces instead of the names of the originals.
I hope someone wil find a solution.
Thanks.

Hi. It just needs a couple of extra lines:


--	Given a folder:
--
set originalFolder to choose folder

--	We want to copy it to a new location,
--	and resolve any alias files within:
--
tell application "Finder" to set copiedFolder to duplicate originalFolder to (choose folder)

ResolveAliasFiles(copiedFolder)

on ResolveAliasFiles(thisFolder)
	
	tell application "Finder"
		
		set thisFolder to folder (thisFolder as string)
		
		set everyItem to every item of thisFolder
		
		repeat with i from 1 to everyItem's length
			
			set thisItem to item i of everyItem
			
			--	If we encounter an alias file, we want to
			--	replace it with its original item
			--
			if (thisItem's class = alias file) then
				
				--	Save the alias file to variable
				--
				set aliasFile to thisItem
				set aliasName to name of aliasFile -- Get the name of the alias file.
				
				--	Resolve thisItem to its original item
				--
				repeat while (thisItem's class = alias file)
					
					set thisItem to thisItem's original item
					
				end repeat
				
				--	Replace
				--
				delete aliasFile
				set thisItem to duplicate thisItem to thisFolder
				set name of thisItem to aliasName -- Set the name of the duplicated original to that of the alias file.
				
				--	Note: Because alias files do not have to have the same
				--	name as the thing they are aliasing, a name conflict is
				--	possible, ie: the original item may have the same name
				--	as some other item in thisFolder. Let me know if you need
				--	a solution to this.
				
			end if
			
			if (thisItem's class = folder) then
				
				--	Find nested alias files:
				--
				my ResolveAliasFiles(thisItem)
				
			end if
		end repeat
	end tell
end ResolveAliasFiles

Thank you for your answer.
There seems to be an error though :
I ran it on a folder containing multiple subfolders with aliases, one of the aliases having another name than its originla. I get a error telling me it is impossible to get the original file of a that particular alias.

Assuming the alias file was still attached to the original before you ran the script, the only thing I can think of is that the script might be deleting an intermediate alias file in a chain. This would theoretically cause the one in front of it to point at nothing. (It would happen both with Arthur’s original script and with my modifications.) But so far, I haven’t been able to create such a chain to test it. When I create an alias of an alias, it points directly to the original file.

I’ll have another look at more leisure this evening.

It is not an alias to an alias, just an alias to a file.
Also, the problem doesn’t appear in Arthur’s original script.