Apple Script to execute terminal command and display results

756 views Asked by At

I am totally newbie with Apple script (and scripts in general). I have a Terminal command which uses cfgutil to display informations about a lot of iPad.

The command is as follows:

./cfgutil -f --format JSON get-property name firmwareVersion batteryCurrentCapacity passcodeProtected configurationProfiles installedApps

cfgutil is inside Apple Configurator 2 app

/Applications/Apple\\ Configurator\\ 2.app/Contents/MacOS/cfgutil

I made an Apple Script to generate a text file from a Python script.

on run {input, parameters}
    tell application "Terminal"
        activate
        do script with command "/Applications/Apple\\ Configurator\\ 2.app/Contents/MacOS/cfgutil -f --format JSON get-property name firmwareVersion batteryCurrentCapacity passcodeProtected configurationProfiles installedApps | python3  /Users/admin/Desktop/jsonpy3.py > /Users/admin/Desktop/Check.txt | top; exit"
    end tell
end run

The Python script (thx to flaf):

#!/usr/bin/env python3

import sys
import json

human_readable_fields = {
    'name': 'Nom',
    'firmwareVersion': 'Firmware',
    'batteryCurrentCapacity': 'Charge',
    'passcodeProtected': 'Passcode',
    'configurationProfiles': 'Profils',
    'installedApps': 'Applications',
}

template = ' '.join(
  ['{name:<25.25}', # name will be truncated to 25 characters.
   '{firmwareVersion:>10}',
   '{batteryCurrentCapacity:>10}',
   '{passcodeProtected:>15}',
   '{configurationProfiles:>10}',
   '{installedApps:>15}']
)

data = json.loads(''.join(sys.stdin.readlines()))
device_ids = data['Devices']

devices = []

for device_id in device_ids:

    d = data['Output'][device_id]

    device = {'id': device_id,
              'name': d['name'],
              'firmwareVersion': d['firmwareVersion'],
              'batteryCurrentCapacity': d['batteryCurrentCapacity'],
              'passcodeProtected': d['passcodeProtected'],
              'configurationProfiles': len(d['configurationProfiles']),
              'installedApps': len(d['installedApps'])}

    devices.append(device)

# The header.
print(template.format(**human_readable_fields))

for device in devices:

    if device['passcodeProtected']:
        device['passcodeProtected'] = 'activé'
    else:
        device['passcodeProtected'] = 'désactivé'

    # A device.
    print(template.format(**device))

But I would prefer an only Apple Script that display results in a popup window. Is there a good person to help me ? :)

0

There are 0 answers