Separate aliases with local paths from remote paths

I'm not sure if this is the appropriate place for this, but I'm looking to make a macro that can take a selection of aliases and separate the aliases with remote paths from those with local paths. Is this even possible?

I would be grateful for any help you can provide.

Paul

If you provide some sample input and what you'd hope the sample output to be, hopefully someone can give you a helping hand with where to start.

Thanks for your reply.

I have a folder full of thousands of aliases and need to isolate the ones referencing a remote path on another computer on an AFP network. I can iterate all of the alias files over a terminal command using KM but can't find the right command to determine if the original path of the alias is remote. I need to eliminate the remote-path aliases because the computer they're referencing no longer exists and Kontakt (the application I'm using) hangs whenever it tries to resolve them.

So input would be a Finder selection of all of the aliases and the output would be to place the matching aliases into another folder.

Any thoughts?

I must admit, I've not aliased remote paths myself before, and so my inclination here is to AppleScript a solution.

When retrieving the properties of an alias file through AppleScript, as far as I can tell, if the remote path to which the alias file points cannot be accessed, Finder sets the original item property for the file (in AppleScript) to missing value, otherwise this property will contain a Finder reference to the file path of the original item. Therefore, this property seems like our discriminator.

However, you also said that your folder contains thousands of aliases. This isn't a number that Finder loves, and I wouldn't recommend trying to select them all either. Therefore, I've scripted System Events to retrieve the list of alias files from a chosen folder, then invoked Finder to iterate through them one-by-one to test the original item property and build a list of those aliases with broken links.

set dir to (choose folder)
set dirout to "__null"

set L to {}

tell application "System Events" to set fs to the path of ¬
	every file of dir whose kind = "Alias"

with timeout of 600 seconds
	tell application "Finder"
		repeat with f in fs
			tell file f to if not ¬
				(its original item exists) ¬
					then set end of L to it
		end repeat
		
		ignoring application responses
			make new folder at dir with properties {name:dirout}
		end ignoring
		
		move L to the folder named dirout in dir
	end tell
end timeout

The script will ask you to select a directory, which will be the directory containing your alias files. If this is a one-time thing, I'd suggest it might be easier just to run this script from within Script Editor or, if you have it, Script Debugger. There's no point running this from Keyboard Maestro, which I think will slow the execution time down somewhat.

Bear in mind that this will not be a quick process, depending on your computer's processing power and the number of broken alias links that exist (Finder can identify a working alias pretty quickly, but takes a couple of seconds to determine that an alias is broken). It will also likely block Finder in the process, so you should choose a time to execute this script when you won't be wanting to utilise Finder yourself. However, be around during its run, in case the script throws an unexpected error.

When the script is complete, a dialog showing "Done." will pop up, and the broken alias files will be found in a subfolder called "__null" of the original directory.

Let me know how it goes.

Thank you so much for taking the time to write a script for this. I’ll give it a try and report back.

Hey Guys,

This critter works nicely.

I believe it will solve the speed issues related with macOS trying to resolve an alias on a network, but I can't test it to be certain.

It's very simple to build from the Terminal if you have Xcode installed, and I can provide a 64-bit binary built on macOS 10.12.6 if needed.

-Chris

Thank you for posting! This worked!

My only issue is the offending aliases are nested in folders sometimes a few levels deep and aren't residing all in one directory. Would you mind tweaking this so it runs on a selection of files from the Finder? Thanks.

This processes through the Finder selection and creates a folder on your desktop into which the broken alias files will be moved:

property dirout : "__null_aliases__"

set fs to {}
set L to {}

tell application "Finder"
	repeat with f in (get selection)
		if f's class = folder then set fs to fs & ¬
			(every alias file in f as alias list)
	end repeat
	
	with timeout of 600 seconds
		repeat with f in fs
			try
				f's original item
			on error
				set end of L to f's contents
			end try
		end repeat
		
		ignoring application responses
			make new folder at desktop ¬
				with properties ¬
				{name:dirout}
		end ignoring
		
		move L to the folder named dirout
	end timeout
end tell

Alternatively, the one that follows is similar to the initial script, but performs a deep search of nested folders. Please consider the depth of the directory tree. For example, selecting your home folder would probably be a bad idea if you want to stay sane. The seemingly innocuous desktop folder is the other location I recommend against doing a deep search, since it tends to contain a link to Macintosh HD (or whatever you might have renamed your local hard disk to), which will obviously result in every single file and folder on your entire system being searched.

property dirout : "__null_aliases__"

set dir to (choose folder)
set L to {}

tell application "Finder"
	set fs to every alias file in the entire contents of dir as alias list
	
	with timeout of 600 seconds
		repeat with f in fs
			try
				f's original item
			on error
				set end of L to f's contents
			end try
		end repeat

		ignoring application responses
			make new folder at dir with properties {name:dirout}
		end ignoring
		
		move L to the folder named dirout in dir
	end timeout
end tell

It worked! You're a ninja. This will be very helpful. Thank you so much for doing this for me and giving me the opportunity to learn more about AppleScript.

1 Like

A post was split to a new topic: How Do I Prompt for Folder, and Limit File Selection to aliases?