- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
谁能想出解决 osascript 按名称索引瓶颈的方法,因为它引用了同一应用程序的多个实例?
如果我们获得两个进程 ID——同一个应用程序的两个不同实例中的每一个,osascript 返回同一个实例以换取任何一个 pid——就好像它首先将 pid 映射到一个应用程序名称,然后检索第一个具有该名称的应用程序。
例如,启动两个不同的 VLC.app 实例,播放两个不同的视频文件,如下所示:
open -na /Applications/VLC.app ~/fileA.m4v
open -na /Applications/VLC.app ~/fileB.m4v
然后获取两个单独的应用程序进程 ID,例如:
echo "$(ps -ceo pid=,comm= | awk '/VLC/ { print $1}')"
然后我们可以使用 Applescript 或 Yosemite JXA Javascript 从任一 pid 获取对应用程序对象的引用。
然而,事实证明,无论我们提供哪个进程 ID,我们总是返回对同一实例的引用,运行相同的视频文件,就好像 osascript 只是将 pid 转换为应用程序名称,然后总是返回匹配该名称的第一个进程。
用于应用程序的 Yosemite Javascript:
function run() {
var app = Application.currentApplication();
app.includeStandardAdditions = true;
var lstVLC = app.doShellScript(
"echo \"$(ps -ceo pid=,comm= | awk '/VLC/ { print $1}')\""
).split(/[\r\n]/).map(Number).map(Application);
return {
firstInstance: lstVLC[0].windows[0].name(),
secondInstance: lstVLC[1].windows[0].name()
};
}
苹果脚本:
on run {}
set strCMD to "echo \"$(ps -ceo pid=,comm= | awk '/VLC/ { print $1}')\""
set lstNum to paragraphs of (do shell script strCMD)
repeat with i from 1 to length of lstNum
set item i of lstNum to (item i of lstNum) as number
end repeat
tell application "System Events"
set oProcA to first application process where unix id = (item 1 of lstNum)
set oProcB to first application process where unix id = (item 2 of lstNum)
end tell
return [name of first window of oProcA, name of first window of oProcB]
end run
对分别为每个实例编写脚本的途径有什么想法吗?
最佳答案
对于每个实例,从与特定进程相同的行中询问窗口的名称,如下所示:
set windowNames to {}
set lstNum to paragraphs of (do shell script "ps -ceo pid=,comm= | awk '/VLC/ { print $1}'")
tell application "System Events" to repeat with i in lstNum
set end of windowNames to name of first window of (first application process where unix id = i)
end repeat
return windowNames
关于javascript - 如何通过 osascript 处理同一应用程序的两个实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29391225/
我有以下使用 osascript 命令的 shell 脚本: #!/usr/bin/osascript on run argv tell application "Terminal" ac
我想在 bash 中执行此操作: read -r -d '' script <<'EOF' echo 1 echo 2 echo 3 EOF osascript -e "do shell script
这是我的小测试脚本: do shell script "cd ~/Desktop; ls -l1" set x to the result tell application "Finder" to a
谁能想出解决 osascript 按名称索引瓶颈的方法,因为它引用了同一应用程序的多个实例? 如果我们获得两个进程 ID——同一个应用程序的两个不同实例中的每一个,osascript 返回同一个实例以
以下问题与发布在 this question 上的答案有关: 我喜欢创建自己的函数来打开新终端的想法,因此 Craig Walker 在上述问题中链接到的脚本符合我的需要。该脚本由 Mark Liya
set thisFolder to (choose folder with prompt "Choose the folder to which you want to attach the acti
我写了一个发送文本的命令,但它不起作用,即使我将命令粘贴到其中也是如此。是否有语法错误或我遗漏了什么? 打印的命令是:/usr/bin/osascript -e '告诉应用程序“消息”' -e '将
我有一些使用 osascript 执行的 AppleScript 代码。这是一个更大的 Perl 程序的一部分。我希望能够从 AppleScript 打印到标准输出,然后让 Perl 脚本处理输出。但
我正在尝试从 Python 将文件路径传递到 n Applescript 中,不知道如何实现它。下面的代码显示了工作的 Applescript 代码,它提示打开一个或多个文件,然后重新分配音频 cha
好的,我有这个非常简单的 python 脚本: import time import sys for i in range(25): time.sleep(1) print(i) sy
我有一个 LiveCode 应用程序,作为更大目的的一部分,它检查“最前面”应用程序的名称(即菜单栏中的名称)。我让 LiveCode 通过 shell 运行它: osascript -e 'tell
OSX 优胜美地 Xcode 7.1 我正在通过 vim 或 atom 开发 iOS。但有件事我不喜欢。必须按 command + tab 然后切换到 Xcode 才能运行。 我发现这个脚本可以解决这
我一直在尝试创建一个脚本来生成所有可用服务名称的 XML 结构。它在脚本编辑器中运行良好,但无法从命令行运行。我的代码是: ObjC.import("Cocoa"); ObjC.import("App
我正在尝试在远程网格上托管的 selenium 测试中运行以下 Applescript 片段。 protected void enableTouchIDLogin(){ Runtime runt
我知道我可能非常想念这个, 但有人知道为什么这会不断返回错误吗? $ node -v && node v0.4.6 > var cmd = 'osascript -e "open location \
使用 python,我正在尝试“””字符串和 osascript。所以我所拥有的是这个 os.system("""arch -i386 osascript -e
使用 osascript,我可以告诉终端应用程序在单独的终端窗口中打开 ssh 连接,但是,当我尝试在脚本中传递 bash 变量时遇到错误。 这个脚本工作得很好: osascript -e 'tell
我想问一下在设计 Mac (Sierra) 应用程序时是否可以在 swift/xcode 中使用 osascript。所以我基本上只是使用 Xcode 和 swift 构建了一个空的应用程序。 我现在
Apple 权限 hell :osascript 和辅助功能(辅助访问)。 错误:不允许使用 osascript 进行辅助访问。 (-1719) 我在名为 QuickenUpdate.scpt 的文件
我正在尝试在新窗口中创建一个新进程。我正在关注此示例的命令 Running a command in a new Mac OS X Terminal window (使用 Java 进程构建器)。我打
我是一名优秀的程序员,十分优秀!