0

这个问题可能看起来很奇怪。但是,我需要与在全球范围内以不同名称发布的应用程序进行交互,例如。AppEN、AppGB、AppDE 等...

我正在寻找允许我使用此命令的解决方案:

tell application process "AppnameGB"

但是,它应该适用于此应用程序的所有不同变体。我不知道这是否可能,但在应用程序名称中搜索字符串可以解决问题:告诉应用程序进程在其名称中包含“Appname”。

4

2 回答 2

2

如果该过程已经打开,您可以使用以下内容:

tell application "System Events"
    tell (process 1 where name starts with "TextEd")
        properties
        set f to its file
    end tell
end tell
tell application (f as text)
    properties
end tell

告诉 Finder 列出文件真的很慢:

tell application "Finder"
    item 1 of (path to applications folder) where name starts with "Text"
end tell

你可以使用do shell script

set a to do shell script "ls /Applications/ | grep -m1 '^Text.*\\.app$'"
tell application a
    properties
end tell

set a to do shell script "mdfind 'kMDItemContentType==com.apple.application-bundle&&kMDItemFSName==Text*' | head -n1"还将在应用程序文件夹之外搜索。

于 2012-12-17T13:03:14.470 回答
2

最简单的方法是您可以使用应用程序的包标识符而不是其名称。很可能捆绑 ID 不会更改,而名称会更改。因此,在该应用程序上使用此脚本并检查捆绑 ID 是否更改。

tell application "Finder" to set bundleID to id of (choose file)

如果你发现它没有改变,那么你可以像这样通过applescript访问它......

tell application "com.bundle.id"
   activate
   -- do something
end tell

您唯一的另一种选择是获取所有应用程序的列表,并按照您的建议遍历它们检查名称。像这样的东西可以工作,但速度很慢。

-- use spotlight to find all the apps on the computer
set cmd to "mdfind 'kMDItemContentType == \"com.apple.application-bundle\"'"
set foundApps to paragraphs of (do shell script cmd)

-- search the found apps looking for the one you're interested in
set appPath to missing value
repeat with i from 1 to count of foundApps
    if (item i of foundApps) contains "Appname" then
        set appPath to item i of foundApps
        exit repeat
    end if
end repeat

if appPath is missing value then error "Couldn't find the app!"

-- open the app
set macAppPath to (POSIX file appPath) as text
tell application macAppPath
    activate
    -- do something
end tell
于 2012-12-17T13:14:01.253 回答