From d1f9855d3960de623645b2f20d43ab9fae0b6923 Mon Sep 17 00:00:00 2001 From: Jirat K Date: Tue, 6 Dec 2016 02:47:09 +0700 Subject: [PATCH 1/4] Reuse empty tab on open chrome apple script --- .../react-dev-utils/openChrome.applescript | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/packages/react-dev-utils/openChrome.applescript b/packages/react-dev-utils/openChrome.applescript index b36b70f6cfc..0eb6e0b7730 100644 --- a/packages/react-dev-utils/openChrome.applescript +++ b/packages/react-dev-utils/openChrome.applescript @@ -34,16 +34,49 @@ on run argv end if end repeat + -- Reload debugging tab if found + -- then return if found then tell theTab to reload set index of theWindow to 1 set theWindow's active tab index to theTabIndex tell theWindow to activate + return + end if + + -- In case debugging tab was not found + -- We try to find an empty tab instead + set foundEmpty to false + set theEmptyTabIndex to -1 + repeat with theWindow in every window + set theEmptyTabIndex to 0 + repeat with theTab in every tab of theWindow + set theEmptyTabIndex to theEmptyTabIndex + 1 + if theTab's URL as string contains "chrome://newtab/" then + set foundEmpty to true + exit repeat + end if + end repeat + + if foundEmpty then + exit repeat + end if + end repeat + + -- if empty tab was found + if foundEmpty then + set URL of theTab to theURL + set index of theWindow to 1 + set theWindow's active tab index to theEmptyTabIndex + tell theWindow to activate else + -- both debugging and empty tab were not found + -- make a new tab with url tell window 1 activate make new tab with properties {URL:theURL} end tell end if + end tell end run From f6e9798e2591b7a329873aec31920eaee92936ab Mon Sep 17 00:00:00 2001 From: Jirat K Date: Tue, 6 Dec 2016 19:31:55 +0700 Subject: [PATCH 2/4] Break find tab into function --- .../react-dev-utils/openChrome.applescript | 102 ++++++++++-------- 1 file changed, 57 insertions(+), 45 deletions(-) diff --git a/packages/react-dev-utils/openChrome.applescript b/packages/react-dev-utils/openChrome.applescript index 0eb6e0b7730..ba9f8b74250 100644 --- a/packages/react-dev-utils/openChrome.applescript +++ b/packages/react-dev-utils/openChrome.applescript @@ -16,27 +16,15 @@ on run argv make new window end if - -- Find a tab currently running the debugger - set found to false - set theTabIndex to -1 - repeat with theWindow in every window - set theTabIndex to 0 - repeat with theTab in every tab of theWindow - set theTabIndex to theTabIndex + 1 - if theTab's URL as string contains theURL then - set found to true - exit repeat - end if - end repeat - - if found then - exit repeat - end if - end repeat - - -- Reload debugging tab if found + -- 1: Looking for tab running debugger + -- then, Reload debugging tab if found -- then return - if found then + set foundWindow to my findTabByURL(theURL) + if foundWindow is not equal null then + set theTab to foundWindow's targetTab + set theTabIndex to foundWindow's targetTabIndex + set theWindow to foundWindow's targetWindow + tell theTab to reload set index of theWindow to 1 set theWindow's active tab index to theTabIndex @@ -44,39 +32,63 @@ on run argv return end if + -- 2: Looking for Empty tab -- In case debugging tab was not found -- We try to find an empty tab instead - set foundEmpty to false - set theEmptyTabIndex to -1 + set foundWindow to my findTabByURL("chrome://newtab/") + if foundWindow is not equal null then + set theTab to foundWindow's targetTab + set theTabIndex to foundWindow's targetTabIndex + set theWindow to foundWindow's targetWindow + + set URL of theTab to theURL + set index of theWindow to 1 + set theWindow's active tab index to theTabIndex + tell theWindow to activate + return + end if + + -- 3: Create new tab + -- both debugging and empty tab were not found + -- make a new tab with url + tell window 1 + activate + make new tab with properties {URL:theURL} + end tell + end tell +end run + +-- Function: +-- Lookup tab with given url +-- return object +-- - targetWindow +-- - targetTab +-- - targetTabIndex +on findTabByURL(lookupUrl) + tell application "Google Chrome" + -- Find a tab currently running the debugger + set found to false + set theTabIndex to -1 repeat with theWindow in every window - set theEmptyTabIndex to 0 + set theTabIndex to 0 repeat with theTab in every tab of theWindow - set theEmptyTabIndex to theEmptyTabIndex + 1 - if theTab's URL as string contains "chrome://newtab/" then - set foundEmpty to true + set theTabIndex to theTabIndex + 1 + if (theTab's URL as string) contains lookupUrl then + set found to true exit repeat end if end repeat - if foundEmpty then - exit repeat + if found then + -- create object + script myWindow + property targetTab: theTab + property targetTabIndex: theTabIndex + property targetWindow: theWindow + end script + return myWindow end if end repeat - - -- if empty tab was found - if foundEmpty then - set URL of theTab to theURL - set index of theWindow to 1 - set theWindow's active tab index to theEmptyTabIndex - tell theWindow to activate - else - -- both debugging and empty tab were not found - -- make a new tab with url - tell window 1 - activate - make new tab with properties {URL:theURL} - end tell - end if - end tell -end run + return null +end findTabByURL From cffadbbd5e53c6d29144a85c999961748de5a1e5 Mon Sep 17 00:00:00 2001 From: Jirat K Date: Tue, 6 Dec 2016 19:44:27 +0700 Subject: [PATCH 3/4] Use property to store found --- .../react-dev-utils/openChrome.applescript | 64 ++++++++----------- 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/packages/react-dev-utils/openChrome.applescript b/packages/react-dev-utils/openChrome.applescript index ba9f8b74250..d83e4e94fa4 100644 --- a/packages/react-dev-utils/openChrome.applescript +++ b/packages/react-dev-utils/openChrome.applescript @@ -7,10 +7,14 @@ This source code is licensed under the BSD-style license found in the of patent rights can be found in the PATENTS file in the same directory. *) +property targetTab: null +property targetTabIndex: -1 +property targetWindow: null + on run argv set theURL to item 1 of argv - tell application "Chrome" + tell application "Google Chrome" if (count every window) = 0 then make new window @@ -19,32 +23,24 @@ on run argv -- 1: Looking for tab running debugger -- then, Reload debugging tab if found -- then return - set foundWindow to my findTabByURL(theURL) - if foundWindow is not equal null then - set theTab to foundWindow's targetTab - set theTabIndex to foundWindow's targetTabIndex - set theWindow to foundWindow's targetWindow - - tell theTab to reload - set index of theWindow to 1 - set theWindow's active tab index to theTabIndex - tell theWindow to activate + set found to my lookupTabWithUrl(theURL) + if found then + tell targetTab to reload + set index of targetWindow to 1 + set targetWindow's active tab index to targetTabIndex + tell targetWindow to activate return end if -- 2: Looking for Empty tab -- In case debugging tab was not found -- We try to find an empty tab instead - set foundWindow to my findTabByURL("chrome://newtab/") - if foundWindow is not equal null then - set theTab to foundWindow's targetTab - set theTabIndex to foundWindow's targetTabIndex - set theWindow to foundWindow's targetWindow - - set URL of theTab to theURL - set index of theWindow to 1 - set theWindow's active tab index to theTabIndex - tell theWindow to activate + set found to my lookupTabWithUrl("chrome://newtab/") + if found then + set URL of targetTab to theURL + set index of targetWindow to 1 + set targetWindow's active tab index to targetTabIndex + tell targetWindow to activate return end if @@ -60,13 +56,11 @@ end run -- Function: -- Lookup tab with given url --- return object --- - targetWindow --- - targetTab --- - targetTabIndex -on findTabByURL(lookupUrl) +-- if found, store tab, index, and window in properties +-- (properties were declared on top of file) +on lookupTabWithUrl(lookupUrl) tell application "Google Chrome" - -- Find a tab currently running the debugger + -- Find a tab with the given url set found to false set theTabIndex to -1 repeat with theWindow in every window @@ -74,21 +68,19 @@ on findTabByURL(lookupUrl) repeat with theTab in every tab of theWindow set theTabIndex to theTabIndex + 1 if (theTab's URL as string) contains lookupUrl then + -- assign tab, tab index, and window to properties + set targetTab to theTab + set targetTabIndex to theTabIndex + set targetWindow to theWindow set found to true exit repeat end if end repeat if found then - -- create object - script myWindow - property targetTab: theTab - property targetTabIndex: theTabIndex - property targetWindow: theWindow - end script - return myWindow + exit repeat end if end repeat end tell - return null -end findTabByURL + return found +end lookupTabWithUrl From 49c49f4c61b62dabb6bdba1f4c0c8f50b7f8a843 Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Tue, 6 Dec 2016 17:03:26 +0000 Subject: [PATCH 4/4] Fix minor issues that caused window to not get active --- packages/react-dev-utils/openChrome.applescript | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/react-dev-utils/openChrome.applescript b/packages/react-dev-utils/openChrome.applescript index d83e4e94fa4..f5d79bb6201 100644 --- a/packages/react-dev-utils/openChrome.applescript +++ b/packages/react-dev-utils/openChrome.applescript @@ -25,10 +25,10 @@ on run argv -- then return set found to my lookupTabWithUrl(theURL) if found then - tell targetTab to reload - set index of targetWindow to 1 set targetWindow's active tab index to targetTabIndex + tell targetTab to reload tell targetWindow to activate + set index of targetWindow to 1 return end if @@ -37,9 +37,8 @@ on run argv -- We try to find an empty tab instead set found to my lookupTabWithUrl("chrome://newtab/") if found then - set URL of targetTab to theURL - set index of targetWindow to 1 set targetWindow's active tab index to targetTabIndex + set URL of targetTab to theURL tell targetWindow to activate return end if @@ -48,8 +47,8 @@ on run argv -- both debugging and empty tab were not found -- make a new tab with url tell window 1 - activate - make new tab with properties {URL:theURL} + activate + make new tab with properties {URL:theURL} end tell end tell end run