csiket715
csiket715

Reputation: 51

Passing an variable FROM applescript to a bash script

So I am embedding an AppleScript inside a bigger bash script, I want the dialog to open for the user ask them questions, accept those variables and then pass them to the bash script to run further commands. However, it doesn't seem to be passing the variables along into my bash script and is in a constant loop. I need to do it this way because of the delivery method I will be using. I know I could accomplish the same result with a read command.

I have tried looking up this issue and found a lot of answers but either none of the work or they are working with an environment larger then what I need. This link https://discussions.apple.com/thread/2512634 is the closest thing I found to what I am doing, and I almost have it verbatim, but still, it just doesn't pass them and is in a constant loop

#! /bin/sh #Asset.sh
NUM="$1"  # << this is supposed to be varible FROM applescript

if [[ "$Model" = "MacBook Pro" ]]; then #do not mind my varibles here
  osascript -e 'tell me to activate
  display dialog "We need to ask you a few questions"
  property FloatNumber : ""
  display dialog "Please enter a floater number:" default answer FloatNumber
  set the FloatNumber to text returned of the result
  do shell script ("/Users/administrator/Downloads/Asset.sh " & FloatNumber)' 
      if [[ "$SSD_check" = "Yes" ]]; then #do not mind my varibles here
        scutil --set HostName CAD$NUM;
        scutil --set LocalHostName CAD$NUM;
        scutil --set ComputerName CAD$NUM;
        dscacheutil -flushcache;

As you can probably make out, if I am naming my laptops CAD## I would like the variable to be passed from the AppleScript into the larger bash script and not have the AppleScript stuck in a loop.

Basically, I am trying to bundle all the scripts I run on our "floater/lender" laptop AND user laptops and deploy that script through a 3rd party distributor that we use which is JAMF which has an app called Self Service. I want to be able to run this script with that and this Master script to basically rename, add remote desktop fields, make certain user directories we use, etc, etc, etc.... As I mentioned using Applescript is completely new to me where bash isn't. I would prefer to have it just bundled in one "Master" script. Really the only issue I am having is that applescript

Edited adding script:

#!/bin/sh
SIPs=$(csrutil status | awk -F ": " '{print $2}')
lastUser=$(defaults read /Library/Preferences/com.apple.loginwindow lastUserName)
ecSIGN=$(/Applications/Enterprise\ Connect.app/Contents/SharedSupport/eccl -p signedInStatus | awk -F ": " '{print $2}')
ecNAME=$(/Applications/Enterprise\ Connect.app/Contents/SharedSupport/eccl -a name | awk -F ": " '{print $2}')
ecDEPT=$(/Applications/Enterprise\ Connect.app/Contents/SharedSupport/eccl -a department | awk -F ": " '{print $2}')
ecTITLE=$(/Applications/Enterprise\ Connect.app/Contents/SharedSupport/eccl -a title | awk -F ": " '{print $2}')
L3_cache=$(system_profiler SPHardwareDataType | grep L3 | sed -e 's/^[ \t]*//')
Model=$(system_profiler SPHardwareDataType | grep "Model Name" | awk -F ": " '{print $2}' | sed -e 's/^[ \t]*//')
SSD_check=$(diskutil info disk0 | grep "Solid State" | awk -F ": " '{print $2}' | sed -e 's/^[ \t]*//')
PUBLIC_MOUNT=$(mount | awk '$3 == "/Volumes/Public" {print $3}')
DIALOG=$(osascript -e 'display dialog "Please enter a floater number :" default answer "" with title "Just a Few Questions"  
    set the FloatNumber to text returned of the result
    return FloatNumber')
NUM=$(echo $DIALOG)

echo "Checking to see if csrutil is enabled";
if [[ "$SIPs" = "enabled." ]]; then
    echo "csrutil is enabled. Please turn it off";
    exit 1
  else {
    echo "About to run some scripts...";
    echo "Setting ARDFields and setting computer name and assigning it in JSS";
    if [[ "$lastUser" = "administrator" ]] && [[ "$Model" = "MacBook Pro" ]]; then
        $DIALOG
          if [[ "$SSD_check" = "Yes" ]]; then
            scutil --set HostName CAD$NUM;
            scutil --set LocalHostName CAD$NUM;
            scutil --set ComputerName CAD$NUM;
            dscacheutil -flushcache;
          else {
            scutil --set HostName Floater$NUM;
            scutil --set LocalHostName Floater$NUM;
            scutil --set ComputerName Floater$NUM;
            dscacheutil -flushcache;
          }
          fi
        else {
          scutil --set HostName NewUser$NUM;
          scutil --set LocalHostName NewUser$NUM;
          scutil --set ComputerName NewUser$NUM;
          dscacheutil -flushcache;
        }
      fi
    }
    fi

end result!!

Upvotes: 0

Views: 1084

Answers (1)

Ted Wrigley
Ted Wrigley

Reputation: 3174

Scratching what I said before: full reset.

Looking at the full script that you edited in, I'm going to suggest a rewrite that does away with the recursive do shell script entirely. It's not much of a change, really; rather than pass the 'floater number' on as an argument, we're going to pass it back as a result. To do that:

  1. Delete line 12 (the NUM="$1" line)
  2. Rewrite the whole AppleScript section like so:
    NUM=$(osascript -e 'display dialog "Please enter a floater number :" default answer "" with title "Just a Few Questions"  
    set the FloatNumber to text returned of the result
    return FloatNumber')

(Note that I've taken the liberty of simplifying your code and wrapping the two display dialog commands into one). The floater number should now be in $NUM, and you can proceed with the main script as written.

Upvotes: 1

Related Questions