Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. -- AppleScript --
  2.  
  3. -- This example is meant as a simple starting point to show how to get the information in the simplest available way.
  4. -- Keep in mind that when asking for a `return` after another, only the first one will be output.
  5. -- This method is as good as its JXA counterpart.
  6.  
  7. -- Google Chrome
  8. tell application "Google Chrome" to return title of active tab of front window
  9. tell application "Google Chrome" to return URL of active tab of front window
  10. -- Google Chrome Canary
  11. tell application "Google Chrome Canary" to return title of active tab of front window
  12. tell application "Google Chrome Canary" to return URL of active tab of front window
  13. -- Chromium
  14. tell application "Chromium" to return title of active tab of front window
  15. tell application "Chromium" to return URL of active tab of front window
  16. -- Opera
  17. tell application "Opera" to return title of active tab of front window
  18. tell application "Opera" to return URL of active tab of front window
  19. -- Vivaldi
  20. tell application "Vivaldi" to return title of active tab of front window
  21. tell application "Vivaldi" to return URL of active tab of front window
  22. -- Brave
  23. tell application "Brave Browser" to return title of active tab of front window
  24. tell application "Brave Browser" to return URL of active tab of front window
  25. -- Safari
  26. tell application "Safari" to return name of front document
  27. tell application "Safari" to return URL of front document
  28. -- Safari Technology Preview
  29. tell application "Safari Technology Preview" to return name of front document
  30. tell application "Safari Technology Preview" to return URL of front document
  31. -- Webkit
  32. tell application "Webkit" to return name of front document
  33. tell application "Webkit" to return URL of front document
  34.  
  35. -- This example will return both the title and URL for the frontmost tab of the active browser, separated by a newline.
  36. -- Keep in mind that to be able to use a variable in `tell application` — via `using terms from` — we’re basically requiring that referenced browser to be available on the system.
  37. -- That means that to use this on "Google Chrome Canary" or "Chromium", "Google Chrome" needs to be installed. Same for other browsers.
  38. -- This method also does not exit with a non-zero exit status when the frontmost application is not a supported browser.
  39. -- For the aforementioned reasons, this method is inferior to its JXA counterpart.
  40.  
  41. tell application "System Events" to set frontApp to name of first process whose frontmost is true
  42.  
  43. if (frontApp = "Google Chrome") or (frontApp = "Google Chrome Canary") or (frontApp = "Chromium") or (frontApp = "Opera") or (frontApp = "Vivaldi") or (frontApp = "Brave Browser") then
  44. using terms from application "Google Chrome"
  45. tell application frontApp to set currentTabTitle to title of active tab of front window
  46. tell application frontApp to set currentTabUrl to URL of active tab of front window
  47. end using terms from
  48. else if (frontApp = "Safari") or (frontApp = "Safari Technology Preview") or (frontApp = "Webkit") then
  49. using terms from application "Safari"
  50. tell application frontApp to set currentTabTitle to name of front document
  51. tell application frontApp to set currentTabUrl to URL of front document
  52. end using terms from
  53. else
  54. return "You need a supported browser as your frontmost app"
  55. end if
  56.  
  57. return currentTabUrl & "\n" & currentTabTitle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement