Skip to main content
added 155 characters in body
Source Link
wch1zpink
  • 7.8k
  • 18
  • 37
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
tell application "Terminal"
    activate
    repeat until frontmost
        delay 0.5
    end repeat
    set windowCount to count of window
    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 {}) 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
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
Source Link
wch1zpink
  • 7.8k
  • 18
  • 37

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 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 {}) 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