line break in jamfHelper message string?

hansjoerg_watzl
Contributor II

Hello
I want to pass a multiline description as a parameter from the policy to a script.
The script is using the jamfHelper app.

How can I define a manual line break in the description parameter?

btw, is there a way to get the policy name or id to a script?

thanks

2 ACCEPTED SOLUTIONS

mm2270
Legendary Contributor III

@hansjoerg.watzi

Ah, but actually you can make the \n work in the script. You just have to know how to get the script to recognize it. See the following test script below for how to make it work:

#!/bin/sh

## jamfhelper line break test

## Path to JAMFHelper.app
jhPath="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"

## Description passed in from parameter set in the policy
Desc="$4"

## The new message variable passed to jamfHelper
MSG=$( echo "$Desc" )

sudo "$jhPath" -windowType utility -title "My Title" -description "$MSG" -button1 "OK" -defaultButton 1

The trick above is to use 'echo' to set up a new variable from the one passed in the parameter. This works because echo will recognize the newline character, \n, and translate it into actual line breaks as it sets up the final message variable to pass into jamfHelper. I haven't tested it, but I think using printf may also do the trick here.

I just ran this through a quick test and setting up $4 with the line:

"This is the first  line of the message.\
This is the second line in the message.\
This is the third line in the message."

Actually gave me something like this in the jamfHelper window-

This is the first line of the message.
This is the second line in the message.
This is the third line in the message.

I can even do this in the message to get a blank line between:

This is line 1.\
\
This is line 2.

Result:
**This is line 1.

This is line 2.**

Give it a try. It seems to work for me.
The cool thing about this is its pretty flexible because if your message doesn't have any line break characters in it, it doesn't matter. It also doesn't matter how many lines you need. You can add as many as you like in one parameter, and it also doesn't use more than one parameter in the policy.

As for getting html messages with formatting and background images, etc, sorry, can't help you there ; )

Edit: Quick additional note. The command 'sudo jamf runScript -script "ScriptName.sh" -path, etc seems to handle line breaks and other escaped stuff differently than when passing a parameter to a script from a policy. In doing some tests with the above example, I found I didn't need to have an escaped new character in the parameter, such as \n. Doing just worked, whereas the other did not. It was the opposite when using a direct jamf runScript call. Same if I wanted to quote some text in the message. Trying to escape the quotes like "quoted text" failed, but doing "Quoted text" directly in the parameter field worked fine. Just thought I'd mention that in case you run into the same thing.

View solution in original post

jonwithnoh
New Contributor

I was able to make it work by changing this line

 

MSG=$( echo "$Desc" )

 

 in the accepted answer to this

 

MSG=$( printf "$Desc" )

 

 Tested in macOS Monterey and the macOS Ventura Beta

View solution in original post

14 REPLIES 14

mm2270
Legendary Contributor III

Are you talking about the parameters defined inside a policy when you add the script in? If so, I'm not sure there's a way to do that. You can try adding the new line symbol ( ) and see that if that works. You might need to escape it by doing a double backslash \n though.

Edit: Regarding getting the policy name or ID to a script, I had asked a similar question awhile ago and never got any responses to my question. I don't know how to do that, or if its possible. The only thing I can think of is at the exact time the script is running to parse the /var/log/jamf.log file for the current running policy name. Something like this should give you the name of the running policy, unless for some reason you have 2 policies running one right after the other, but that generally never happens.

cat /var/log/jamf.log | awk -F "Policy " '/Executing/{ print $2 }' | tail -1

themacdweeb
Contributor

since the alert description is in double quotes, you can simply include a carriage return in your script. we do this all the time:

#!/bin/sh

icon="/path/to/your/icons/icon.png"

/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper -windowType fs -icon $icon -heading "Macintosh Alert:" -alignHeading center -description "Please stand by... Adobe updates are currently being installed on your Mac.

You will be notified when the installation is complete." &

exit 0

we've also got scripts that set many of the jamfHelper functions to variables. that way we just change that parameter/variable from script to script. easier management = more saved time.

