vitorgalvao/custom-alfred-iterm-scripts

Needs to wait for unfinished script executions

Closed this issue · 4 comments

The current way it's implemented it won't wait for the shell session to be ready to receive a new input. For instance, I'm having an issue where I have a script that perform some actions whenever a new session is created - it's part of my PATH.

This causes a small delay, ~500ms, which is enough to make the AppleScript instructions to misbehave.

Just to give an example:
Let's say that my search in Alfred returns me the following path: /Users/foobar/Documents/bla.pdf. When I choose to open that file in iTerm what I have as the output is the following:

cd /Users/foobar/Documents/bla.pdf
My script printed this message, meaning it just ended its execution...
foobar@~/SomeRandomFolder $

I have no idea how to fix this behavior using AppleScript. The script should ideally wait for the session to be ready before moving on and writing the text:

set mysession to (launch session \"Default Session\")
-- add something here to wait for the session to be ready?
tell mysession to write text q

Any ideas?

Thanks!

Btw I know the quick fix for it would be to add a delay 1 there… but as that's also a dirty fix I was hoping for a better way to approach this.

Any suggestions are welcome, as AppleScript is definitely not one of my best skills 😛

OOOOH that is an interesting one... I will have to see if there is a way to scrutinise if a tab is "ready" for a command or not.

I will see what can be done with iTerm, and will come back to discuss options (as we then have to figure out how to handle the majority of cases).

Will keep you posted :)

(Using the latest version of iTerm2. I couldn't find an iTerm 2.1.1 version to test the other script.)

tell application "iTerm"
    activate
    try
        select first window
        set onlywindow to false
    on error
        create window with default profile
        select first window
        set onlywindow to true
    end try
    tell the first window
        if onlywindow is false then
            create tab with default profile
        end if
        repeat while is processing of current session is true
            delay 0.1
        end repeat
        tell current session to write text "haskell"
    end tell
end tell

Hello everyone,

Stuart no longer has the time to maintain these scripts, and as such has been kind enough to transfer the repository to me. I’ve shortened the code, made it always open a command in a new iTerm tab, and removed support for older iTerm version.

You might know me as vitor from the Alfred Forums or if you’ve ever contributed to Homebrew Cask.

I’m reviewing issues and pull requests right now, so apologies if you’ve received this message more than once.

If I close an issue with a problem that still occurs in the new version, please open a new issue.

Try the new script.