Posted on 01-15-2013 02:33 AM
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
Solved! Go to Solution.
Posted on 01-21-2013 07:20 AM
@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.
08-03-2022 06:40 PM - edited 08-03-2022 06:41 PM
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
Posted on 01-15-2013 07:06 AM
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
Posted on 01-15-2013 11:58 AM
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.
07-22-2022 10:26 AM - edited 08-03-2022 06:44 PM
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
Posted on 01-15-2013 12:03 PM
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.
Posted on 01-15-2013 03:06 PM
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.
Posted on 01-21-2013 01:38 AM
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 ;-)
Posted on 01-21-2013 07:20 AM
@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.
Posted on 01-31-2013 08:45 AM
@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!
Posted on 06-12-2020 10:58 AM
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!
Posted on 09-17-2021 09:18 AM
hello all, repoening this topic, this dint work for me 😕
the line breaks get not aplied even with the echo trick
Posted on 12-02-2021 02:12 PM
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.
When I run it from a policy in Jamf the line breaks don't come through, and it shows "\n" for the breaks.
I'm running macOS 12.0.1 and Jamf 10.33.0 if that might have something to do with it.
08-03-2022 06:40 PM - edited 08-03-2022 06:41 PM
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
Posted on 01-18-2023 07:33 AM
[One hundred sixty-nine days later …]
Using Unicode Character U+2028 is working well for us.
02-24-2023 09:43 AM - edited 02-24-2023 09:47 AM
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
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