Applescript:用户界面脚本,当两个同名的进程存在时如何“告诉”一个进程?

问题描述:

我一直在使这个applescript应用程序自动化一个不支持脚本的应用程序(TimeFactory2)的行为。所以,我最终用它来做UI脚本。事情是,为了我的使用,我想让应用程序的多个实例同时运行,所以我复制了.app并启动了每个副本(重点在于利用多线程,每个实例都使用计算机上的核心 - 该应用程序非常密集,但不支持多线程)。所以我结束了最初使用此代码(遗憾的法国评论):Applescript:用户界面脚本,当两个同名的进程存在时如何“告诉”一个进程?

tell application "TimeFactory2 Demo" 
    activate 
end tell 

tell application "System Events" 
tell process "TimeFactory2 Demo" 
    repeat with extension in extensions_list 
     -- pomme-O pour sélectionner un fichier à ajouter 
     repeat until window "Open" exists 
      keystroke "o" using {command down} 
     end repeat 
     -- pomme-shift-g pour entrer un path de fichier 
     keystroke "g" using {shift down, command down} 
     -- on copie dans le clipboard le path du fichier à séléctionner 
     tell application "Finder" to set the clipboard to (audio_file_path & extension) 
     -- on colle dans la box 
     keystroke "v" using {command down} 
     -- enter enter 
     keystroke return 
     keystroke return 
    end repeat 
    -- dans le cas où la dernière selection de fichier n'aurait rien donné, on fait deux fois escape pour sortir des fenêtre de sélection de fichier 
    key code 53 
    key code 53 
end tell 
end tell 

...当TF2的只有一个实例正在运行的不正常工作。 但是,运行2+实例时,一切都会中断;彻底的测试后,我明白,出于某种原因,作为行:

repeat until window "Open" exists 

...剧本不能再告诉我们,如果窗口打开存在(即使它),从而模拟命令-O键击开始和结束。我认为整个事情来自一个事实,即进程的名称是应用程序的所有实例是相同的,因此代码

tell process "TimeFactory2 Demo" 

含糊不清的系统。然后我搜索的方式起飞的模糊性,并试图给每个进程挑出其UNIX ID(这里是一个实例,其uid是629为例):

set TF2_process_list to every process whose unix id is 629 

set TF2_process to item 1 of TF2_process_list 

tell (process TF2_process) 
    repeat until window "Open" exists 
     keystroke "o" using {command down} 
     delay 2 
    end repeat 
end tell 

这让我以下错误:

error "System Events error : impossible to render {} as integer type." number -1700 from {} to integer 

...我卡在那里。为了解决这个问题,很多人都在网上浏览,但我仍然无能为力。任何人都能看到任何解或者对整个脚本采取不同的方法来避免这个问题?

常规应用程序包未设置为像这样运行(例如,它们都会尝试使用相同的首选项文件),因此您可以尝试更改副本的包标识符(例如添加数字后缀)和在tell语句中使用这些包标识符而不是应用程序名称。

不确定它是否会有所作为,但通常会“告诉应用程序”而不是“告诉进程”:

告诉应用程序“系统事件”
告诉应用程序“TimeFactory2演示”

我找到了一种方法来区分流程:与语法:

set processID to id of first process whose name is "Time Factory 2 Demo" 

tell process id processID 
    etc. 

我可以“告诉”不同的进程对他们ID,它是独一无二的。

要获得每个进程的ID,我可以使用进程的''文件''属性,其中包括.app包的名称,这个名称根据我试图控制的应用程序的副本而不同。