Skip to content

Instantly share code, notes, and snippets.

@codingChewie
Last active March 26, 2021 16:23
Show Gist options
  • Save codingChewie/50fac27fda21c2a30ea9990dd705baee to your computer and use it in GitHub Desktop.
Save codingChewie/50fac27fda21c2a30ea9990dd705baee to your computer and use it in GitHub Desktop.
AppleScript to unminify JSON into a new JSON file
(*
Date: 16-10-21
Developer: codingChewie
Purpose: Takes a minified JSON file and un-minifies it
Version: 1.0
Name: json-unminify.scpt
Site: http://programmingmonkeys.com/
*)
tell application "Finder"
set scriptTitle to "JSON unminify" as text
try
## prompts for JSON file
set jsonLocation to choose file with prompt "Please select the JSON file" of type {"public.json"}
## gets JSON file path and filename
## set jsonPath to POSIX path of jsonLocation
set jsonPath to POSIX path of (parent of jsonLocation as string)
## gets just the JSON filename
set jsonFile to name of file jsonLocation
## call system events to get the file extension
tell application "System Events"
set jsonExt to name extension of jsonLocation
end tell
## test if JSON file is selected
if jsonExt is equal to "JSON" then
display notification "JSON file selected" with title scriptTitle
set AppleScript's text item delimiters to "."
if jsonFile contains "." then
set {fileName, nameExt} to {text 1 thru text item -2, text item -1} of jsonFile
set jsonNew to fileName & "_unminify" as text
do shell script "cd /" & jsonPath & "; python -m json.tool " & jsonFile & " " & jsonNew & "." & jsonExt
end if
else
## show error if file type isn't JSON
display dialog "The file chosen is not JSON" with title scriptTitle
error number -128
end if
on error error_message number error_number
if error_number is equal to -128 then
display notification "Folder prompt cancelled" with title scriptTitle
else
display dialog error_message with title scriptTitle
end if
end try
end tell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment