arcpy.AlterAliasName doesn't apply to dataset until map is closed

383
0
05-12-2022 01:08 AM
Labels (2)
Dirk
by
New Contributor II

Short version:

arcpy.AlterAliasName immediately updates alias of a feature class dataset when run from python window. Yet when run as script tool, changes aren't applied until the map is exited. 

Background and context:

I have an ArcGIS Pro Desktop project with 230 layers in its table of contents (TOC). Many are geodatabase feature classes which I have given user-friendly names to in the TOC. 

I can use a loop of arcpy.AlterAliasName to go through all layers in the map and update the data source alias of all geodatabase feature classes:

 

 

# Code to loop through layers in a given map and update the data source alias with the layer name
#
#
aprx = arcpy.mp.ArcGISProject("CURRENT")
# !! Enter the name of the map you want to update aliases for
mapname = "Map"
m = aprx.listMaps(mapname)[0]
for l in m.listLayers():
    lname = l.name
    try:
        dsource = l.dataSource
        print("Writing layer name <" + lname + "> to dataset <" + dsource + ">...")
        # !!!KEY LINE OF SCRIPT!!!
        arcpy.AlterAliasName(dsource, lname)
        print("Successfully processed: <" + lname + ">")
    except Exception:
        print("Can't process: <" + lname + ">..group layer, non-gdb or read only?")
# END

 

 

This works perfectly when I paste it in the ArcGIS Pro Python window and run it. As soon as it is run, if I look at the feature class properties in Catalogue, the alias is updated with the description from the TOC.

My issue is with turning this into an ArcGIS Pro Script tool. When run as a script tool, the data source alias does not update until the map is closed in ArcGIS Pro. 

My script tool was just passing a single parameter to the script - a Map, selected by the user.

I thought I had cracked the problem when I came across the following, but when I added the second derived parameter, it made no difference.

Dirk_0-1652341735906.png

I'd really like to understand why arcpy.AlterAliasName works fine when run from the Python window, but when run within a script tool, won't apply changes to the dataset while the map is open.

It wouldn't be such an issue if I could automate closing of the map with the script tool/python code, but I don't think that's possible either?

FWIW, here is the code called by the script tool:

 

import arcpy


# Get the current project as an object
aprx = arcpy.mp.ArcGISProject("CURRENT")
# Get the selected map parameter as text from the script tool dialogue
mapName = arcpy.GetParameterAsText(0)
# Look up the correct map object from the list of maps in the project
m = aprx.listMaps(mapName)[0]

for l in m.listLayers():
    lname = l.name
    try:
        dsource = l.dataSource
        arcpy.AddMessage("Writing layer name <" + lname + "> to dataset <" + dsource + ">...")
        # Key line of script. Updates the alias.
        arcpy.AlterAliasName(dsource, lname)
        arcpy.AddMessage("Successfully processed: <" + lname + ">")
    except Exception:
        arcpy.AddMessage("Can't process: <" + lname + ">..group layer, non-gdb or read only?")

# Set a derived output parameter to get ArcGIS Pro to refresh the map and apply the changes
# Without this line the new aliases won't apply on the feature classes until map is closed
# Currently this doesn't work though!!
arcpy.SetParameterAsText(1,mapName)

 

0 Replies