AppleScript that reads active application name, title and URL

北城余情 提交于 2019-12-06 06:04:04

I wanted to make a script that does exactly this running in the background so that I can better keep track of my time. Here's what I came up with:

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)

Credit goes to https://apple.stackexchange.com/a/171738 for getting me on the right track.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!