Pass in variable from shell script to applescript

前端 未结 2 1755
独厮守ぢ
独厮守ぢ 2021-01-11 17:26

I\'ve got a shell script that I call that uses osascript, and that osascript calls a shell script and passes in a variable that I\'ve set in the or

相关标签:
2条回答
  • 2021-01-11 17:31

    You can also use a run handler or export:

    osascript -e 'on run argv
        item 1 of argv
    end run' aa
    
    osascript -e 'on run argv
        item 1 of argv
    end run' -- -aa
    
    osascript - -aa <<'END' 2> /dev/null
    on run {a}
        a
    end run
    END
    
    export v=1
    osascript -e 'system attribute "v"'
    

    I don't know any way to get STDIN. on run {input, arguments} only works in Automator.

    0 讨论(0)
  • 2021-01-11 17:40

    Shell variables doesn't expanding inside single quotes. When you to want pass a shell variable to osascript you need to use double "" quotes. The problem is, than you must escape double quotes needed inside the osascript, like:

    the script

    say "Hello" using "Alex"
    

    you need escape quotes

    text="Hello"
    osascript -e "say \"$text\" using \"Alex\""
    

    This not very readable, therefore it much better to use the bash's heredoc feature, like

    text="Hello world"
    osascript <<EOF
    say "$text" using "Alex"
    EOF
    

    And you can write multiline script inside for a free, it is much better than using multiple -e args...

    0 讨论(0)
提交回复
热议问题