AnsweredAssumed Answered

String values in GetParameterAsText()

Question asked by jared_pilbeam on Jan 3, 2017
Latest reply on Jan 11, 2017 by jared_pilbeam

Hi,

I have a working ArcMap script tool called Update Map Elements that updates text and numbers in ArcMap documents.

 

It has four parameters; Workspace, Output, oldText and newText. Workspace is where the input folder goes. Output is the save-to folder. OldText is the text string to be replaced by the newText text string (Red Branch changes to Blue Branch).

 

My problem is: how can I have multiple string values? So, in the above parameters I specify the Red Branch string value to be replaced by Blue Branch. What if I wanted to have a bunch of string values--such as Red Branch, 600, Violet (oldText) and the corresponding newText values Blue Branch, 800, Green?

 

I have a feeling I add index positions to the values within the script, but I'm not sure how. I've had a look at this though I'm not sure it's the right approach: SetParameterAsText—Help | ArcGIS for Desktop  

 

Here's the script behind the Update Map Elements tool:

#set path to relevant folder
import arcpy
from arcpy import env
import os
arcpy.env.overwriteOutput = True

# set path to relvant folder
arcpy.env.workspace = Workspace = arcpy.GetParameterAsText(0)
Output = arcpy.GetParameterAsText(1)
oldText = arcpy.GetParameterAsText(2)
newText = arcpy.GetParameterAsText(3)
# list the mxds of the workspace folder
for mxdname in arcpy.ListFiles("*.mxd"):   
    print mxdname
# set the variable
    mxd = arcpy.mapping.MapDocument(Workspace + "\\" + mxdname)
# replace '2016' that occurs in the title of document
    for elm in arcpy.mapping.ListLayoutElements(mxd, "TEXT_ELEMENT"):
        if oldText in elm.text:
            elm.text = elm.text.replace(oldText, newText)
            print '{} changed'.format(elm.text)
   
    for elm in arcpy.mapping.ListLayoutElements(mxd, ""):
        if oldText in mxd.title:
            mxd.title = elm.text.replace(oldText, newText)
# move the mxd.saveACopy outside of the if loop so a copy is saved even it does not meet the condition of the if loops
    mxd.saveACopy(os.path.join(Output + "\\" + mxdname))
# do not include this delete statement inside the above loop or it will delete the mxd object inside the loop. Make sure to dedent.
del mxd

 

Thanks in advance.

Outcomes