You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

openChrome.applescript 2.2 KiB

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