AppleScript 读取活动应用程序名称、标题和 URL

Tom*_*iba 6 macos scripting command-line applescript osascript

我需要 AppleScript,当从命令行运行时,它会返回三件事:

  1. 当前活动的应用程序名称;前任。“Google Chrome”、“Safari”、“Steam”、“iTunes”、“Atom”等。
  2. 当前活动的应用程序标题(如果有)
  3. 如果当前活动应用程序是浏览器,我需要当前活动选项卡 URL

例子:

我知道还有一些类似的问题。stackoverflow 上的答案在这里,但我无法将它们组合在一起以完成所有这些工作。非常感谢您的帮助。

clw*_*ght 6

我想制作一个在后台运行的脚本,以便我可以更好地跟踪我的时间。这是我想出的:

tell application "System Events"
    set frontmostProcess to first process where it is frontmost
    set appName to name of frontmostProcess
end tell
tell application appName
    set windowName to the name of the front window
end tell
if appName is equal to "Safari" then
    tell application "Safari"
        set theURL to the URL of the current tab of the front window
    end tell
else if appName is equal to "Google Chrome" then
    tell application "Google Chrome"
        set theURL to the URL of the active tab of the front window
    end tell
else
    set theURL to ""
end if

return (appName, windowName, theURL)
Run Code Online (Sandbox Code Playgroud)

感谢https://apple.stackexchange.com/a/171738让我走上了正确的道路。