Upgrading our naming policy

shiggi
New Contributor

(seriously amateur scripter) looking to upgrade how we manage device naming.

Our naming policy is not complicated, Department-Building-Tag#, and our current script requires us to maintain a separate list of data to populate a dialog box that lets the user select from the available options:

set Department to (choose from list {"AAA", "BBB", "CCC", "DDD", etc.

But that means anytime there is a change in department structures, we need to update the Jamf>Network>Departments information, as well as the script.

 

I am hoping to use API commands to grab the current list of departments when the Naming Policy is triggered something like:

departmentList=$(curl -su username:password\
"https://url/JSSResource/departments" \
-H "accept: xml/text" \
| xmllint -format - \
| awk '/name/ {print}' \
| awk -F '[><]' '{print $3}')

This gets me the data in a list, but I can't figure out how to (or if it is possible to) prompt the user to select from a list just by calling the variable:

...

set Department to (choose from list $departmentList \    

for example.

 

Is it possible to do this?

 

3 REPLIES 3

Mauricio
Contributor III

AppleScript (osascript via shell/bash script) would be a possibility

https://developer.apple.com/library/archive/documentation/LanguagesUtilities/Conceptual/MacAutomatio...

 

Exactly what I am thinking! 

The trick seems to be that: 

set theFruitChoices to {"Apple", "Banana", "Orange"}

Requires the choices to be enumerated, and I am hoping to replace that with something more like:

set theFruitChoices to {"$fruitOptions"}

I am sure that (either this is just not possible with osascript, or) there is a step that I am missing.

Best results I get is a prompt appearing with no options, sometimes an error that shows 'List is empty'.

 

Hello, 

When you get the list of Departments that will be in new lines.

To make that work in the AppleScript a change is required:

departmentList=$(curl -su username:password\
"https://url/JSSResource/departments" \
--header 'Accept: application/xml, application/json' \
| xmllint --format --xpath "//department/name/text()" - \
| sed 'N;s/^/"/;s/\n/","/;s/$/"/'

 

The --xpath will extract the name of the Department in a newline format without the markup text

Then we use sed to add " around the values and replace the new line with comma separated values.

I hope this helps

Regards