Script running in Self Service to Prompt user for input

DBrowning
Valued Contributor II

Anyone been able to do such a thing?

Have a script that when run outside of Self Service it works fine and prompts for input. When running via SS, it doesn't prompt the user.

11 REPLIES 11

alexjdale
Valued Contributor III

Yes, I use CocoaDialog to prompt for user input. How are you prompting for it? A read-type command?

joshuasee
Contributor III

Note that if you're using AppleScript, 10.9 and up have tightened security in a way that makes osascript trickier to use. You'll want to code something like launchctl bsexec console_pid osascript -e 'tell app "Self Service" to display dialog "Interact with me"'

mm2270
Legendary Contributor III

Yeah, the likely issue is that you're using AppleScript, which is designed and expected to be run by the user. When a script gets run from Self Service, its run as root, and the OS will throw an error since you're (unknowingly) asking the root user to display a dialog to the logged in user.

DBrowning
Valued Contributor II

I am using shell script

DBrowning
Valued Contributor II

@alexjdale yes I am using a read command.

!/bin/sh

echo "Is this a Laptop (l) or Desktop (w)?:"
read type
echo "What is the location of this machine? (BUR, DAL, NHP, etc):"
read location
OS=Mac

sn=$(system_profiler SPHardwareDataType | awk '/Serial/ {print $4}')
newsn=$(echo "${sn:4}")

name=$type$location$OS$newsn

stevewood
Honored Contributor II
Honored Contributor II

Like @alexjdale, I use CocoaDialog for this as well.

joshuasee
Contributor III

Shell scripts don't have really have concept of a GUI. The read builtin expects to work within a terminal session. Opening a terminal window would be possible, but ugly and it would carry security risks. CocoaDialog, Pashua, or AppleScript are your best bets for this sort of interaction.

joshuasee
Contributor III

Brief AppleScript in bash example:

#!/bin/bash

/bin/launchctl bsexec $(who -u | sed -n s/'.*.console.*[ 	]'//p) /usr/bin/osascript -e 'tell app "Self Service" to display dialog "Enter a location (BUR, DAL, NHP, etc) and then click the type of this computer." buttons {"Laptop","Desktop"} default answer "etc"'

You could use the AppleScript choose from list function to ensure valid locations.

Look
Valued Contributor III

Similar to @joshuasee Here is what I used to ask for a printer location.

LOCATION=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set the answer to text returned of (display dialog "Enter a FLOOR or ROOM or PRINTER.
ie: AA1 or AA123 or AA123-P455
Only general use printers may be added." default answer "" buttons {"Continue"})
end tell
END)

bentoms
Release Candidate Programs Tester

You can also upload an AppleScript.

No need for faffing around with bash to AppleScript to bash..

Instead faff around with AppleScript to bash.

tlarkin
Honored Contributor

Here is and example of what I have used for returning elements in a bash script from Apple script

getSite() {

theSite=$(osascript <<AppleScript

set mySites to {"Minneapolis", "Cupertino", "New York", "Hong Kong", "Amsterdam", "Eau Claire"}

set selectedSite to {choose from list mySites}

AppleScript

echo "${theSite}"
)
}

getSite

echo "${theSite}"

This is what it looks like:

8eb801a3e57d425f9dc3238bfd1017b1