Alias script concept

Hi Everyone,

This is my first post here. :slight_smile:

Would something like the following be possible with an AppleScript on a Finder window selection?

• Manually search through the Finder for terms using “name matches”
• Manually select from returned search items
• The manual selection includes 1 folder and various # of aliases with varying names
• The script then is executed by the user, using the above manual selection
• The script mimics setting the “Select New Original” path for all aliases to the singular selected folder path, or re-creates aliases at their current location and with same name and pointing to the singular folder in selection.
• Repeat the entire process as needed

My problem is I have many aliases that need re-associated with various original folders. The aliases are broken coming from an older macOS and do not point to the correct original folder so I need to manually Get Info > “Select New Original” or manually remake them. So I was wondering if I could do a search in the finder (using “name matches”) to get a list of these related folder & aliases and then use these user selected results within a script to resolve the alias paths back to the singular actual folder? I was hoping I could just change the original path of the aliases using “set original item” to match the manual process when using “Get Info > Select New Original” without having to delete and remake all the aliases?

Generic Example: search term = Original Folder

• Original Folder (actual folder)
• Original Folder 1 alias
• Original Folder 2 alias
• Original 3 Folder alias
• Original Blah Folder alias
• Blah Original Folder alias
• Blah Original Blah Folder alias

The above would be the user selection from the returned search. The reason I mention just user selected specific items (just the specific folder + relative aliases) from the search query is because there might also be other related files returned in the search also like other folders, aliases, .txt, .webloc, .zip, .img, etc. So I would need to manually select the relative folder + aliases that I want to target in the script. I think due to variables involved doing a manual selection from the returned search results and using that selection within the script would be easier and would still save a lot of clicks verse doing everything manually to fix the aliases.

I have many instances similar to the above example, the number of aliases derived from the original can vary its not a set number and their names can slightly vary also aside from including the key search terms. So I was hoping through a script that I could target these specific returned finder search items (just the folder + associated aliases) and have the script basically mimic the “Select New Original” to the singular folder path of the selection for all the aliases?

I’ve found various threads on the forum and elesewhere given my lack of understanding which are possibly relative that I’ve tried to use to cobble something together. Though I’m really not confident regarding my described scenario as I am not so well versed in AppleScript at this level. I sure wouldn’t want to mess things up further than what they already are regarding old broken aliases by trying to cobble something together and break things further or completely screw up the file system :—).

Below are some of my failed attempts at trying to cobble something together which should easily show my lack of understanding unfortunately. Obviously nothing is working as expected to completion in my isolated tests. I feel in some small way I’m blindly on the right track but in reality I’m probably way off which could easily be the case given my lack of knowledge. With some of my attempts even when other things are sorted out, it seems maybe I will need to use a different loop other than a simple repeat given some “display” feedbacks don’t seem to pair up correctly? Again I’m rather confused in my attempts.

Here is one of my failed attempts at trying to use “set original item”.

tell application "Finder"
	set selItems to selection
	repeat with curItem in selItems
		if class of curItem is folder then
			set folderPath to POSIX path of (curItem as alias)
			display dialog folderPath
		end if
	end repeat
	set selItems to selection
	repeat with curItem in selItems
		if class of curItem is alias file then
			-- set original item of file curItem to file folderPath
			-- set original item of alias file curItem to folderPath
			-- set original item of file curItem to POSIX file folderPath
			-- set originalFile to the original item of (file folderPath)
			-- set original item of file curItem to folderPath
		end if
	end repeat
	display dialog "Done"
end tell

Here is one of my failed attempts at remaking the aliases at their current location.

tell application "Finder"
	set selItems to selection
	repeat with curItem in selItems
		if (kind of curItem) is "folder" then
			set folderPath to POSIX path of (curItem as alias)
			-- display dialog folderPath -- Display
		end if
	end repeat
	repeat with currentFile in selItems
		if (kind of currentFile) is "alias" then
			set currentAliasName to displayed name of currentFile
			set aliasPath to currentFile as alias
			set currentAliasFile to file currentAliasName of folderPath as alias
			tell application "Finder"
				delete currentFile
				make new alias file at folder aliasPath to currentAliasFile
			end tell
		end if
	end repeat
