Can anyone think of a workaround for the osascript index-by-name bottle-neck in its reference to multiple instances of the same application ?
If we obtain two process ids – one for each of two different instances of the same application, osascript returns the same instance in exchange for either pid - as if it first maps the pid to an application name, and then retrieves the first application process with that name.
For example, start two different instances of VLC.app, playing two different video files, with something like:
open -na /Applications/VLC.app ~/fileA.m4v
open -na /Applications/VLC.app ~/fileB.m4v
then obtain the two separate application process ids with, for example:
echo "$(ps -ceo pid=,comm= | awk '/VLC/ { print $1}')"
We can then use Applescript or Yosemite JXA Javascript to get a reference to an application object from either pid.
It turns out, however, that whichever process id we supply, we are always returned a reference to the same instance, running the same video file, as if osascript simply translates a pid to an application name, and then always returns the first process which matches that name.
Yosemite Javascript for applications:
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()
};
}
Applescript:
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
Any thoughts on a route to scripting each instance separately ?