Carriage returns solved my issue with \n not working in macOS 12.5 and Jamf Helper. We are also on Jamf Pro 10.34.2

Edit: Oddly enough I ran into this issue again with a new script I was working on. To get it to work I used printf instead of echo in this line

MSG=$( printf "$Desc 

 

mm2270
Legendary Contributor III

Yeah, I do that all the time as well. I think (not 100% sure) that the OP was asking how to add that description with a line break into the script parameter field inside a policy when you add in the script to said policy, as in $4, etc. That field doesn't accept line breaks. If its coded into the script its easy. Not as easy though if trying to pass it via a parameter.

I haven't tried including a line break character in it to see it works. I don't have a script currently up on my JSS that I can test that with.

mm2270
Legendary Contributor III

So, just thinking this through on one side of my brain as I do other things, I think the following may work, though its kind of crude. Set up 2 parameters (in addition to any others you need) for your script and when its in the JSS. Call them Desc1 and Desc2 for example.

In your actual script that displays the message, do something like this:

#!/bin/sh

jhPath="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"

Title="$4"
Desc1="$5"
Desc2="$6"
Icon="$7"

Msg="$Desc1

$Desc2"

"$jhPath" -windowType utility -title "$Title" -description "$Msg" -button1 "OK" -icon "$Icon"

In my super quick test just now, I was able to pass 2 separate parameters to it that combined into one description for the message, with a break in the lines between them.

Not as nice as being able to enter it directly into the JSS policy in one field, but it works (as long as you don't require a 3rd line or something)
You might also need to do a quick if/then check to make sure both parameters are assigned before setting the $Msg variable, or set it appropriately if only $Desc1 is actually assigned, for example.

hansjoerg_watzl
Contributor II

Unfortunately, or didn't work.
The combined multi variables approach should work, but it's not very flexible and I have not enough free script parameter fields for this.

Best would be a web/html capable jamfHelper, so we could pass whatever we want to show. (Including formatting, more images, background image and so on...)
Just dreaming ;-)

mm2270
Legendary Contributor III

@hansjoerg.watzi

Ah, but actually you can make the \n work in the script. You just have to know how to get the script to recognize it. See the following test script below for how to make it work:

#!/bin/sh

## jamfhelper line break test

## Path to JAMFHelper.app
jhPath="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"

## Description passed in from parameter set in the policy
Desc="$4"

## The new message variable passed to jamfHelper
MSG=$( echo "$Desc" )

sudo "$jhPath" -windowType utility -title "My Title" -description "$MSG" -button1 "OK" -defaultButton 1

The trick above is to use 'echo' to set up a new variable from the one passed in the parameter. This works because echo will recognize the newline character, \n, and translate it into actual line breaks as it sets up the final message variable to pass into jamfHelper. I haven't tested it, but I think using printf may also do the trick here.

I just ran this through a quick test and setting up $4 with the line:

"This is the first  line of the message.\
This is the second line in the message.\
This is the third line in the message."

Actually gave me something like this in the jamfHelper window-

This is the first line of the message.
This is the second line in the message.
This is the third line in the message.

I can even do this in the message to get a blank line between:

This is line 1.\
\
This is line 2.

Result:
**This is line 1.

This is line 2.**

Give it a try. It seems to work for me.
The cool thing about this is its pretty flexible because if your message doesn't have any line break characters in it, it doesn't matter. It also doesn't matter how many lines you need. You can add as many as you like in one parameter, and it also doesn't use more than one parameter in the policy.

As for getting html messages with formatting and background images, etc, sorry, can't help you there ; )

Edit: Quick additional note. The command 'sudo jamf runScript -script "ScriptName.sh" -path, etc seems to handle line breaks and other escaped stuff differently than when passing a parameter to a script from a policy. In doing some tests with the above example, I found I didn't need to have an escaped new character in the parameter, such as \n. Doing just worked, whereas the other did not. It was the opposite when using a direct jamf runScript call. Same if I wanted to quote some text in the message. Trying to escape the quotes like "quoted text" failed, but doing "Quoted text" directly in the parameter field worked fine. Just thought I'd mention that in case you run into the same thing.

hansjoerg_watzl
Contributor II

@mm2270
Thanks! You're post was my highlight of the day!

It worked with from passing a parameter to a script from a policy. That's what I needed most.

I tried your:

## Description passed in from parameter set in the policy
Desc="$4"

## The new message variable passed to jamfHelper
MSG=$( echo "$Desc" )

to this:

MSG=$( echo "$4" )

And it worked too.

Thanks again!

jtrant
Valued Contributor

This was extremely helpful. I customized the script a little so I can edit the title, and use it for all my jamfHelper notifications.

Thank you for putting this together!

jpoirson
New Contributor III

hello all, repoening this topic, this dint work for me 😕

 

 

the line breaks get not aplied even with the echo trick

cwaldrip
Valued Contributor

curious... I'm sure this worked when I first setup my script. but now the new lines aren't registering.

Here's my script...

#!/bin/sh
jhPath="/Library/Application Support/JAMF/bin/jamfHelper.app/Contents/MacOS/jamfHelper"
Title="$4"
Head="$5"
Desc="$6"
Button1="$7"
Icon="$8"
if [ "$8" == "Alert" ];then
Icon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/Actions.icns"
elif [ "$8" == "Caution" ];then
Icon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertCautionIcon.icns"
elif [ "$8" == "Note" ];then
Icon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertNoteIcon.icns"
elif [ "$8" == "Stop" ];then
Icon="/System/Library/CoreServices/CoreTypes.bundle/Contents/Resources/AlertStopIcon.icns"
fi
echo "$Icon"
msg=$(echo "$Desc")
"$jhPath" -windowType utility -title "$Title" -heading "$Head" -alignHeading center -description "$msg" -button1 "$7" -icon "$Icon"

When I run the command from a local script...

./test.sh null null null "Warning" "" "Long bit of text with \\n\\n to add two line breaks between paragraphs" "Understood" "Alert"

...the line breaks come through.

Screen Shot 2021-12-02 at 5.06.47 PM.png

When I run it from a policy in Jamf the line breaks don't come through, and it shows "\n" for the breaks.

Screen Shot 2021-12-02 at 5.07.57 PM.png

I'm running macOS 12.0.1 and Jamf 10.33.0 if that might have something to do with it. 

jonwithnoh
New Contributor

I was able to make it work by changing this line

 

MSG=$( echo "$Desc" )

 

 in the accepted answer to this

 

MSG=$( printf "$Desc" )

 

 Tested in macOS Monterey and the macOS Ventura Beta

dan-snelson
Valued Contributor II

[One hundred sixty-nine days later …]

Using Unicode Character U+2028 is working well for us.

brunerd
Contributor

I've recently been sussing out the behaviors of echo in various shells, I think it important to note the differences in echo across the carious 

  • sh and that weirdo dash: echo <string>
    • echo takes only arguments to print, no other options, it prints everything
  • bash: echo -e <string>
    • without -e it will not process escaped text
    • -E will explicitly prevent processing escapes
    • fun fact, if your text starts with - you better encode it so it doesn't think it's an option!
  • zsh: echo <string>
    • can also accept -e without printing it (unlike dash and sh)
    • -E will prevent processing escapes

I've also created a new tool shef to escape your text, whether it's newlines, tabs, or other exotic whitespace like @dan-snelson 's U+2028 (line separator, which is ) shef can encode it for use as Jamf parameter (or shell variable), which btw encodes to 3 bytes in UTF-8

 

% shef <<< $'\U00002028'
\xE2\x80\xA8

 

 

You can then reconstitute an encoding string using one of the above echo methods depending on your shell and capture the results to a variable. By default shef encodes in hex and escapes for "non-shell" tools like Jamf, like so (apparently this code snippet killed my Unicode!):

 

% shef <<EOF
Hope this helps. 
Rock on! 
EOF
Hope this helps. \xF0\x9F\x98\x85\nRock on! \xF0\x9F\xA4\x98

 

Some more examples and writing about that here: Avoid Unicode Mangling in Jamf with shef