2012-04-05 114 views
14

2部分的问题:使用AppleScript打开的程序

我只是试图运行使用AppleScript从终端程序,所以我尝试:

$ osascript tell application "iTunes" to activate 

并且得到错误:

osascript: tell: No such file or directory 

给程序的完整路径也不起作用。我错过了什么?问题的第二部分是我最终希望使用applescript的。我想用它来打开我使用py2app构建的应用程序。可以applescript打开任何mac应用程序或只是某些已经兼容的应用程序。

谢谢

回答

19

试试这个。注意你在编写命令时使用“-e”。如果没有“-e”,你会给一个applescript运行的路径。另请注意,字符串命令必须用引号引起来。

osascript -e "tell application \"iTunes\" to activate" 

如果你有一个多行的AppleScript你喜欢这个每行前使用“-e” ......

osascript -e "tell application \"iTunes\"" -e "activate" -e "end tell" 

如果你想打开一个应用程序只使用Unix的“开放“命令

open "/path/to/application" 

如果你想使用AppleScript打开一个应用程序,并在‘激活’命令不起作用(它应该几乎所有的工作,虽然)然后告诉查找程序将其打开。请记住,AppleScript的采用逗号分隔的路径...

osascript -e "tell application \"Finder\" to open file \"path:to:application\"" 
+0

是否可以告诉被打开,其工作区吗? – 2015-06-10 09:35:24

5

尝试:

do shell script "open /Applications/iTunes.app" 
4

你需要把单引号括起来的诉说:

osascript -e“告诉应用 “iTunes” 的到激活”

当您运行-e

+0

你能指出一些文件在哪里解释?通过osascript的手册页看,我没有看到这提到。 – regulus6633 2015-06-28 08:00:34

7

在bash的sh否则你定义一个变量(例如在终端中),您可以通过使用“here document”发送多行到osascript。

osascript -e "tell application \"iTunes\"" -e "activate" -e "end tell" 

成为

osascript <<EOF 
tell application "iTunes" 
    activate 
end tell 
EOF 

作为一个老斯库尔的Unix黑客,我在$ HOME/bin目录中保存这些小片段,并在命令行中调用它们。尽管如此,仍然在学习细节。

Alan

1

我也是新手脚本。

我困惑,所以我扫描命名为AppleScript Language Guide 的文章,当我通过脚本命令的项目,我了解到,如果你想与applescript editor以激活mac os的应用程序,你应该在你的编辑器中键入下面的代码,然后编译并运行它们!可以这样回答会帮助你,这里是代码:

// applescript editor code  

----------  

activate application "iTunes" line 1  

----------  

tell application "iTunes" to activate line 2 
2

替代osascript:

open -a Calendar 

附近:

pkill Calendar 
+0

-a是什么?你能提供一个解释吗? – user1271772 2017-05-23 21:45:40

+0

@ user1271772,阅读'open' _command_的手册页,它会告诉你'-a'的用途! – user3439894 2018-02-11 03:21:46

+0

@ user3439894:这是干什么用的? – user1271772 2018-02-11 23:28:59

相关问题