Variables in Scripts

Not applicable

Hello,

Does anyone know if there is a way in the jamf cli to run a script where instead of hard coding the username and password, the person running the script gets prompted to enter these credentials?

I know that there are the variables that can be used, however I was looking for functionality that prompts via the CLI or the GUI to enter these credentials.

My ideal is a script that will change the computer name, prompting the tech to enter it manually, and then a command to bind to AD, prompting the tech again for their credentials.

Thank You,
Matt Oclassen
Associate Configuration and Release Engineer, Client Engineering
415.536.4869- office
415.216.9955 - mobile
![external image link](attachments/6fb84931878f4146ad37d50829a4b8fa)

8 REPLIES 8

bentoms
Release Candidate Programs Tester

AppleScript is great for this.

You can also wrap shell scripts in them.

Regards,

Ben.

jarednichols
Honored Contributor

You can prompt for it with AppleScript and then run the script from within the AppleScript. Though, I've found passing variables between applescript and shell script to be dicey. Maybe it's just because I find AppleScript old and busted and don't want to take the time to work out its idiosyncrasies.

j
---
Jared F. Nichols
Desktop Engineer, Client Services
Information Services Department
MIT Lincoln Laboratory
244 Wood Street
Lexington, Massachusetts 02420
781.981.5436

tlarkin
Honored Contributor

Python does this very well, however, I am still a python noob....

![external image link](attachments/dbc9071a46744ed2b0e5dbeb5c9e3ea7)

Jak
New Contributor III

Hi,

This a snipped of some code I've written, this uses Applescript for input and passes the value back to the shell.

gettext()
{
lpass=`arch -i386 /usr/bin/osascript << EOT
tell app "System Events"
Activate
set foo to text returned of (display dialog "Enter some text" buttons {"OK"} default answer "sometext" )
end tell
EOT`

echo $lpass
fi
}

Call the function in your script with what-ever variable you want e.g:

textineed=gettext

You may need a cancel button and if so you will need to trap that in the function.

For passwords you can use: default answer "" with hidden answer

Hope that helps, I've never had any issue passing variable from Applescript to bash.

Jak.

rmanly
Contributor III

I tried popping up an AppleScript window to prompt for the name change
script that I posted a few days ago so that my guys could run it from Self
Service as well.

It has been a week or so since I tried that method but I remember getting a
bunch of errors that basically wouldn't allow the window to be displayed. I
even tried running the console trick and opening the window as that user to
no avail.

Anyone know how to get around this?

I do have a script that pops an AppleScript dialog box with WiFi and
Ethernet IPs listed for users and that worked on the first try...I don't
know why one worked and the other didn't.

I haven't investigated this yet on my own just tossing this out here.

Ryan M. Manly
Glenbrook High Schools

![external image link](attachments/17fb818b08314c46a05ee4f957534b88)

tlarkin
Honored Contributor

Can you post the script? To get the output from an apple script to a shell script you will have to do some redirection with your outputs. I had help from a long bearded Unix wizard on this, but you can redirect Apple Script output to be able to read it with in a shell script. And it can have user interaction. I cannot take credit for this as I had help, and I also cannot say it will work. Here is the example script this guy helped me come up with

#!/bin/bash -
IFS=$' '
declare -x PATH=/bin:/usr/bin:/sbin:/usr/sbin

message='Hello World!'
ASerror=1 # assumes worst case to err on the side of caution

# function to parse the string (1st arg is the full text, 2nd arg is the desired field):
parse () { echo "$1" |sed '1!d;s/^.'"$2"':([^,]).*$/1/'; }

:
# (*your* other script lines)
:

# here's where we present a dialog...
# redirecting fd2 to dump AppleScript's error text when Cancel is clicked:
reply=$(2>/dev/null /usr/bin/osascript <<-AppleSkript
tell application "System Events"
set frontApp to (name of (some process
whose frontmost is true)) as text end tell
tell application frontApp
display dialog "$message" with icon note end tell
AppleSkript
)
ASerror=$? # capture exit status!

# dialog is dismissed, so what happened?
clicked=$(parse "$reply" 'returned')

# seems best to test for cancel click via exit code FIRST:
if [ $ASerror -ne 0 ]
then
echo 'use hit cancel'
# do whatever

else
echo "user clicked on: $clicked"
# do whatever
fi
# I used the dialog's status next just for test purposes...
exit $ASerror # <-- put whatever exit code you like instead

![external image link](attachments/94435789e23142769fbc6a0f5b828c96)

tlarkin
Honored Contributor

oh man that is mangled....resending

#!/bin/bash - IFS=$' ' declare -x PATH=/bin:/usr/bin:/sbin:/usr/sbin

message='Hello World!' ASerror=1 # assumes worst case to err on the side of caution

# function to parse the string (1st arg is the full text, 2nd arg is the desired field): parse () { echo "$1" |sed '1!d;s/^.'"$2"':([^,]).*$/1/'; }

: # (*your* other script lines) :

# here's where we present a dialog... # redirecting fd2 to dump AppleScript's error text when Cancel is clicked: reply=$(2>/dev/null /usr/bin/osascript <<-AppleSkript tell application "System Events" set frontApp to (name of (some process whose frontmost is true)) as text end tell tell application frontApp display dialog "$message" with icon note end tell AppleSkript ) ASerror=$? # capture exit status!

# dialog is dismissed, so what happened? clicked=$(parse "$reply" 'returned')

# seems best to test for cancel click via exit code FIRST: if [ $ASerror -ne 0 ] then echo 'use hit cancel' # do whatever

else echo "user clicked on: $clicked" # do whatever fi # I used the dialog's status next just for test purposes... exit $ASerror # <-- put whatever exit code you like instead

-Tom

![external image link](attachments/642f8776fcc146ffa744c47b50401565)

Not applicable

If you're encountering errors while trying to run it as part of a policy,
you might need specify the execution context using ``launchctl bsexec''.
That would make it look something like this:

###########

finder_pid=$(ps -xawwo pid,command | grep
"/System/Library/CoreServices/Finder.app/Contents/MacOS/Finder$" | awk
'{print $1}')

selected_hostname=$( /bin/launchctl bsexec $finder_pid /usr/bin/osascript
<<-AS_EOF

tell application "System Events" set frontApp to (name of (some process whose frontmost is true)) as
text
end tell

tell application frontApp set HostnameInput to text returned of (display dialog "What is the
hostname of this computer?" default answer "hostname" with icon note)
end tell

AS_EOF

)

scutil -set HostName "${selected_hostname}"

###########

Brandt

J. Brandt Buckley
Systems Integration Engineer
CRBS

brandt at ncmir.ucsd.edu http://crbs.ucsd.edu
P: +1 858 822 0743 F: +1 858 524 7497

Center for Research in Biological Systems
University of California, San Diego

Holly & Calit2
9500 Gilman Drive
Basic Science Building #1000
La Jolla, CA 92093-0608

![external image link](attachments/d63164d86e6046f6afd608a1510cf517)