1

我一直在制作这个 applescript 应用程序来自动化不支持脚本的应用程序 (TimeFactory2) 的行为。所以,我最终用它来编写 UI 脚本。事情是,为了我的使用,我希望应用程序的多个实例同时运行,所以我复制了 .app 并启动​​了每个副本(重点是利用多线程,每个实例都使用我计算机上的一个核心 -该应用程序非常占用 CPU,但不支持多线程)。所以我最初得到了这段代码(对不起法语评论):

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

...脚本无法再判断打开的窗口是否存在(即使确实存在),因此会不断模拟 command-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

...我被困在那里。几乎在互联网上浏览了这个问题的解决方案,但我仍然一无所知。有人看到任何解决方案吗?或者可能对整个脚本采取不同的方法来避免这个问题?

4

3 回答 3

1

我找到了一种区分进程的方法:使用语法:

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

tell process id processID
    etc.

我可以根据他们的 id “告诉”不同的进程,这是独一无二的。

要获取每个进程的 ID,我可以使用进程的“文件”属性,其中包括 .app 包的名称,这取决于我要控制的应用程序的哪个副本。

于 2012-09-10T15:10:29.957 回答
0

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

于 2012-09-08T18:20:42.030 回答
0

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

告诉应用程序“系统事件”
告诉应用程序进程“TimeFactory2 Demo”

于 2012-09-10T01:25:16.880 回答