将变量从applescript传递到bash脚本

时间:2019-08-12 19:46:25

标签: bash applescript

因此,我将AppleScript嵌入到更大的bash脚本中,希望打开对话框供用户询问问题,接受这些变量,然后将其传递给bash脚本以运行其他命令。但是,它似乎并没有将变量传递到我的bash脚本中,而是处于恒定循环中。由于要使用的投放方式,我需要这样做。我知道我可以使用读取命令来完成相同的结果。

我尝试查找此问题并找到了很多答案,但是要么没有工作,要么他们正在比我需要的环境更大的环境下工作。这个链接https://discussions.apple.com/thread/2512634是我发现的最接近我所做的事情,并且我几乎逐字记录,但是,它只是不传递它们而是处于恒定循环中

#! /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;

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

基本上,我试图捆绑在“便携式/便携式”笔记本电脑和用户笔记本电脑上运行的所有脚本,并通过我们使用的第三方分销商JAMF部署该脚本,JAMF拥有一个名为“自助服务”的应用程序。我希望能够以此运行该脚本,并且此Master脚本基本上可以重命名,添加远程桌面字段,创建我们使用的某些用户目录等,等等。。。正如我所提到的,使用Applescript对我来说是全新的没有bash的地方。我希望将其仅捆绑在一个“主”脚本中。我真正遇到的唯一问题是applescript

编辑添加脚本:

#!/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

最终结果!

1 个答案:

答案 0 :(得分:1)

抓取我之前说的内容:完全重置。

查看您在其中编辑的完整脚本,我建议您进行一次完全消除递归do shell script的重写。确实,这没有多大变化。而不是将“浮点数”作为参数传递,因此,我们将其传递回去。为此:

  1. 删除第12行(NUM="$1"行)
  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中,您可以按照编写的顺序继续执行主脚本。