end tell

Both probably don’t make much sense given at this point I’m very confused myself with all the attempts and changes I’ve tried thus far. But I wanted to show that I’m earnestly trying but I’m seriously confused and stuck in my efforts to accomplish this. So I’m seeking assistance here.

I’m really hoping something could be accomplished as this would really help me automate some the otherwise brutal manual process of relinking or remaking all these broken aliases. I’m hoping such a script could work from the selection in the search window because it needs to be generic enough for various inputs of original folder + # of aliases. I have a list of the all the unique search terms I will need to search for.

The more I search this forum, stackoverflow, etc., hoping to cobble something together the more confused I get. Plus I’m concerned that I may mess up my folders further that’s why I’ve created an isolated test hoping to create a working script. Any suggestions, input, insight or overall help is very much welcome from anyone.

Sorry for the huge write up I hope it makes some sort of sense.

Thanks everyone !

LL

Hi,
I have to go to work, so I will show you the beginning of the workaround.

First of all, there is no need to search for dead Finder alias files manually. Start your script with finding them programatically:


use framework "Foundation"
use scripting additions

-- choose header folder, we wil repair aliases of its ENTIRE contents
set rootFolderPath to POSIX path of (path to home folder)
-- get all Finder alias files
set aliasFiles to my getAliasFiles(rootFolderPath)
-- get broken Finder alias files
set brokenAliasFiles to my getBrokenAliasFiles(aliasFiles)


