Skip to content

Unnecessary delay when opening a terminal #29184

Closed

Description

The Issue

I'm writing an AppleScript for opening a terminal and writing some stuff in it, the problem is that without an hard coded delay between the action "open the terminal" and the action "write this stuff in it" it doesn't work.

This is what, ideally, should be working (minus the useless delay call):

tell application "Visual Studio Code" to activate
tell application "System Events"
	keystroke "`" using control down
	delay 0
	keystroke "placeholder"
end tell

This is what I see in the terminal if I run the AppleScript with different delay values:

  • 0: fabiomac:project fabio$ ❌
  • 0.1: fabiomac:project fabio$ ❌
  • 0.15 placeholderfabiomac:project fabio$ placeholder ❌ 😮
  • 0.2 fabiomac:project fabio$ placeholder ✅

IMHO this is broken because I shouldn't have to hard code environment-dependant delay calls in my AppleScripts, and consequentially I shouldn't have to wait those extra seconds.

My Setup

  • VSCode Version: Code 1.13.1 [Unsupported] (379d2ef, 2017-06-14T18:13:05.928Z)
  • OS Version: Darwin x64 16.7.0
  • Extensions:
Extension Author Version
EditorConfig EditorConfig 0.9.3
status-bar-tasks GuardRex 0.2.0
beautify HookyQR 1.1.1
crane HvyIndustries 0.3.6
vscode-docker PeterJausovec 0.0.16
code-settings-sync Shan 2.8.1
sort-lines Tyriar 1.3.0
vscode-color anseki 0.4.1
vscode-custom-css be5invis 2.3.2
gitignore codezombiech 0.5.0
vscode-svgviewer cssho 1.4.2
vscode-dash deerawan 1.5.0
python donjayamanne 0.6.5
applescript idleberg 0.6.0
indentation-level-movement kaiwood 1.1.1
graphql-for-vscode kumar-harsh 1.4.1
ecdc mitchdenny 0.10.3
vscode-duplicate mrmlnc 1.1.0
debugger-for-chrome msjsdiag 3.1.4
ava samverschueren 0.4.0
todotasks sandy081 0.5.0
php-ci small 0.2.0
Align steve8708 0.2.0
cordova-tools vsmobile 1.2.7
vscode-react-native vsmobile 0.3.2
change-case wmaurer 1.0.0

