将变量 FROM applescript 传递给 bash 脚本

Passing an variable FROM applescript to a bash script

所以我在更大的 bash 脚本中嵌入了一个 AppleScript,我希望打开对话框供用户提问,接受这些变量,然后将它们传递给 bash 脚本运行 进一步的命令。但是,它似乎并没有将变量传递到我的 bash 脚本中,而是处于一个恒定的循环中。由于我将使用的交付方式,我需要这样做。我知道我可以用读取命令完成相同的结果。

我已经尝试查找这个问题并找到了很多答案,但是要么是 none 的工作,要么是他们正在使用比我需要的更大的环境。这个 link https://discussions.apple.com/thread/2512634 是我发现的最接近我正在做的事情,我几乎一字不漏,但它仍然没有通过它们并且处于不断循环中

#! /bin/sh #Asset.sh
NUM=""  # << 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;

你可能会发现,如果我将我的笔记本电脑命名为 CAD##,我希望将变量从 AppleScript 传递到更大的 bash 脚本中,而不是让 AppleScript 陷入循环.

基本上,我正在尝试将我 运行 的所有脚本捆绑到我们的 "floater/lender" 笔记本电脑和用户笔记本电脑上,并通过我们使用的第 3 方分销商部署该脚本,即 JAMF,它具有名为自助服务的应用程序。我希望能够 运行 这个脚本与那个和这个主脚本基本上重命名,添加远程桌面字段,制作我们使用的某些用户目录,等等,等等......正如我提到的使用 Applescript 是bash 对我来说是全新的。我更愿意将它捆绑在一个 "Master" 脚本中。我真正遇到的唯一问题是 applescript

编辑添加脚本:

#!/bin/sh
SIPs=$(csrutil status | awk -F ": " '{print }')
lastUser=$(defaults read /Library/Preferences/com.apple.loginwindow lastUserName)
ecSIGN=$(/Applications/Enterprise\ Connect.app/Contents/SharedSupport/eccl -p signedInStatus | awk -F ": " '{print }')
ecNAME=$(/Applications/Enterprise\ Connect.app/Contents/SharedSupport/eccl -a name | awk -F ": " '{print }')
ecDEPT=$(/Applications/Enterprise\ Connect.app/Contents/SharedSupport/eccl -a department | awk -F ": " '{print }')
ecTITLE=$(/Applications/Enterprise\ Connect.app/Contents/SharedSupport/eccl -a title | awk -F ": " '{print }')
L3_cache=$(system_profiler SPHardwareDataType | grep L3 | sed -e 's/^[ \t]*//')
Model=$(system_profiler SPHardwareDataType | grep "Model Name" | awk -F ": " '{print }' | sed -e 's/^[ \t]*//')
SSD_check=$(diskutil info disk0 | grep "Solid State" | awk -F ": " '{print }' | sed -e 's/^[ \t]*//')
PUBLIC_MOUNT=$(mount | awk ' == "/Volumes/Public" {print }')
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

最终结果!!

刷之前说的:完全重置

查看您在其中编辑的完整脚本,我将建议完全取消递归 do shell script 的重写。真的,变化不大。我们不会将 'floater number' 作为参数传递,而是将其作为结果传回。为此:

  1. 删除第 12 行(NUM="" 行)
  2. 像这样重写整个 AppleScript 部分:
    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')

(请注意,我冒昧地简化了您的代码并将两个 display dialog 命令合并为一个)。浮动数字现在应该在 $NUM 中,您可以继续编写主脚本。