3

How can I insert a text into Terminal.app using AppleScript? I don't mean to execute it (press Enter). Only to insert it. My current script almost works:

tell application "Terminal"
    activate
    tell application "System Events" to keystroke "TextToInsert" using {}
end tell

The trouble is when this is executed by a hot key, e.g. CMD-T, and then the pressed CMD interferes with the keystrokes and changes the desired behavior.

I need either to tell the keystroke command to ignore any modifier keys or to use other command to write the text there. I think write should do it but have no idea how.

  • Using using {} does absolutely noting and if you're not using a modifier, it's not necessary. Also, Terminal's AppleScript Dictionary does not have or understand the write command; however, iTerm2 does: writev : Send text as though it was typed. BTW If Terminal is already running without a window or with a window minimized, activate will not cause a window to be created or unminimized a minimized window and the rest of you code will fail, so you need to code for these possibilities. Also, ⌘T is already a default shortcut in Terminal. – user3439894 Aug 19 '19 at 19:44
4

You can try something like, first copying the text to your clipboard, that you want inserted into Terminal, then have the AppleScript code paste the clipboard into Terminal.

tell application "Terminal"
    activate
    tell application "System Events"
        tell application process "Terminal"
            set frontmost to true
            repeat while not frontmost
                delay 0.1
            end repeat
        end tell
        keystroke "v" using {command down}
    end tell
end tell

If you have all of your Terminal windows set to open in tabs rather than separate windows, this following AppleScript code should account for any of the situations as mentioned by @user3439894 in his comment to your post.

tell application "Terminal"
    activate
    repeat until frontmost
        delay 0.5
    end repeat
    set windowCount to count of window
    if windowCount is not 0 then
        tell its front window
            set isMinimized to miniaturized
        end tell
    end if

    if windowCount is 0 then
        tell application "System Events" to tell application process "Terminal"
            set frontmost to true
            repeat while not frontmost
                delay 0.1
            end repeat
            keystroke "n" using {command down}
            delay 1
            keystroke "v" using {command down}
        end tell
    else
        activate
        repeat until frontmost
            delay 0.5
        end repeat
        delay 0.1
        if ((processes of selected tab of front window) is {} or isMinimized) then
            tell application "System Events" to tell application process "Terminal"
                set frontmost to true
                repeat while not frontmost
                    delay 0.1
                end repeat
                keystroke "n" using {command down}
                delay 1
                keystroke "v" using {command down}
            end tell
        else
            tell application "System Events" to tell application process "Terminal"
                set frontmost to true
                repeat while not frontmost
                    delay 0.1
                end repeat
                delay 0.5
                keystroke "v" using {command down}
            end tell
        end if
    end if
end tell
| improve this answer | |
1

I've marked the above answer as correct even though it did not solve my problem because there is a lot of other useful code.

My unsolved issues: 1. modifier keys are still interfering even with CMD-c trick 2. Clipboard trick alters the clipboard. I could set it back to original value but that changes the history.

My solution: Don't run the script till any modifier keys are pressed. It's also kind of a trick but it solves the root-cause.

Here is my code:

- (void) cmd_panel_curItem_sendPathToTerminal;
{
    if (self.noKeyModifiersTimer != nil) {
        return;
    }
    self.noKeyModifiersTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(_cmd_panel_curItem_sendPathToTerminal) userInfo:nil repeats:YES];
}

- (void) _cmd_panel_curItem_sendPathToTerminal;
{
    NSUInteger modifiers = ([NSEvent modifierFlags] & NSEventModifierFlagDeviceIndependentFlagsMask);

    if ((modifiers & NSEventModifierFlagOption) == 0 && (modifiers & NSEventModifierFlagShift) == 0 && (modifiers & NSEventModifierFlagCommand) == 0 && (modifiers & NSEventModifierFlagControl) == 0) {
        [self.noKeyModifiersTimer invalidate];
        self.noKeyModifiersTimer = nil;
        [self impl_panel_curItem_sendPathToTerminal];
    }
}

- (void) impl_panel_curItem_sendPathToTerminal;
{
    // orig code
}
| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .