Creating a Folder Alias Button

Could someone help me create a button that:

If a folder is selected (filepath), use it, otherwise, use sourcepath:

  • Gets highlighted if it's in the folder aliases.
  • Modifies the alias name if it's already in the folder aliases.
  • Creates a folder alias if it isn't in the folder aliases.

Give this a try:

@dirsonly
@evalalways:=path = seldirs == 1 ? selpath : source
@evalalways:=alias = Resolve(path, "aef");
@toggle:=!!alias
="Set NOOP"
@if:=!!alias
Favorites ALIAS=delete NAME={=FilePart(alias)=}
@if:common
Favorites ADD=alias NAME={RS|Alias for "{=path=}"|{=FilePart(alias ?? path)=}} PATH "{=path=}"

This button isn’t perfect, you can’t modify built-in aliases, so if you try to change a built-in alias it will fail silently.

@a815

Thank you very much. I will use this only for created folder aliases.

I noticed that for a created folder alias, clicking Cancel or the close button in the dialog removes the alias.

Try this:

@dirsonly
@evalalways:=path = seldirs == 1 ? selpath : source
@evalalways:=alias = Resolve(path, "aef");
@toggle:=!!alias
@set name={RS|Name}
="Set NOOP"
@if:=!!alias
Favorites ALIAS=delete NAME={=FilePart(alias)=}
@if:common
Favorites ADD=alias NAME={$name} PATH "{=path=}"

This version doesn't have dynamic title and default string as using evaluator in {dlgString} has some bugs :sweat_smile:.

The {dlgString} can almost be replaced with an Evaluator Dialog() , but the returned value is a map, one part of which contains the new alias name .
It has the form of [choice: (button number), text: NewAlias] .
I don't know how to specify only that part.

Favorites ADD=alias NAME={=Dialog( "s", "Alias for " + path, FilePart(alias ?? path) )=} PATH "{=path=}"
d = Dialog("s", "Alias for");
d.text

Thanks !
That did the trick.
This seems to be generating the correct default string and correctly creating new Aliases.

@dirsonly
@evalalways:=path = seldirs == 1 ? selpath : source
@evalalways:=alias = Resolve(path, "aef");
@toggle:=!!alias
="Set NOOP"
@if:=!!alias
Favorites ALIAS=delete NAME={=FilePart(alias)=}
@if:common
@evalalways:=d = Dialog( "s", "Alias for " + path, FilePart(alias ?? path));
@if:=d.choice==1
Favorites ADD=alias NAME="{=d.text=}" PATH "{=path=}"

Edit: Ooops !
I forgot to test what button was pressed.
It was creating an alias when either Ok or Cancel was pressed .
I fixed the above code.

Thanks a lot.

The last modified code fixed alias creation on pressing Cancel, but it removes the alias if Cancel or close button is pressed in the dialog.

I found the problem.
It removes the alias if Resolve(path, "aef" ) returns true.
The original code in this thread has an @if:=!!alias which runs Favorites ALIAS=delete NAME={=FilePart(alias)=} if alias = Resolve(path, "aef") is true.

This clause seems to be intended to prevent two alias names with the same path.
This also replaces alias paths built from aliases with an alias built from a path.

To keep this clause and also fix the problem we can move the clause to below the dialog and add a check of what button was pressed.

@dirsonly
@evalalways:=path = seldirs == 1 ? selpath : source
@evalalways:=alias = Resolve(path, "aef");
@toggle:=!!alias
="Set NOOP"
@if:common
@evalalways:=d = Dialog( "s", "Alias for " + path, FilePart(alias ?? path));
@if:=!!alias && d.choice == 1
Favorites ALIAS=delete NAME="{=FilePart(alias)=}"
@if:=d.choice==1
Favorites ADD=alias NAME="{=d.text=}" PATH "{=path=}"

Edit: The line Favorites ALIAS=delete NAME={=FilePart(alias)=} was missing quotes.
It should be Favorites ALIAS=delete NAME="{=FilePart(alias)=}" .
It is fixed above.

I found an icon that works well for this button.
It is an SVG icon found in @PassThePeas' EasyColor SVG iconsets .
The add_road icon found within the Material Filled icon set looks fine and can be colored to your liking in both light and dark mode.

Thank you all for your contributions! The latest version from @David is working wonderfully.

Since this is related to folder aliases, may I ask if it would be possible to create a button to delete all user-defined folder aliases for the selected file paths, or for the source path if there is no selection, after displaying a confirmation dialog?

A path can have multiple aliases, so this functionality would delete all user-defined aliases (not built-in aliases) associated with the path.

The following example command in the documentation indicates that the alias name must be given: Favorites ALIAS=delete NAME=MyAlias

Yes, via scripting.

@a815 deserves much credit here.
His code broke through the hard part of getting started even though I misunderstood his !! syntax until later.

All I did was attempt to fix this and in the end it actually works !
But the reason it does now work really did involve a bit of luck and some patience.
Be happy we have this !

Much appreciated! @a815 @David @lxp @Leo

If anyone was able to create a script, as Leo pointed out, to delete all aliases for selected folders or sourcepath if there's no selection, could you share it? Thank you.

My visual indicators script in the buttons section of this forum creates a user command to do this.