简体   繁体   English

使用变量定义 osascript 路径

[英]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:但问题是我希望main文件将自己添加到登录项中,这意味着main的路径可能会改变,所以我希望路径是这样的变量:

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谢谢

Because the applescript body is in single quotes, the variable cannot expand.因为applescript 正文是单引号,所以变量不能扩展。
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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM