Reputation: 103
Is there any way we can get the window title and its corresponding process name in mac.
Currently, I am only able to get the window title using AppleScript:
tell application "System Events" to get the title of every window of (every process whose visible is true)
If we can do this via any electron API/node module that would also be great.
Basically I just want to get a list of active window title along with their process id/name
Upvotes: 1
Views: 1597
Reputation: 7555
I guess this all depends on what you're really trying to do...
The following example AppleScript code, shown further below, will create a list of visible processes and their windows titles as a list of lists in processNamesAndWindowTitlesLists
.
In other words, each item of processNamesAndWindowTitlesLists
is a list containing a process name and its windows titles as a list.
For example, on my system at the present moment, item 1 of processNamesAndWindowTitlesLists
is:
{"TextEdit", {"Untitled", "Untitled 2"}}
So, with each item in processNamesAndWindowTitlesLists
its first item is the process name and the second item is a list of its windows titles.
Example AppleScript code:
set tmpList to {}
set windowTitles to {}
set visibleProcesses to {}
set visibleProcessesWithWindows to {}
set processNamesAndWindowTitles to {}
set processNamesAndWindowTitlesLists to {}
tell application "System Events"
set visibleProcesses to name of (every process whose visible is true)
repeat with processName in visibleProcesses
if (count windows of process processName) is not 0 then
set end of visibleProcessesWithWindows to processName
end if
end repeat
repeat with processName in visibleProcessesWithWindows
set end of windowTitles to (name of every window of process processName)
set end of tmpList to (processName as list) & windowTitles
set end of processNamesAndWindowTitles to tmpList
set windowTitles to {}
set tmpList to {}
end repeat
end tell
repeat with aItem in processNamesAndWindowTitles
set end of processNamesAndWindowTitlesLists to item 1 of aItem
end repeat
return processNamesAndWindowTitlesLists
Note: The example AppleScript code is just that and does not contain any error handling as may be appropriate. The onus is upon the user to add any error handling as may be appropriate, needed or wanted. Have a look at the try statement and error statement in the AppleScript Language Guide. See also, Working with Errors.
Upvotes: 3