- Sun 25 April 2021
- Automation
- Gaige B. Paulsen
- #automation
Occasionally, I get requests for new features that are very simple, and might take longer to deploy in the code base than it would take to make them available through AppleScript. In many of these cases, over the years, I've gone ahead and written and distributed AppleScript to meet these needs.
A couple of days ago, a user reached out needing st set a specific column in new rows to a particular value. The general case of this requires providing a way to choose the column to be set, the value to be set, and which rows to set it in.
However, if you know the value and column names will remain constant, the only information you need is a list of rows, and that's simple to provide by selecting them.
Here's the final script
-- This script sets the SURVEYOR column to the name specified in columnValue
tell application "Cartographica"
-- set my variables here
set targetColumnName to "SURVEYOR"
set columnValue to "Somebody"
-- Get Map information
set mapWindow to the first map window
set thedoc to the document of mapWindow
set theViewer to the map viewer of mapWindow
-- Get selected layer
set selectedLayers to the selected layers of theViewer
set originalLayer to the first item in selectedLayers
-- get selected features
set selectedFeatures to selected features of theViewer
-- set the value of specific coluumn in data
repeat with aFeature in selectedFeatures
set theFieldData to aFeature's field data targetColumnName
set value of theFieldData to columnValue
end repeat
end tell
Walking though this, the code is pretty straightforward:
- specify column and value
- connect to Cartographica and get the selected layer and features
- loop over the features, setting the column value to the prescribed value
This looks a little animated when it runs because each set triggers an update to the map and data grid. For very large selections, it can be better to uncheck the layer's visibility box before running the script, since that will only update the data view.
Also note that you need to use the name of the column, not the title. These are frequently the same, but if you're uncertain, show the Layer Info Window and check the Show Field Names box and use the value in the Field Name column as opposed to the Display Name column if they differ.