Problems using osascript Display Dialog with -event policy

HenryOzsoy
New Contributor III

I want to run a bash script that uses osascript display dialog to prompt the current user for with 3 options. Base on the user's choice I want the script to call on custom triggers I have set for jamf policies. Unfortunately, it doesn't execute the jamf -event command without an admin password. I looked into jamfhelper yet I need 3 button option. Is there a different approach I can use or would I need change the script?

#!/bin/sh
/usr/bin/osascript <<-EndOfScript
tell application "Finder"
    activate
display dialog "In order for the workflow to complete, please indicate who this device is being prepared for." buttons {"Red", "Blue", "Orange"} with icon stop with title "Important!"
    if button returned of result = "Red" then
         do shell script "/usr/local/bin/jamf policy -event redpolicy"
else
    if button returned of result = "Blue" then
         do shell script "/usr/local/bin/jamf policy -event redpolicy; /usr/local/bin/jamf policy -event bluepolicy"
else
    if button returned of result = "Orange" then
         do shell script "/usr/local/bin/jamf policy -event redpolicy; /usr/local/bin/jamf policy -event orangepolicy"

                end if
            end if
        end if
    end tell
EndOfScript

Thanks in advance.

1 ACCEPTED SOLUTION

HenryOzsoy
New Contributor III

Thanks @Tangentism ,

Before your post, I was actually tinkering around and had come to the same conclusion.

I had to swap from display dialog to choose from list as the display dialog output would not refelct in my if statements.

Here is what I ended up with,

#!/bin/sh

#Workflow Chooser
# Will prompt a choose from list box to the tech preparing device so they can indicate who the machine is being prepped for, 
# then will initiate the final workflow policies in JSS according to the Tech's output.

#Logged in user info and UID
loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u $loggedInUser)

#chose from list box as a variable
computerType=$(/bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser << EOF
/usr/bin/osascript -e 'tell application "System Events"
activate
choose from list {"Student Device", "Staff Device @X", "Staff Device @Y"}
end tell' 2>/dev/null
EOF
)

#if (( $? ));

if [[ "$computerType" == "Student Device" ]]  ; then 
    /usr/local/bin/jamf policy -event finalconfig
fi

if [[ "$computerType" == "Staff Device @X" ]]  ; then 
    /usr/local/bin/jamf policy -event finalconfig; /usr/local/bin/jamf policy -event @X
fi

if [[ "$computerType" == "Staff Device @Y" ]]  ; then
    /usr/local/bin/jamf policy -event finalconfig; /usr/local/bin/jamf policy -event @Y
fi
exit 0

Works great.

View solution in original post

3 REPLIES 3

mschroder
Valued Contributor

If I remember correctly osascripts are always executed as the user, not as root, whereas all other scripts are running as root. I think the easiest way out is to use a shell script that uses osascript only to get the feedback from the user.

Tangentism
Contributor II

Why not have the Applescript as a bash variable so the result is returned? It would be more simple and adhere to DRY principles as well.

Something along the lines of....

#!/usr/bin/env bash

button=$(/usr/bin/osascript << EOF
tell application "Finder"
    activate
display dialog "In order for the workflow to complete, please indicate who this device is being prepared for." buttons {"Red", "Blue", "Orange"} with icon stop with title "Important!"
end tell
EOF)

# run policy with returned value
/usr/local/bin/jamf policy -event "$button"

HenryOzsoy
New Contributor III

Thanks @Tangentism ,

Before your post, I was actually tinkering around and had come to the same conclusion.

I had to swap from display dialog to choose from list as the display dialog output would not refelct in my if statements.

Here is what I ended up with,

#!/bin/sh

#Workflow Chooser
# Will prompt a choose from list box to the tech preparing device so they can indicate who the machine is being prepped for, 
# then will initiate the final workflow policies in JSS according to the Tech's output.

#Logged in user info and UID
loggedInUser=$(stat -f%Su /dev/console)
loggedInUID=$(id -u $loggedInUser)

#chose from list box as a variable
computerType=$(/bin/launchctl asuser $loggedInUID sudo -iu $loggedInUser << EOF
/usr/bin/osascript -e 'tell application "System Events"
activate
choose from list {"Student Device", "Staff Device @X", "Staff Device @Y"}
end tell' 2>/dev/null
EOF
)

#if (( $? ));

if [[ "$computerType" == "Student Device" ]]  ; then 
    /usr/local/bin/jamf policy -event finalconfig
fi

if [[ "$computerType" == "Staff Device @X" ]]  ; then 
    /usr/local/bin/jamf policy -event finalconfig; /usr/local/bin/jamf policy -event @X
fi

if [[ "$computerType" == "Staff Device @Y" ]]  ; then
    /usr/local/bin/jamf policy -event finalconfig; /usr/local/bin/jamf policy -event @Y
fi
exit 0

Works great.