on getAliasFiles(thePath)
	-- get the folder URL
	set theFolderURL to current application's NSURL's fileURLWithPath:thePath
	-- get URL of all items in folder, recursively
	set isAliasKey to current application's NSURLIsAliasFileKey
	set theFileManager to current application's NSFileManager's defaultManager()
	set allURLs to (theFileManager's enumeratorAtURL:theFolderURL includingPropertiesForKeys:{isAliasKey} options:((current application's NSDirectoryEnumerationSkipsPackageDescendants) + ((current application's NSDirectoryEnumerationSkipsHiddenFiles) as integer)) errorHandler:(missing value))'s allObjects()
	-- build an array with URLs to alias files
	set filteredArray to current application's NSMutableArray's new()
	repeat with oneURL in allURLs
		set keyValue to end of (oneURL's getResourceValue:(reference) forKey:isAliasKey |error|:(missing value))
		if (keyValue as boolean) then (filteredArray's addObject:oneURL)
	end repeat
	-- convert URL array to file list
	return filteredArray as list
end getAliasFiles


on getBrokenAliasFiles(aliasFiles)
	set brokenAliasFiles to {}
	repeat with aliasFile in aliasFiles
		tell application "Finder"
			set aliasFile to file (aliasFile)
			if not (exists original item of aliasFile) then set end of brokenAliasFiles to aliasFile
		end tell
	end repeat
	return brokenAliasFiles
end getBrokenAliasFiles

Thank you so much for taking time to respond to me it’s very much appreciated.

That looks really impressive for gathering the broken aliases via Applescript. Though I’m sorry my comment from above …

… was trying to convey that I already know which are the dead aliases that can’t be resolved (that’s the list I actually have). So I was hoping to accomplish the described workflow outlined above with AppleScript to help me fix them. I’m not sure it can be automated completely due to various factors which I tried to outline above. But maybe I don’t really understand what you’re alternatively proposing? If it can be entirely automated without causing issues or sacrificing accuracy then I would surely be open to that. :–)

Thank for again for your time and response.

I have tried to minimize your manual work as much as possible. To improve the speed of the script, I limit the search scope to the user’s domain and its subfolders. The script assumes that alias names end with " alias", (to retrieve basename of alias file), that is, space&“alias”. Script:


use framework "Foundation"
use scripting additions


-- choose header folder, we wil repair aliases of its ENTIRE contents
set rootFolderPath to POSIX path of (path to desktop folder) -- you can choose other folder here
-- get all Finder alias files
set aliasFiles to my getAliasFiles(rootFolderPath)
-- get broken Finder alias files
set brokenAliasFiles to my getBrokenAliasFiles(aliasFiles)

-- limit the search scope to the user's domain and its subfolders
set baseFolder to POSIX path of (path to home folder) -- search beginning from home directory

-- Repair broken alias files
repeat with brokenAliasFile in brokenAliasFiles
	tell application "Finder"
		set brokenAliasFileName to text 1 thru -7 of (get name of file ((contents of brokenAliasFile) as text))
		set theContainer to folder of file ((contents of brokenAliasFile) as text)
	end tell
	with timeout of 600 seconds
		set foundItems to paragraphs of (do shell script "/usr/bin/find " & quoted form of baseFolder & " -name " & quoted form of (brokenAliasFileName & "*"))
	end timeout
	set theChoice to choose from list foundItems with prompt "Select Original Item for current broken alias file"
	if theChoice is false then
		display dialog "Your selection was empty (Broken alias remains as was)"
	else
		try
			tell application "Finder" to set originalItem to file theChoice
		on error
			tell application "Finder" to set originalItem to folder theChoice
		end try
		delete contents of brokenAliasFile
		tell application "Finder" to make alias file at theContainer to originalItem
	end if
end repeat


on getAliasFiles(thePath)
	-- get the folder URL
	set theFolderURL to current application's NSURL's fileURLWithPath:thePath
	-- get URL of all items in folder, recursively
	set isAliasKey to current application's NSURLIsAliasFileKey
	set theFileManager to current application's NSFileManager's defaultManager()
	with timeout of 600 seconds
		set allURLs to (theFileManager's enumeratorAtURL:theFolderURL includingPropertiesForKeys:{isAliasKey} options:((current application's NSDirectoryEnumerationSkipsPackageDescendants) + ((current application's NSDirectoryEnumerationSkipsHiddenFiles) as integer)) errorHandler:(missing value))'s allObjects()
	end timeout
	-- build an array with URLs to alias files
	set filteredArray to current application's NSMutableArray's new()
	repeat with oneURL in allURLs
		set keyValue to end of (oneURL's getResourceValue:(reference) forKey:isAliasKey |error|:(missing value))
		if (keyValue as boolean) then (filteredArray's addObject:oneURL)
	end repeat
	-- convert URL array to file list
	return filteredArray as list
end getAliasFiles


on getBrokenAliasFiles(aliasFiles)
	set brokenAliasFiles to {}
	repeat with aliasFile in aliasFiles
		tell application "Finder"
			set aliasFile to file (aliasFile)
			if not (exists original item of aliasFile) then set end of brokenAliasFiles to aliasFile
		end tell
	end repeat
	return brokenAliasFiles
end getBrokenAliasFiles

Thank you @KniazidisR ,

That’s a pretty neat approach to things. It includes so much script stuff that I’ve never seen before, you’re AppleScript skills are certainly apparent compared to my lack of knowledge. :—)

I tried your script on a mirrored disk to test it. Since I didn’t fully know what it would be doing with things. Given my alias situation here is what my test resulted in.

• It spent a lot of time “Running …” of course initially but also in between queries.
• The prompt comes up with multiple aliases, but only trashes one alias and seemingly not all related aliases
• After it did numerous using the script. While it was running, I checked the trash and manually searched, it appears to not be finding or replacing all relative aliases
• The prompt also includes other file types at times like .html etc.,
• I eventually got a “Script Error: Apple event timed out” maybe due to me not reacting to the prompt in time or something?

It’s an impressive approach for sure. Although given my situation even though it tries to be a more automated approach it seems maybe to take longer than my proposed approach due to the “Running …” between queries of found instances. Also it seems to not be finding and replacing all relative alias instances. If you think you can and want to improve your proposed approach that’s certainly welcome.

But regardless if it’s not too much to ask can you please help me fix my initial approach also? I think the manual search is going to be more effective and fail safe in allowing me to work from a manual selection. Allowing me to accurately ensure that all instances of aliases get resolved to the original folder as described in my initial post. I would really like to have a working version of this approach available, even if a more automated approach can accurately be accomplished.

I greatly appreciate your time and willingness to help me. You have certainly opened my eyes to things and approaches I never really knew existed for my possible future discovery. I really welcome your or anybodies help in correcting my more manual described and shown approach in my initial post. If you would be so kind and generous to help me with it, I would be very grateful to have that approach.

Thank you so much.

I have added in post #5 instructions for increasing the timeout so that timeout errors should no longer occur.

Now, for the rest - don’t test the speed of the script on an external drive, as the script tries to take advantage of Spotlight’s speed.

There will always be much less broken aliases than file system elements. For example, in my domain I found only 1 broken alias and about 300,000 elements. I will fix this 1 broken alias, not the aliases of 300,000 items.

I will not write a script following the reverse logic of things. Because first there is 1 broken file alias, and then it (and only it) is corrected if you find 1 (and only one) corresponding original. Then - processing of the next broken alias, one at a time. And not like yours - on the contrary - you are looking for alias files for each element, of which there may be many, and some may be quite working.

Also, the script cannot be “instant”, since every time there is a search in the folder and subfolders. This is logical and to be expected.

My test only had a singular internal SSD disk which is already indexed with Spotlight inside a cMP. So that is not an issue regarding external drive, etc. Though the search portion of the script seemed slower than searching manually with a given criteria and getting accurate returns then a finder search? Of course it’s then certainly faster (than manual clicks) after that when relinking / recreating the aliases but it didn’t accurately target all the associated instances or recreate them.

As mentioned previously I already know which are broken and which are not. So even with a manual search and selection I would only be targeting the broken aliases for replacement.

The only aliases I want to replace (obviously broken) are the ones I already know are associated with an original. I just want to manually target multiple aliases and also point to a corresponding folder via a selection.Then automate the portion of the relinking / remaking process of each alias at its current location and delete the broken one. That’s all I want from the Manual approach, search, make selection (folder + aliases) - Then Automated delete broken, recreate selected aliases associated to the selected folder at their current location. Rinse & repeat.

Are you saying with your script if there are 20 aliases of a folder then I have to pick the same folder eventually in the prompt 20 times to recreate them as they are found? I guess I don’t fully understand what you are saying otherwise? In my test your script is not finding all the associated aliases of the folder so maybe that is what you mean by “one at a time”.

If so then I really believe working from a manual selection would be more accurate and proficient in my case. To do a singular search and then replace them all at once, repeat. I just can’t figure out how to get that working fully. So I signed up and I’m asking here.

Please understand that anything I say or express is certainly not against your script or abilities. Maybe the situation I have is just more complex than what can be fully automated accurately or in an efficient manner. But I think it could be accomplished if I could get my described partially manual and partially automated approach working.

Thank you

Well, since you insist, then try this script, which I wrote based on yours:


tell application "Finder"
	set selItems to selection
	
	repeat with curItem in selItems
		if kind of curItem is "Folder" then
			set originalItem to folder (contents of curItem as text)
			exit repeat
		end if
	end repeat
	
	repeat with currentFile in selItems
		if kind of currentFile is "Alias" then
			set theContainer to container of currentFile
			delete currentFile
			try
				make alias file at theContainer to originalItem
			end try
		end if
	end repeat
end tell

Thanks so much @KniazidisR. Can you please modify it so it keeps the same name of the original alias for the recreated alias thats being made. That would be the only remaining thing I had hoped to accomplish.

I’m not sure if the below is what does that from your initial automated version? I can’t seem to understand how that’s being used in your script.

set brokenAliasFileName to text 1 thru -7 of (get name of file ((contents of brokenAliasFile) as text))

Also for understanding, reverting back to my original post. Is it correct to assume “set original item” does not work on broken aliases and they must instead be recreated?

Thanks so much for your assistance, time and knowledge KniazidisR, it’s greatly appreciated.

It is easy:


tell application "Finder"
	set selItems to selection
	
	repeat with curItem in selItems
		if kind of curItem is "Folder" then
			set originalItem to folder (contents of curItem as text)
			exit repeat
		end if
	end repeat
	
	repeat with currentFile in selItems
		if kind of currentFile is "Alias" then
			set theContainer to container of currentFile
			set oldName to name of currentFile
			delete currentFile
			try
				set newAlias to make alias file at theContainer to originalItem
				set name of newAlias to oldName
			end try
		end if
	end repeat
end tell

Yes.

This is how my other script defines itself search term. Every alias file ends with " alias", so script cuts this word and remains only base name of alias file, which is used as search term further. Search term is used in the shell’s find command as you can see. It finds every file and every folder (in the subfolders of home folder too), whose name begins with search term. The ending is wirdcard (*) as you see. It can be any. (“.mp3”, “.pdf”, “bla_bla.pdf”, “Untitled folder”)

Easy for you KniazidisR, thats for sure. :smiley:

Now that I see my concept fixed by you and working that simple code actually is starting to make sense to me. Also thanks for the feedback on “set original item” regarding broken aliases, its a pitty thats not an option also.

I may never (or at least for a long time) understand your more automated approach :—). But now having these two approaches provided so graciously by you, it should really help me to more easily correct these broken aliases faster and with less direct intervention. I really appreciate you sharing your automated approach that’s indeed wonderful also.

I’m super thankful for your input and getting things working for me. That’s awesome.

Side note: This is how I originally gathered the broken aliases list, it was manual (though not too bad) and you will probably laugh. :–D

• Finder search for ‘alias’ and save the search
• Then from that save folder ‘get info’ in batches of 140 aliases down the results in the folder
• Once all the ‘get info’ windows open in a brief moment the finder resolves all aliases that it can
• Close all windows Option-Command-W
• Re-open saved search and repeat from where last left off with another 140, until all done
• Once done, scroll down past all the modified that the finder fixed and any that were not broken
• Multiple select and copy all that are remaining - aka broken
• Paste that list in text editor, sort, remove duplicates
• That’s the list of broken alias search terms to find (per the last script)

Crazy huh, but accurate. :stuck_out_tongue:

But after seeing your initial script above, I’m assuming a script could have produced the same list but all automated. It must be wonderful to have the ability to command AppleScript at your demand for whatever results you require.

I suppose a person could even script something that uses a generated ‘list’ of search terms (regardless of how generated) as an array. Then from this array does a finder search using ‘name matches’ for each item in the array. Then allows the user to make the custom selection. Then processes the aliases like your last script (folder + aliases) and repeats the prompts and process through the array until completion. That would prevent copying and pasting from the list and doing the search manually. But still allow the apporach of targeting multiples to be resolved at once like the last script.

It’s probably amazing what all can be scripted at will and accessed in the macOS and apps. This forum is inspiring and your help with this has been wonderful KniazidisR.

Can I download your brain from somewhere? ;–)

Here is a little bit of a different approach which you may find useful. It becomes especially handy if you want to address all of the “broken” alias files in a chosen folder or on your entire system, rather than only acting on currently selected items in Finder.

Any new created alias files in Finder will be named like this for example “Some File.txt alias” (with “alias” added to the name after the file extension). However, that alias file can be renamed and by removing the word “alias” from the name, it will still be recognized as an alias file.

This is where mdfind becomes extremely useful. Not only is mdfind Lightning Fast, it also will find the alias files that don’t have “alias” in the file name.

Before testing my full AppleScript code, I would suggest testing mdfind in Terminal.app and make sure it returns the results you’re looking for. For example, I used… [format]mdfind “kMDItemKind == ‘Alias’” | egrep -iv ‘ColorSync|adobe|paral|icloud|onyx’ | sort[/format]This returned the results to all alias files which do not have “ColorSync” or “adobe” or “paral” or “icloud” or “onyx” in its path name. (The -iv options for egrep mean… -i = case insensitive and -v = opposite of)

Then adjust the “do shell script” commands in the AppleScript code, as needed.

property originalFilesOfProperlyLinkedAliases : {}
property brokenAliasFiles : {}
property brokenAliasFilesNames : {}

-----------------------------------------------------------------------							
(* OPTION 1: Use This To Search The Entire Computer For Alias Files *)
--set aliasFiles to paragraphs of (do shell script "mdfind " & quoted form of "kMDItemKind == '*Alias*'")
-----------------------------------------------------------------------							
(* OPTION 2: Use This To Choose Folder To Search Through For Alias Files *)
set mdfindSearchOnlyInFolder to quoted form of POSIX path of (choose folder with prompt ¬
	"mdfind Only In Folder" with invisibles without multiple selections allowed)
set aliasFiles to paragraphs of (do shell script "mdfind -onlyin " & mdfindSearchOnlyInFolder & ¬
	" " & quoted form of "kMDItemKind == '*Alias*'")
-----------------------------------------------------------------------							

tell application "Finder"
	repeat with i from 1 to count of my aliasFiles
		set thisItem to item i of my aliasFiles as POSIX file as alias
		tell alias file thisItem
			try
				-- Who Knows, You May Need This List In The Future For Some Reason
				set end of my originalFilesOfProperlyLinkedAliases to original item as alias
			on error errMsg number errNum -- If original item Can't Be Found
				try
					set theContainer to thisItem's container
					set brokenAliasFileName to thisItem's name
					set end of my brokenAliasFiles to thisItem
					set end of my brokenAliasFilesNames to brokenAliasFileName
					
					if file type of thisItem is not "fdrp" then -- If Item Is Not A Folder
						set deletedAliasFile to delete thisItem -- Deletes Alias File If Link Is Broken
						try
							-----------------------------------------------------------------------							
							(* OPTION 1: Choose New Source File & Creates A New Alias
							 File With Name Of New Chosen Source File *)
							--set newAlias to (make new alias file at theContainer to ¬
							--	(choose file with prompt "Choose A New Source File For " & ¬
							--		brokenAliasFileName))
							-----------------------------------------------------------------------							
							(* OPTION 2: Choose New Source File & Creates A New Alias File 
								With Name Of The Original Deleted Alias File *)
							if "alias" is not in brokenAliasFileName then ¬
								set brokenAliasFileName to brokenAliasFileName & " alias"
							set name of (make new alias file at theContainer to ¬
								(choose file with prompt "Choose A New Source File For " & ¬
									brokenAliasFileName)) to brokenAliasFileName
							-----------------------------------------------------------------------							
						on error errMsg number errNum
							move deletedAliasFile to theContainer -- Undeletes Alias File If User Chooses Cancel
						end try
					else -- If Item Is A Folder
						set deletedAliasFile to delete thisItem
						try
							-----------------------------------------------------------------------							
							(* OPTION 1: Choose New Source Folder & Creates A New Alias File 
								With Name Of The New Chosen Source Folder *)
							--set newAlias to (make new alias file at theContainer to ¬
							--	(choose folder with prompt "Choose A New Source Folder For " & ¬
							--		brokenAliasFileName))
							-----------------------------------------------------------------------
							(* OPTION 2: Choose New Source Folder & Creates A New Alias File 
										With Name Of The Original Deleted Alias File *)
							if "alias" is not in brokenAliasFileName then ¬
								set brokenAliasFileName to brokenAliasFileName & " alias"
							set name of (make new alias file at theContainer to ¬
								(choose folder with prompt "Choose A New Source Folder For " & ¬
									brokenAliasFileName)) to brokenAliasFileName
							-----------------------------------------------------------------------
						on error errMsg number errNum
							move deletedAliasFile to theContainer -- Undeletes Alias File If User Chooses Cancel
						end try
					end if
				end try
			end try
		end tell
	end repeat
end tell

Thank you so much for your input and script also @wch1zpink. This thread is really becoming a wealth of information on the topic and demonstrating how to use various approaches. So that has truly been great.

Yep, I actually used mdfind as my initial step to evaluate the overall alias situation once I was experiencing various broken instances. It is indeed virtually instantaneous returning the results, I just used — mdfind kMDItemKind=“Alias”

What you have demonstrated using that method as an alternative is again very interesting. Though much of your script remains above my current comprehension :—). But throughout this thread there are really lots things that are helping to make sense in certain areas for me. So I’m very thankful for your input and script as well, thank you.

I really appreciate everyones input on this topic, it has been wonderful.