openChrome.applescript 2.6 KB

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