Chris Reynolds
Chris Reynolds

Reputation: 13

Increase timeout when prompting using osascript from bash script

I'm working on some bash scripting for use in JAMF in my environment. My scripting skills include: googling other peoples scripts, clipping out pieces of those and pasting them into my own script. Then eating the paste. I'm having a bit of a bizarre issue with a piece of AppleScript that runs within a bash script. Original script follows:

#!/bin/sh

# Remove pre-existing settings

rm -rfv /var/db/scrubbed/hostName
rm -rfv /var/db/scrubbed/imageTech
rm -rfv /var/db/scrubbed/adBinding

# Prompt for Hostname of new computer
hostName="$(/usr/bin/osascript -e 'Tell application "System Events" to display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1' -e 'text returned of result')"
/bin/echo "Computer hostname set to $hostName"

This code listed works just fine. Pops up my dialog box as expected. Unfortunately the box times out after 60 seconds. This is part of an imaging process so if the tech walks away for a few minutes the script continues and the hostname does not get set. Doing some research I found the with timeout of X command in AppleScript. When I update the first block of code to this:

# Prompt for Hostname of new computer
hostName="$(/usr/bin/osascript -e 'Tell application "System Events" with timeout of 86400 to display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1' -e 'text returned of result')"
/bin/echo "Computer hostname set to $hostName"

If it helps, this runs in the actual apple script app

tell application "System Events"
    with timeout of 86400 seconds
        display dialog "Please enter the hostname of the new computer" default answer "" with title "Hostname" buttons {"Ok"} default button 1
    end timeout
end tell

I'm a scrub! Send help!

Upvotes: 1

Views: 1762

Answers (2)

wch1zpink
wch1zpink

Reputation: 3142

If all you were looking to achieve was getting the actual host name of the computer… this one line of AppleScript code will achieve that for you.

set hostName to host name of (system info)

Otherwise

First, the display dialog command is not handled by System Events. The display dialog command is a Standard Additions command.

Another thing to point out is while using the with timeout clause, the script will continue running after the specified amount of time which was set in the clause. This puts you right back to the initial problem of the script continuing, after the time out, without user input.

The display dialog command has a giving up after option which allows you to set the amount of seconds it will wait until the dialogue will close itself and continue on. You can then add an if… then… clause to stop the script if the display dialog gave up after the specified amount of time.

Here is an example of an AppleScript which may be more suitable to your needs. I added a “Cancel” button giving the user the option to stop the script. I also used 10 seconds for testing purposes rather than 86400…. Which can easily be edited

set enterHostName to display dialog ¬
    "Please enter the hostname of the new computer" default answer "" with title ¬
    "Hostname" buttons {"Cancel", "Ok"} default button 2 giving up after 10

if gave up of enterHostName then
    return
else
    set hostName to text returned of enterHostName
end if

Again, the code was only an example. You will have to tweak it to work correctly within Terminal. Or you can save the code as an AppleScript file and use the osascript command in terminal to run the AppleScript file.

Upvotes: 1

Barmar
Barmar

Reputation: 780724

You need line breaks in the script, just as in the regular AppleScript. You're also missing end timeout, end tell, and the seconds units.

hostName="$(/usr/bin/osascript -e 'tell application "System Events" 
    with timeout of 86400 seconds
        display dialog "Please enter the Hostname of the new computer:" default answer "" with title "Hostname" with text buttons {"Ok"} default button 1
    end timeout
end tell' -e 'text returned of result')"

Upvotes: 1

Related Questions