How to pass a variable from a shell script to applescript

I have a shell script that checks if a folder has a given number of files and displays error messages to the user. I am using applescript to display an error message. Is there a way to pass script shell variables to AppleScript?

Here is what I have:

declare -i count=5; osascript -e 'tell app "Xcode" to display dialog "Counted $count items." buttons {"Continue"}' 

I want the result to be Counted 5 items. but it just appears as Counted $count items. How to pass script shell variable in applescript?

+4
source share
2 answers

There are three solutions:

  • Use "" instead of '' to expand $ var with the shell

  • Close the quote, put var and close the spaces again (for example, "string" $ var'rest of string '). However, this is unsafe if the variable may contain spaces.

  • Use formal command line arguments with your AppleScript script

+5
source

Just to clarify, this is what I did at the end to make it work.

 declare -i count=5; osascript -e 'tell app "Xcode" to display dialog "Counted '$count' items." buttons {"Continue"}' 
+2
source

Source: https://habr.com/ru/post/1441282/


All Articles