0

This question: Google Apps Script to open a URL leaves one thing unanswered: After opening the URL, can the script close the tab (or window) used to open the URL? Specifically, I'm using the code from that post to open a PDF for printing. After printing, and closing the PDF file, the tab opened by the script remains. Can the same script that opened the tab ("_blank"), also close the tab?

<script> window.open('<?=url?>', '_blank', 'width=100, height=100'); 
google.script.host.close(); 
window.close(); 
</script>

The code above comes from code posted by stephen-m-harris

TheMaster
  • 45,448
  • 6
  • 62
  • 85
Kevin
  • 23
  • 1
  • 3
  • Yes⠀‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎‎⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀ – TheMaster Sep 25 '22 at 15:47
  • Ah. That is good to know. Thank you. Now that we know it's possible would you, TheMASTER, happen to have the working code to do so; and, if so having, would you be willing to share that code here, in this thread; and if willing, would you actually show, for the world to see -- but specifically, me -- the line-by-line apps script ES6 code that accomplishes the objective requested above? – Kevin Sep 26 '22 at 16:16
  • 1
    Alternate proposal: You read https://developer.mozilla.org/en-US/docs/Web/API/Window/close and show what you've tried with debugging details, if you're unable to accomplish the task. – TheMaster Sep 26 '22 at 16:22
  • I've tried window.close(). Nothing happens, that is, the tab opened by the script code to open the URL does not close. – Kevin Sep 26 '22 at 16:26
  • Comments are not for adding code. [Edit] your question to post your code, a [mre] and I'll add a answer. – TheMaster Sep 26 '22 at 16:32
  • If you're adding someone else's code, make sure to given them appropriate credit. – TheMaster Sep 26 '22 at 16:33

1 Answers1

0

window refers to the current window. To close the window that was opened, you should call window.close on that widow. window.open gives a reference to the window that was opened.

<script> 
  const openedWindow = window.open('<?=url?>', '_blank', 'width=100, height=100'); 
  //google.script.host.close(); 
  setTimeout(() => openedWindow.close(), 5000);
  const closeSelf = () => openedWindow.closed && google.script.host.close();
  setInterval(closeSelf, 5000) 
</script>

A timeout is added because the openedWindow might not have been loaded and may not respond to .close() request as soon as it was opened. setInterval is used to check if the window is closed every 5s and if closed, close the current window too.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • The most recent update with setTimeout, etc. code works! Thanks so much for providing the solution. I think I understand how it works, but I don't understand the purpose of the arrow function technique here. This seems like a unique use of that syntax. Can you explain? – Kevin Sep 28 '22 at 21:07
  • @Kevin No unique use. Just normal arrow syntax. In `setTimeout` however, it gives access to enclosing context's `this`( though it isn't important in this case). See `this` problem: https://developer.mozilla.org/en-US/docs/Web/API/setTimeout `The most recent update` Do you mean the original one didn't work? – TheMaster Sep 28 '22 at 21:11
  • @Kevin I added the reason for `setTimeout` in the answer. I didn't test any of it, for your question. All of it was just based on previous experience. – TheMaster Sep 28 '22 at 21:28
  • Correct. I didn't save a screen shot, but every attempt to print the file produced a Google message page that I interpreted to mean the file no longer existed. – Kevin Sep 28 '22 at 21:44
  • Regarding the use of arrow syntax, why wouldn't this work for closing the window: setTimeout(openedWindow.close(), 5000); ? – Kevin Sep 28 '22 at 21:47
  • Ok. I read the referenced "this" problem on the `setTimeout` page. A bit steep, but I'll let it sink in and read it again. It also answered my last question, so thanks for the reference. – Kevin Sep 28 '22 at 22:47