使用Applescript有选择地激活具有两个运行实例的应用程序的一个实例

使用Applescript有选择地激活具有两个运行实例的应用程序的一个实例,applescript,Applescript,我正在我的计算机(OS X 10.9)上同时运行两个iTerm实例。我复制了iTerm捆绑包并进行了以下更改: 重命名bundleiTerm copy.appscratchpad.app 重命名scratchpad.app/Contents/MacOS/iTermscratchpad 在Info.plist中,设置CFBundleNamescratchpad 在Info.plist中,设置CFBundleIdentifiercom.seanmackey.scratchpad 在Info.pli

我正在我的计算机(OS X 10.9)上同时运行两个iTerm实例。我复制了iTerm捆绑包并进行了以下更改:

  • 重命名bundleiTerm copy.appscratchpad.app
  • 重命名scratchpad.app/Contents/MacOS/iTermscratchpad
  • Info.plist
    中,设置CFBundleNamescratchpad
  • Info.plist
    中,设置CFBundleIdentifiercom.seanmackey.scratchpad
  • Info.plist
    中,设置CbundleExecutablescratchpad
我能够成功地运行这两个实例——在dock和菜单栏中,第二个实例根据需要标记为“scratchpad”。但是,当我执行applescript时:

tell application "iTerm" to activate

这将导致我的草稿行激活。如何避免此情况,使此行仅激活我的原始iTerm实例?

尝试按应用程序[bundle]ID激活:

告诉应用程序id“net.sourceforge.iTerm”激活
至于你所看到的发生的原因: 您可能需要更改另外两个属性:

  • CbundleDisplayName
  • CFDisplayName
顺便说一下,要获取应用程序的[bundle]ID,只需运行(在终端中),例如:

osascript -e 'tell application "iTerm" to id'

我喜欢mklement发布的关于使用应用程序id的内容。另一种方法是使用系统事件和进程。进程具有可帮助您识别特定正在运行的进程的属性。例如,可以使用“显示的名称”属性或“名称”或“短名称”或“标题”等。这样,您可以查看这些属性,找出其独特之处并加以识别。一旦你有了特定的流程,你可以像这样把它带到前台。注意,在本例中,我使用“displayed name”属性来标识Safari

tell application "System Events"
    set theProcess to first application process whose displayed name is "Safari"
    set frontmost of theProcess to true
end tell