openChrome.applescript 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. with timeout of 2 seconds
  13. tell application "Chrome"
  14. if (count every window) = 0 then
  15. make new window
  16. end if
  17. -- 1: Looking for tab running debugger
  18. -- then, Reload debugging tab if found
  19. -- then return
  20. set found to my lookupTabWithUrl(theURL)
  21. if found then
  22. set targetWindow's active tab index to targetTabIndex
  23. tell targetTab to reload
  24. tell targetWindow to activate
  25. set index of targetWindow to 1
  26. return
  27. end if
  28. -- 2: Looking for Empty tab
  29. -- In case debugging tab was not found
  30. -- We try to find an empty tab instead
  31. set found to my lookupTabWithUrl("chrome://newtab/")
  32. if found then
  33. set targetWindow's active tab index to targetTabIndex
  34. set URL of targetTab to theURL
  35. tell targetWindow to activate
  36. return
  37. end if
  38. -- 3: Create new tab
  39. -- both debugging and empty tab were not found
  40. -- make a new tab with url
  41. tell window 1
  42. activate
  43. make new tab with properties {URL:theURL}
  44. end tell
  45. end tell
  46. end timeout
  47. end run
  48. -- Function:
  49. -- Lookup tab with given url
  50. -- if found, store tab, index, and window in properties
  51. -- (properties were declared on top of file)
  52. on lookupTabWithUrl(lookupUrl)
  53. tell application "Chrome"
  54. -- Find a tab with the given url
  55. set found to false
  56. set theTabIndex to -1
  57. repeat with theWindow in every window
  58. set theTabIndex to 0
  59. repeat with theTab in every tab of theWindow
  60. set theTabIndex to theTabIndex + 1
  61. if (theTab's URL as string) contains lookupUrl then
  62. -- assign tab, tab index, and window to properties
  63. set targetTab to theTab
  64. set targetTabIndex to theTabIndex
  65. set targetWindow to theWindow
  66. set found to true
  67. exit repeat
  68. end if
  69. end repeat
  70. if found then
  71. exit repeat
  72. end if
  73. end repeat
  74. end tell
  75. return found
  76. end lookupTabWithUrl