Activity

  1. Tyriar commented on Jun 21, 2017

    @Tyriar
    Contributor

    Fabio Spampinato (@fabiospampinato) the problem is that the terminal takes more than 0ms to start up? It takes time to spin up the pty and attach everything, a little longer than other terminals because everything is going through V8 first.

  2. added
    terminal
    info-needed
    on Jun 21, 2017
  3. fabiospampinato commented on Jun 21, 2017

    @fabiospampinato
    ContributorAuthor

    Daniel Imms (@Tyriar) the problem is that I have to put workarounds in my AppleScripts in order for them to work.

    Of course it won't be as fast as a native terminal, but perhaps instead of just throwing away whatever is typed during the time where the terminal panel is in focus but the new pty hasn't started yet we could store it and replay it back once the pty started. What do you think?

  4. added
    feature-request
    and removed
    info-needed
    on Jun 21, 2017
  5. added this to the Backlog milestone on Jun 21, 2017
  6. added
    help wanted
    on Jun 21, 2017
  7. Tyriar commented on Jun 21, 2017

    @Tyriar
    Contributor

    Sure, I'll add it to the backlog and open it up for PRs. To anyone interested in implementing this you will need to listen to key strokes in TerminalInstance and add them to a queue. Once the pty is ready then push the events through the normal mechanism.

  8. fabiospampinato commented on Jun 21, 2017

    @fabiospampinato
    ContributorAuthor

    Sounds nice, thanks!

  9. cleidigh commented on Jun 21, 2017

    @cleidigh
    Contributor

    Fabio Spampinato (@fabiospampinato)
    Daniel Imms (@Tyriar)
    FTW - somewhere in the last month/1.5months I started to see a similar problem
    under Windows using the run commands function on a text file I use for various DOS commands
    building VS code running git commands, et cetera. before I could use run commands
    in the terminal would open and run the command for 100% without fail
    recently the terminal opens but the subsequent command is only executed seventy five percent
    of the time and I have to redo the run commands
    you can mark me down to do a PR on this

  10. cleidigh commented on Jun 21, 2017

    @cleidigh
    Contributor

    Daniel Imms (@Tyriar)
    Fabio Spampinato (@fabiospampinato)

    I haven't looked at the code yet, but I'm assuming there could be some state awareness issues.
    for instance , in order to track and queue the keystrokes the state in context of VS code has to transition
    to the terminal context (presumably minimal time, but not zero as you stated above).
    This means there will always be a window where keystrokes from an automation program
    such as AppleScript will be missed. doing the queuing between the time the open terminal
    command is executed and the PTY is ready only reduces the window but not to zero.

    I also realized my situation with the VSC run commands is related but different since the run command is an internal command.

    I'll look into what I think this takes

  11. 14 remaining items

  12. added a commit that references this issue on Jul 5, 2017
    b3e36f9
  13. fabiospampinato commented on Jul 5, 2017

    @fabiospampinato
    ContributorAuthor

    Sounds great, thanks a lot for your time!

  14. Tyriar commented on Jul 5, 2017

    @Tyriar
    Contributor

    Fabio Spampinato (@fabiospampinato) thanks for the suggestion, this will make things feel much snappier 🙂

  15. cleidigh commented on Jul 5, 2017

    @cleidigh
    Contributor

    Fabio Spampinato (@fabiospampinato)
    Daniel Imms (@Tyriar)
    FYI I have limited means of doing work on OS X since I don't have Dragon voice Control for OS X
    the only way I can do OS X work is a remote VNC connection which introduces its own set of problems
    I finally got a chance to look at OS X unfortunately it does have different timing

    using the exact same script as Fabio Spampinato (@fabiospampinato) I can reproduce the problem as stated at the beginning of this issue. with the data to fix keystrokes to not appear lost but there is a double rendering
    of "placeholder" as shown in Fabio Spampinato (@fabiospampinato) 150mS delay new code with no delay
    so the terminal is getting the correct characters but it duplication before the keystrokes
    are captured by the terminal . as I possibly suspected there is a window where the keystrokes
    after the launch terminal are consumed in two places.
    bottom line is that I think I've improved things but there is still a vulnerability window
    I can try some more experiments but this is difficult for me on OS X
    I had thought that the Windows experiments were sufficiently similar - my error in this

  16. Tyriar commented on Jul 5, 2017

    @Tyriar
    Contributor

    Christopher Leidigh (@cleidigh) the time to input is as small as it can get in a nice way imo, for the OP's case I'd say making an extension to drive the terminal would be a much more reliable way of scripting VS Code.

  17. cleidigh commented on Jul 5, 2017

    @cleidigh
    Contributor

    Daniel Imms (@Tyriar)
    I tend to agree, you can't change code state machine instantly without making a lot of other things
    not work well.
    I just feel badly this helps Windows more than OS X

  18. Tyriar commented on Jul 5, 2017

    @Tyriar
    Contributor

    Christopher Leidigh (@cleidigh) well I can definitely notice the difference on macOS as well 👍

  19. cleidigh commented on Jul 5, 2017

    @cleidigh
    Contributor

    Daniel Imms (@Tyriar)
    I just had someone help me doing direct keyboard Control (on my OS X machine eliminating Dragon)
    as far as I can tell there is duplicate rendering but the terminal only processes the correct keystrokes.
    this may actually be related to what I mentioned about duplicate Windows terminal startup headers.
    will try some more experiments

  20. fabiospampinato commented on Jul 13, 2017

    @fabiospampinato
    ContributorAuthor

    Daniel Imms (@Tyriar) I took your advice and built an extension for this use case. I think it came out pretty nice, I hope this will be useful for somebody else as well.

  21. Tyriar commented on Jul 13, 2017

    @Tyriar
    Contributor

    Fabio Spampinato (@fabiospampinato) that's really cool! I'm going to share it with the team and in #20013

  22. locked and limited conversation to collaborators on Nov 17, 2017
  23. added a commit that references this issue on Sep 6, 2026
    7116923
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

feature-requesthelp wantedterminal

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions