Apple脚本执行终端命令和显示结果

时间:2017-07-20 11:44:28

标签: applescript apple-configurator

我完全熟悉Apple脚本(以及一般的脚本)。 我有一个使用 cfgutil 的终端命令来显示有关很多iPad的信息。

命令如下:

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

cfgutil在Apple Configurator 2应用程序中

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

我制作了一个Apple脚本,用于从Python脚本生成文本文件。

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

Python脚本(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))

但我更喜欢仅在弹出窗口中显示结果的Apple脚本。 有一个好人帮助我吗? :)

0 个答案:

没有答案