【问题标题】:AppleScript : tell running application from set of candidate namesAppleScript:从候选名称集中告诉正在运行的应用程序
【发布时间】:2017-07-07 18:50:25
【问题描述】:
我们发布了多个不同版本的应用。它们具有相似的名称 - Foo Basic、Foo Deluxe、Foo Retro 等。类似但不同的包标识符也是。 (这不是我的主意!)有些用户安装了多个这些应用程序,但只能运行一个。
所有应用程序都支持相同的 AppleScript 字典。我需要一个 AppleScript 来编写我们应用程序当前运行版本的脚本来执行操作。我该怎么做?
【问题讨论】:
标签:
process
applescript
running-other-programs
【解决方案1】:
我让它工作了。它需要几个部分:
- 获取正在运行的应用程序的名称。您可以使用
processes 或 System Events 或 do shell script "ps …" 来执行此操作,无论您认为哪种情况更可靠。
- 然后,使用您的 Mac 上的一个应用程序中的术语,您可以
-
*tell application appName...,前提是您有
- 将您的脚本保存为应用程序,因此它已经被编译。
这是一些代码。我正在编写的脚本还没有使用系统事件,所以为了让新用户免于访问系统偏好设置的痛苦,我选择使用 /bin/ps 来代替......
set appName to runningAppWithBaseName("Foo")
using terms from application "Foo Deluxe" -- an app on your Mac
tell application appName
(* whatever code you want here *)
end tell
end using terms from
on runningAppWithBaseName(baseName)
set command to "/bin/ps -eo args | grep " & baseName & " | grep -v grep"
(* The "grep -v grep" is to exclude the grep process itself from the results. *)
try
set fullPathOfRunningApp to do shell script command
end try
(* Here, given the fullPathOfRunningApp and your list of candidate app names, *)
(* insert code to determine the name of the running app. *)
return nameOfRunningApp
end runningAppWithBaseName