Script Error

Indra
New Contributor

Hello Friends,

 

I have a script which will run on mac and pop up will appear to enter your host name.

 

Issue- I-  am getting this error (Script result: 37:45: execution error: System Events got an error: Application isn’t running. (-600)
Rename Successful), how to solve this? Actually pop up is not appearing but if i use, sudo jamf policy then i am getting pop and able to rename

 

Issue -2 -  how can i add one logo on pop up?

 

Script

#!/bin/bash
#Freddie Cox for Knox County Schools
#Edited by Justin Ellis
#2012

ComputerName=`/usr/bin/osascript <<EOT
tell application "System Events"
activate
set ComputerName to text returned of (display dialog "Please Input New Computer Name as LT with asset number of the laptop EX-LT1234, Asset number can be found on bottom of your laptop" default answer "" with icon 2)
end tell
EOT`

#Set New Computer Name
echo $ComputerName
scutil --set HostName $ComputerName
scutil --set LocalHostName $ComputerName
scutil --set ComputerName $ComputerName

echo Rename Successful

4 REPLIES 4

roiegat
Contributor III

Would recommend looking at SwiftDialouge....lots of good resources.  For your need check out:

https://snelson.us/2022/12/swiftdialog-izing-your-scripts/

mm2270
Legendary Contributor III

@Indra Your problem is most likely that you're calling Applescript from a root context and prompting the logged in user for input. Sometimes, when Applescript dialogs are used just to display information, it works fine this way. But most often, if your dialog is asking the user to enter some information, then you have to run the dialog portion of the script as the user, not as root.

You might be saying to yourself, "but mm2270, why does it work if I run the script using sudo jamf policy?" Because there's a difference between you running a sudo/root command versus the OS running something as root using a LaunchDaemon. The latter is how the jamf recurring check-in works, and the context is different. When something is run with the natural check-in trigger, you'll need to ensure the dialog command runs as the logged in user. This mostly applies to Applescript, but it can also apply to some other tools.

You can check this article from Armin Briegel for information on how to do this in your script. This is considered the standard on how to do this:

Running a Command as another User – Scripting OS X

 

Outside of the above, one small point on your script. While the way you're capturing the input into a variable is technically ok, it's not considered the best practice these days. I'm specifically talking about using the backtick mark around the command here:

ComputerName=`/usr/bin/osascript <<EOT

The better way these days is to use the $() syntax, so this instead:

ComputerName=$(/usr/bin/osascript <<EOT

and then at the end of the command use

EOT)

I'm not saying this was the cause of the issue, but just wanted to point it out for future scripting reference.

Lastly, you may want to consider using another 3rd party tool for getting user input and even for displaying information. swiftDialog mentioned above by @roiegat is a great tool, but there is also IBM Notifier here that is a very robust dialoging tool. Both of these tools operate differently and can be used for different purposes, so it might be a good idea to check them both out to see which one you like, or maybe use both like I do.

mm2270
Legendary Contributor III

Oh, forgot you had a question about getting an icon into the dialog. As long as the path to the icon is known and consistent, this isn't too hard. Here's the trick when using Applescript (or at least one way to do it)

tell application "System Events"
    activate
    set icon_path to POSIX file "/full/path/to/icon.png"
    display dialog "Hello" with icon icon_path
end tell

 So in your case, right after you activate System Events, put in an applescript variable that defines the POSIX path to the file (icon) as shown. Then in your line that displays the dialog, use the icon variable you created instead of icon 2 or whatever. It should work, again, as long as the icon is present in that location.

Good luck.

teodle
Contributor II

I always ran into trouble with Applescripts that use Tell blocks.