Giorgio
Giorgio

Reputation: 1613

Tell application process that contain a word

This question may seem strange. However I'm in the need of interacting with an application that is releasing worldwide with different names, eg. AppEN, AppGB, AppDE etc...

I am looking for a solution that allow me to use this command:

tell application process "AppnameGB"

However it should work with all the different variations of this application. I don't know if this is possible but searching for a string in the application name could do the trick: tell application process that contain in its name "Appname".

Upvotes: 0

Views: 340

Answers (2)

regulus6633
regulus6633

Reputation: 19030

The easiest is if you could use the bundle identifier of the application instead of its name. Most likely the bundle ID doesn't change while the name does. So use this script on that application and check if the bundle ID changes or not.

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

If you find it doesn't change then you can access it via applescript like this...

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

Your only other alternative is get a list of all applications, and loop through them checking the name as you suggest. Something like this wold work but is pretty slow.

-- 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

Upvotes: 2

Lri
Lri

Reputation: 27613

If the process is already open, you can use something like this:

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

Telling Finder to list files is really slow:

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

You can use do shell script though:

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" would also search outside the applications folder.

Upvotes: 2

Related Questions