0                    
               

所以我在一个更大的 bash 脚本中嵌入了一个 AppleScript,我希望打开对话框供用户提问,接受这些变量,然后将它们传递给 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 陷入循环。                    

基本上,我试图捆绑我在我们的“浮动/贷方”笔记本电脑和用户笔记本电脑上运行的所有脚本,并通过我们使用的第 3 方分销商部署该脚本,这是 JAMF,它有一个名为 Self Service 的应用程序。我希望能够运行这个脚本和这个主脚本来基本上重命名、添加远程桌面字段、创建我们使用的某些用户目录等等……正如我提到的,使用 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 个回答                      

正确答案  有大用 有大大用                    


                   

       
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')

 自己亲自做的包含取消的功能    有大用 有大大用 有大大大用

#!/bin/bash
#

Result=$(/usr/bin/osascript -e 'display dialog "请输入关键字:" default answer "" with title "aaaa搜索SOP"  
    set result1 to result
    set the textValue to text returned of the result
    set the button_data to button returned of the result1
    if button_data = "Cancel" then
      return ""
    end if
    return textValue')
if [ -z "$Result" ]; then
    exit 0
fi

Result=$(echo "${Result}" | sed -e 's/^[[:space:]]*//' -e 's/[[:space:]]*$//')

                      


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

                                       
编辑                                        
  • 抱歉造成混淆,这是我第一次尝试这样的事情。实际上 Asset.sh 在技术上是嵌入了 applescript 的 bash 脚本 
    – csiket715                                        
     2019 年 8 月 12 日 20:29                                    
  • @csiket715 - 难怪你得到一个无限循环:调用脚本调用脚本调用脚本......如果你能更好地了解你想要完成的事情,我们可以重组脚本为了避免这种递归。 
    – 泰德箭牌                                        
     2019 年 8 月 12 日 21:03                                    
  • 老实说,你提到它太有趣了,之后,我把它扔在那里我在想自己等等我只是一次又一次地调用我的脚本,但在我的辩护中,我认为这是传递这些变量的一种方式。我在问题中添加了更完整的脚本 
    – csiket715                                        
     2019 年 8 月 13 日 1:20                                    
  • @csiket715 好的,我重写了我的答案以稍微重建您的脚本。看看这是如何工作的。 
    – 泰德箭牌                                        
     2019 年 8 月 13 日 6:46                                    
  • 做到了,我想我正在寻找的是“return FloatNumber”。但是,我确实将它添加到一个不同的变量中,并让结果与结果相呼应,这样它就不会问我每一个问题。非常感谢 
    – csiket715                                        
     2019 年 8 月 13 日 12:29                                    
       

您的答案            

来自 https://stackoverflow.com/questions/57467380/passing-an-variable-from-applescript-to-a-bash-script            


           


           

获取从 osascript 返回到 shell 脚本变量的按钮。                                                                
                                                           

摩根森                                                    
 
                                                   
新贡献者 II                                                    

我正在尝试根据用户是否单击“确定”或“取消”来设置一个变量,我想将该变量转发到一个 shell 脚本,这样我就知道我是否可以继续执行脚本。有谁知道如何做到这一点?                                                        

                                                   
1 接受的解决方案                
                               
                                   

                                                               

德内沙                                                        
 
                                                       
重要贡献者 II                                                        

尝试使用“返回的按钮”——您将获得按钮的实际文本,即“确定”或“退出”。您需要指定按钮,而不是使用会引发错误的“取消”。所以修改这篇文章中的第一个代码示例:                                                            

myVariable=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set myAnswer to button returned of (display dialog "Press Ok or Quit" buttons {"Ok", "Quit"})
end tell
return myAnswer
END)
                                                           

在原帖中查看解决方案                                                                

7 回复            

           

                                   
                                       

                                                                   

酿酒师                                                            
 
                                                           
重要贡献者 II                                                            

将您的代码存储osascript为变量。                                                                

例子:                                                                

computerName=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set the answer to text returned of (display dialog "Enter new computer name" with title "Computer Name Change" default answer "$(scutil --get ComputerName)")
end tell
END)
                                                           
                                   
                                       

                                                                   

摩根森                                                            
 
                                                           
新贡献者 II                                                            

所以要求用户输入一些东西。我怎样才能得到他们点击的按钮?                                                                

                                   
                                       

                                                                   

酿酒师                                                            
 
                                                           
重要贡献者 II                                                            

知道了。尝试在您的命令后检查退出代码osascript在我的简短测试中,我在单击第一个按钮后获得退出代码 0,在单击第二个按钮后获得退出代码 1。                                                                

新示例(以当前登录用户身份运行 osascript):                                                                

#!/bin/bash

# Get currently logged in user
loggedInUser=$( scutil <<< "show State:/Users/ConsoleUser" | awk '/Name :/ && ! /loginwindow/ { print $3 }' )

# Get the logged in UID
loggedInUID=$(id -u $loggedInUser)

/bin/launchctl asuser "${loggedInUID}" sudo -iu "${loggedInUser}" /usr/bin/osascript<<-EOF
tell application "System Events"
activate
display dialog "Computer name set to " & host name of (system info) buttons {"OK", "Cancel"} default button 1 with title "Computer name"
end tell
EOF

echo $?
                                                           
                                   
                                       

                                                                   

摩根森                                                            
 
                                                           
新贡献者 II                                                            

所以只是想在这里测试就是我得到的。 d7dc25e32b0a43d395407537a1504907                                                                

                                   
                                       

                                                                   

德内沙                                                            
 
                                                           
重要贡献者 II                                                            

尝试使用“返回的按钮”——您将获得按钮的实际文本,即“确定”或“退出”。您需要指定按钮,而不是使用会引发错误的“取消”。所以修改这篇文章中的第一个代码示例:                                                                

myVariable=$(/usr/bin/osascript<<END
tell application "System Events"
activate
set myAnswer to button returned of (display dialog "Press Ok or Quit" buttons {"Ok", "Quit"})
end tell
return myAnswer
END)
                                                           
                                   
                                       

                                                                   

用户-DnpfQOvJBx                                                            
 
                                                           
新贡献者                                                            

我需要完成同样的事情,但是从内部执行 shell 脚本如果你用 AppleScript 编写,我无法找到如何转义所有引号和特殊字符,并且注册只是为了寻求帮助。有问题的代码是                                                                

do shell script "_status=$( csrutil status | grep -Eio '[a-z\.]+$' ) ;  action_alert=$(if [[ $_status ==  disabled?   ]] ; then echo "System Integrity Protection: disabled" ; fi) ; if [[ $action_alert != "" ]] ; then echo "' display alert \"$action_alert\"  as warning message \"Restart to Recovery\" & return & return & \"While in Recovery start Terminal and run\" & return & \"\\\"csrutil enable ; nvram -d recovery-boot-mode ; reboot\\\"\" buttons {\"Abort\", \"Proceed\"}  ' " ; fi | xargs -I % osascript -e %"
                                                               

只要按钮参数后面的大括号中只有 1 个字符串项,代码就可以工作,但是当我添加另一个用逗号分隔的列表项时,我会收到错误消息。我推断它与大括号扩展有关:在一份 Bash 指南中,我读到在大括号前加上美元符号或转义它们会阻止扩展,但这也没有奏效。那我做错了什么?                                                                


                                                               

@user-DnpfQOvJBx 您是否尝试从 AppleScript 或应用程序运行它?                                                                


                                                               

来自  https://community.jamf.com/t5/jamf-pro/get-the-button-returned-from-an-osascript-to-a-variable-for-a/m-p/167483