Tanuj KS
Tanuj KS

Reputation: 165

Defining osascript path with a variable

So I am trying to add a login item from the terminal and I am using this line:

osascript -e 'tell application "System Events" to make login item at end with properties {path:/Users/me/Desktop/main, hidden:true}'

But the thing is I want the main file to add itself to the login items meaning that the path of main may change so I want the path to be a variable like this:

cd "$(dirname "$0")"
STR="$(dirname "$0")"
osascript -e 'tell application "System Events" to make login item at end with properties {path:$STR, hidden:true}'
sudo python3 main.py

But this gives me a bunch of syntax errors, is there another way to use a variable for the path?

Thanks

Upvotes: 1

Views: 161

Answers (1)

glenn jackman
glenn jackman

Reputation: 246754

Because the applescript body is in single quotes, the variable cannot expand.
Also, because there are double quotes in the body, quoting becomes difficult.

This will work:

printf -v body 'tell application "System Events" to make login item at end with properties {path:%s, hidden:true}' "$STR"
# ...............................................................................................^^................^^^^^^
osascript -e "$body"

Upvotes: 1

Related Questions