1

I've got a script working but not the way I want. Here's the script

return do shell script "BLUETOOTH_DEFAULTS=$(defaults read /Library/Preferences/com.apple.Bluetooth); SYSTEM_PROFILER=$(system_profiler SPBluetoothDataType); MAC_ADDR=$(grep -b2 \"Minor Type: Headphones\"<<<\"${SYSTEM_PROFILER}\"|awk '/Address/{print $3}'); CONNECTED=$(grep -ia6 \"${MAC_ADDR}\"<<<\"${SYSTEM_PROFILER}\"|awk '/Connected: Yes/{print 1}'); BLUETOOTH_DATA=$(grep -ia6 '\"'\"${MAC_ADDR}\"'\"'<<<\"${BLUETOOTH_DEFAULTS}\"); BATTERY_LEVELS=(\"BatteryPercentCombined\" \"HeadsetBattery\" \"BatteryPercentSingle\" \"BatteryPercentLeft\" \return\"BatteryPercentRight\"); if [[ \"${CONNECTED}\" ]]; then for I in \"${BATTERY_LEVELS[@]}\"; do declare -x \"${I}\"=\"$(awk -v pat=\"${I}\" '$0~pat{gsub (\";\",\"\"); print $3 }'<<<\"${BLUETOOTH_DATA}\")\"; [[ ! -z \"${!I}\" ]] && OUTPUT=\"${OUTPUT} $(awk '/BatteryPercent/{print substr($0,15,1)\": \"}'<<<\"${I}\")${!I}%\"; done; printf \"%s\\n\" \"${OUTPUT}\"; else printf \"%s Not Connected\\n\" \"${OUTPUT}\"; fi" # Version 2.3

It returns:

L: 82% R: 88%

What I want it to return is:

L: 82%
R: 88%

I can't for the life of me figure out how to add the break. I've tried \n, \t, \r, & return &,... nothing works. This is probably just my ignorance though. I know enough to get myself in trouble.

Any thoughts?

  • Does it work if you just run the shell script part directly in Terminal? Does return do shell script "echo L; echo R" do what you want? – nohillside Jul 19 at 5:46
1

In the correct context, Unix uses \n or 0x0A.

mac $ echo -e ".\nhi\n."
.
hi
.
mac $ 

If you use command substitution ($(...)) newlines will be stripped. From man bash:

Bash performs the expansion by executing command and replacing the command substitution with the standard output of the command, with any trailing newlines deleted. Embedded newlines are not deleted, but they may be removed during word splitting.

# avoid bash from messing with string, since $z is quoted in the echo. 
mac $ z=$(echo -e "L: 82%\nR: 88%")
mac $ echo "$z"
L: 82%
R: 88%
mac $ 

# With command splitting since the variable $Z isn't in quotes. 
mac $ z=$(echo -e "L: 82%"; echo -e "R: 88%")
mac $ echo $z
L: 82% R: 88%

mac $ echo -e "L: 82%"; echo -e "R: 88%"
L: 82%
R: 88%
mac $ 

I'd just parse the output and add in the Applescript reserve word return. assume the Unix output is in fromUnix

global debug
set debug to 0

log alterString(fromUnix, "%", "%"&return)

-- ------------------------------------------------------
(*
alterString
  thisText is the input string to change
  delim is what string to change.  It doesn't have to be a single character.
  replacement is the new string

  returns the changed string. 
*)

on alterString(thisText, delim, replacement)
    global debug
    if debug ≥ 5 then log "in ~~~ alterString ~~~"
    if debug ≥ 6 then
        hexDumpFormatOne("alterString: input text, thisText,", thisText)
        log "alterString: delim is -->" & delim & "<--"
        hexDumpFormatOne("alterString: input search string, delim,", delim)
        log "alterString: replacement is " & replacement
    end if
    set resultList to {}
    set {tid, my text item delimiters} to {my text item delimiters, delim}
    try
        set resultList to every text item of thisText
        set text item delimiters to replacement
        set resultString to resultList as string
    on error
        log "alterString: ==>error altering the string. "
        set resultString to thisText
    end try
    set my text item delimiters to tid
    if debug ≥ 6 then hexDumpFormatOne("alterString: resultString", resultString)
    return resultString
end alterString
  • Who or where is this word splitting being done? What a mess: mac $ z=$(ls -l); mac $ echo $z – historystamp Jul 19 at 17:59
  • Your shell performs "word splitting". What happens when you prevent word splitting on the variable z ? -- echo "$z" – fd0 Jul 19 at 18:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .