Message script tool for ModelBuilder

3956
2
10-18-2012 10:16 AM
curtvprice
MVP Esteemed Contributor

Today I got a message script tool working along the lines of the one discussed in the ArcGIS Blog for use in generating messages inside ModelBuilder.

First, an example of how it works, set up as a script tool with two text arguments, the first "MESSAGE","ERROR","WARNING", the second your message. What's  new about this one over the one linked from the blog is that with this version, you can do error codes by using the format: "id arg1,arg2". (Sure would be nice if a tool was provided that does this in ModelBuilder.)

Updated for ArcGIS 10x / Pro - currently testing


Executing: Message WARNING "id 591 First,Last"
Start Time: Thu Oct 18 11:58:10 2012
Running script Message...
WARNING 000591: First parameter not Last.
Completed script Message...
Succeeded at Thu Oct 18 11:58:10 2012 (Elapsed Time: 0.00 seconds)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

#
# GP Script tool - Message
# for use in ModelBuilder

import arcpy

# Two text parameters:
# 0) Message type: ERROR, INFORMATIVE, WARNING
# 1) Message text
msgType = arcpy.GetParameterAsText(0).upper()
msgText = arcpy.GetParameterAsText(1)

try:
    # id message, format: "id 345 arg1,arg2"
    if msgText[:2].upper() != "ID":
      raise Exception
    else:
        msgList = msgText[3:].split()
        msgID = int(msgList[0])
        # pick up arguments, comma-separated
        msgArgs = " ".join(msgList[1:]).split(",")
        msgArgs = [msgType, msgID] + msgArgs
        arcpy.AddIDMessage(*msgArgs)
except:
    # text messages (no ID message)
    if msgType == "WARNING":
        arcpy.AddWarning(msgText)
    elif msgType == "ERROR":
        arcpy.AddError(msgText)
    else:
        arcpy.AddMessage(msgText)
arcpy.SetParameterAsText(2,True)
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
Tags (2)
2 Replies
ShitijMehta
Esri Regular Contributor
Thanks for improving it Curt!!!
I bet it will help a lot of people looking for a way to write a custom error, warning or message.


Point under consideration - about the need for a tool in ModelBuilder to do that.
0 Kudos
LindsayRaabe_FPCWA
Occasional Contributor III

Handy tool! I've just customised to make a simpler version for my own use. Others may find handy. 

#
# GP Script tool - Message
# for use in ModelBuilder

import arcpy

# Two text parameters:
# 0) Message type: ERROR, WARNING, INFORMATIVE
# 1) Message text

msgType = arcpy.GetParameterAsText(0).upper()
msgText = arcpy.GetParameterAsText(1)

# Return msgText and format based on msgType parameter
if msgType.upper() == "ERROR":
  arcpy.AddError(msgText)
elif msgType.upper() == "WARNING":
    arcpy.AddWarning(msgText)
else:
    arcpy.AddMessage(msgText)

LindsayRaabe_FPCWA_0-1704420167908.pngLindsayRaabe_FPCWA_1-1704420198396.png

 

Lindsay Raabe
GIS Officer
Forest Products Commission WA