openChrome.applescript 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. (*
  2. Copyright (c) 2015-present, Facebook, Inc.
  3. This source code is licensed under the MIT license found in the
  4. LICENSE file at
  5. https://github.com/facebookincubator/create-react-app/blob/master/LICENSE
  6. *)
  7. property targetTab: null
  8. property targetTabIndex: -1
  9. property targetWindow: null
  10. on run argv
  11. set theURL to item 1 of argv
  12. tell application "Chrome"
  13. if (count every window) = 0 then
  14. make new window
  15. end if
  16. -- 1: Looking for tab running debugger
  17. -- then, Reload debugging tab if found
  18. -- then return
  19. set found to my lookupTabWithUrl(theURL)
  20. if found then
  21. set targetWindow's active tab index to targetTabIndex
  22. tell targetTab to reload
  23. tell targetWindow to activate
  24. set index of targetWindow to 1
  25. return
  26. end if
  27. -- 2: Looking for Empty tab
  28. -- In case debugging tab was not found
  29. -- We try to find an empty tab instead
  30. set found to my lookupTabWithUrl("chrome://newtab/")
  31. if found then
  32. set targetWindow's active tab index to targetTabIndex
  33. set URL of targetTab to theURL
  34. tell targetWindow to activate
  35. return
  36. end if
  37. -- 3: Create new tab
  38. -- both debugging and empty tab were not found
  39. -- make a new tab with url
  40. tell window 1
  41. activate
  42. make new tab with properties {URL:theURL}
  43. end tell
  44. end tell
  45. end run
  46. -- Function:
  47. -- Lookup tab with given url
  48. -- if found, store tab, index, and window in properties
  49. -- (properties were declared on top of file)
  50. on lookupTabWithUrl(lookupUrl)
  51. tell application "Chrome"
  52. -- Find a tab with the given url
  53. set found to false
  54. set theTabIndex to -1
  55. repeat with theWindow in every window
  56. set theTabIndex to 0
  57. repeat with theTab in every tab of theWindow
  58. set theTabIndex to theTabIndex + 1
  59. if (theTab's URL as string) contains lookupUrl then
  60. -- assign tab, tab index, and window to properties
  61. set targetTab to theTab
  62. set targetTabIndex to theTabIndex
  63. set targetWindow to theWindow
  64. set found to true
  65. exit repeat
  66. end if
  67. end repeat
  68. if found then
  69. exit repeat
  70. end if
  71. end repeat
  72. end tell
  73. return found
  74. end lookupTabWithUrl