Tuesday, April 13, 2021

#1 2021-04-05 01:04:59 pm

Fredrik71
Member
Registered: 2019-10-23
Posts: 666

Terminal commands in JSON to AppleScript Record

Many command-line tools use JSON or other format to output status, logging or other kind of info descriptions. It could be we set some option and output to stdout in JSON form or we could write
JSON data and read it back in.

Here is some examples some is standard commands and other is user installed.

PS. This list is limited and there is properly hundreds or more to add to it.

The purpose is to show other way to get data there you as user could control it.

Every example return AppleScript Record

Applescript:

use framework "Foundation"
use scripting additions

(**
* [Shell command]: AssetCacheLocatorUtil
*)

set jsonString to do shell script "AssetCacheLocatorUtil --json"
-- return my recordWithJSONString:jsonString

(**
* [Shell command]: AssetCacheManagerUtil (status)
*)

set jsonString to do shell script "AssetCacheManagerUtil status --json"
-- return my recordWithJSONString:jsonString

(**
* [Shell command]: AssetCacheManagerUtil (settings)
*)

set jsonString to do shell script "AssetCacheManagerUtil settings --json"
-- return my recordWithJSONString:jsonString

(**
* [Shell command]: AssetCacheTetheratorUtil (status)
*)

set jsonString to do shell script "AssetCacheTetheratorUtil --json status"
-- return my recordWithJSONString:jsonString

(**
* [Shell command]: footprint (--pid)
*)

set theTmp to POSIX path of (path to temporary items) & "footprint.json"
set thePID to item 1 of pidWithAppName("Finder")
set jsonString to do shell script "footprint --pid " & thePID & " --json " & theTmp
-- return my recordWithJSONString:(read theTmp)

(**
* [Shell command]: brew (list --formula)
*)

set theFormulaList to do shell script "brew list --formula"
set theItems to every paragraph of theFormulaList
set formulaString to (choose from list theItems) as text
set jsonString to do shell script "brew info --json " & quoted form of formulaString
-- return my recordWithJSONString:jsonString


on pidWithAppName(appName)
   tell application "System Events" to set thePID to unix id of every process whose name is appName
end pidWithAppName

on recordWithJSONString:jsonString
   return (current application's NSJSONSerialization's JSONObjectWithData:((current application's NSString's stringWithString:jsonString)'s dataUsingEncoding:(current application's NSUTF8StringEncoding)) options:0 |error|:(missing value)) as record
end recordWithJSONString:


if you are the expert, who will you call if its not your imagination.

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)