Skip to content

Instantly share code, notes, and snippets.

@mixio
Last active February 1, 2020 08:33
Show Gist options
  • Save mixio/b230c292b8f08fe9c7370eb67b344cf2 to your computer and use it in GitHub Desktop.
Save mixio/b230c292b8f08fe9c7370eb67b344cf2 to your computer and use it in GitHub Desktop.
Applescript for json transform with jq in BBEdit
--
on escape_new_lines(aText)
set vDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to {"\\n", "\\r"}
set vParts to (text items of aText)
set AppleScript's text item delimiters to {"\\\\n"}
set vResult to (items of vParts) as string
set AppleScript's text item delimiters to vDelims
return vResult
end escape_new_lines
--
tell application "BBEdit"
try
tell front window
if (length of (selection as string)) = 0 then
select line (startLine of selection) of first document
end if
set vExpression to the quoted form of my escape_new_lines(contents of selection as string)
tell current application
try
set vCommand to "echo " & vExpression & "| /usr/local/bin/jq '.'" -- Fully qualified path. Change it if incorrect.
log vCommand
set vResult to do shell script vCommand
log vResult
on error
try
set vCommand to "source ~/.bashrc ; echo " & vExpression & "| jq '.'" -- jq should be in bash's PATH.
log vCommand
set vResult to do shell script vCommand
log vResult
on error msg number vErrorNumber
set msg to "Error: " & vErrorNumber & return & msg
if vErrorNumber is 127 then
set msg to msg & return & "----------------------------------" & return & return
set msg to msg & "The path to your jq is probably incorrect!" & return & return
set msg to msg & "Check the path to your jq in terminal with:" & return & return
set msg to msg & " $ which jq" & return & return
set msg to msg & "or" & return & return
set msg to msg & "add the jq path to your ~/.bashrc PATH." & return & return
end if
display dialog msg buttons ["OK"]
return
end try
end try
end tell
copy vResult to selection
display notification "jq: done!"
end tell
on error msg
display dialog msg
end try
end tell
--
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment