4

I have a shell script that checks if folders have a set number of files and displays error messages to the user. I use applescript to display the error message. Is there a way to pass the variables of the shell script to the 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 output to be Counted 5 items. but it just comes out as Counted $count items. How can I pass a shell script variable to the applescript?

2 Answers 2

5

There are three solutions:

  1. Use "" instead of '' so that the $var will be expanded by the shell

  2. Close the quote, put the var, and reopen with no spaces (e.g. 'string'$var'rest of string'). However, this is unsafe if the variable can contain spaces.

  3. Use formal command line arguments with your AppleScript script

Sign up to request clarification or add additional context in comments.

2 Comments

Cheers. I used the second method because I don't have spaces and it was the easiest to implement.
If var contains spaces, I found this to work: osascript -e 'display dialog "string1'"$var"'string2"'
2

Just to clarify, this is what I did in the end to get it to work.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.