From 2b658a576c00b6dbf1a23a3ec6687cced4b93b78 Mon Sep 17 00:00:00 2001 From: Armin Briegel <1933192+scriptingosx@users.noreply.github.com> Date: Fri, 13 May 2022 10:44:26 +0200 Subject: [PATCH] added function for JSON parsing using JXA --- fragments/functions.sh | 12 +++++++++++- fragments/labels/firefox.sh | 3 ++- fragments/labels/firefox_da.sh | 3 ++- fragments/labels/firefox_intl.sh | 3 ++- fragments/labels/firefoxesr.sh | 3 ++- fragments/labels/firefoxesr_intl.sh | 3 ++- fragments/labels/firefoxpkg.sh | 3 ++- fragments/labels/firefoxpkg_intl.sh | 3 ++- 8 files changed, 25 insertions(+), 8 deletions(-) diff --git a/fragments/functions.sh b/fragments/functions.sh index 9e9e977..ddbe70f 100644 --- a/fragments/functions.sh +++ b/fragments/functions.sh @@ -22,7 +22,7 @@ cleanupAndExit() { # $1 = exit code, $2 message, $3 level printlog "$2" $3 fi printlog "################## End Installomator, exit code $1 \n" REQ - + # if label is wrong and we wanted name of the label, then return ################## if [[ $RETURN_LABEL_NAME -eq 1 ]]; then 1=0 # If only label name should be returned we exit without any errors @@ -199,6 +199,16 @@ xpath() { fi } +# from @Pico: https://macadmins.slack.com/archives/CGXNNJXJ9/p1652222365989229?thread_ts=1651786411.413349&cid=CGXNNJXJ9 +getJSONValue() { + # $1: JSON string OR file path to parse (tested to work with up to 1GB string and 2GB file). + # $2: JSON key path to look up (using dot or bracket notation). + printf '%s' "$1" | /usr/bin/osascript -l 'JavaScript' \ + -e "let json = $.NSString.alloc.initWithDataEncoding($.NSFileHandle.fileHandleWithStandardInput.readDataToEndOfFile$(/usr/bin/uname -r | /usr/bin/awk -F '.' '($1 > 18) { print "AndReturnError(ObjC.wrap())" }'), $.NSUTF8StringEncoding)" \ + -e 'if ($.NSFileManager.defaultManager.fileExistsAtPath(json)) json = $.NSString.stringWithContentsOfFileEncodingError(json, $.NSUTF8StringEncoding, ObjC.wrap())' \ + -e "const value = JSON.parse(json.js)$([ -n "${2%%[.[]*}" ] && echo '.')$2" \ + -e 'if (typeof value === "object") { JSON.stringify(value, null, 4) } else { value }' +} getAppVersion() { # modified by: Søren Theilgaard (@theilgaard) and Isaac Ordonez diff --git a/fragments/labels/firefox.sh b/fragments/labels/firefox.sh index 7b310e5..2ff72ab 100644 --- a/fragments/labels/firefox.sh +++ b/fragments/labels/firefox.sh @@ -2,7 +2,8 @@ firefox) name="Firefox" type="dmg" downloadURL="https://download.mozilla.org/?product=firefox-latest&os=osx&lang=en-US" - appNewVersion=$(curl -fs https://www.mozilla.org/en-US/firefox/releases/ | grep '