1

I am currently learning how to use bash shell scripting with AppleScript. I have a problem. The problem is how to pass a bash variable into an osascript The example is as following:

read input
foo $input

foo(){
    osascript path/to/script.scpt $1
}

my question is how to convert input into something that would be accepted by the script.scpt since $1 would not be recognized by the script.scpt . Thanks

1 Answer 1

1

Actually that should work since bash will automatically set the value of $1 to be the argument you passed to foo. So in this scenario, it would be the input that you read.

I cleaned up you bash script a little..

#!/bin/bash

foo(){
  echo "Calling Apple script with argument $1"
  osascript script.scpt "$1"
}

read input
foo "$input"

And with this AppleScript,

on run argv
  return "hello, " & item 1 of argv & "."
end run

I get this output..

> ./test.sh
crazy world
Calling Apple script with argument world
hello, crazy world.
Sign up to request clarification or add additional context in comments.

4 Comments

what if the value of $1 is a string with spaces? I used the run handler to split the string but it does not work. For example, the value of $1 is "About This Mac,Apple" . I changed the delimiter from space to "," so that it should be parsed to "About This Mac" and "Apple", which are the two items of argv that I need. But it does not seem to work
Ah yes. That can be solved with a few quotes.. I will update the answer above.
This is a great read to avoid common gotcas in bash mywiki.wooledge.org/BashPitfalls
Thanks so much !

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.