告诉包含单词的应用程序进程

时间:2012-12-17 12:07:49

标签: macos applescript

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

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

tell application process "AppnameGB"

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

2 个答案:

答案 0 :(得分: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"也会在应用程序文件夹外搜索。

答案 1 :(得分: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
相关问题