volna
volna

Reputation: 2610

Hide terminal window using applescript

The purpose of the current snippet is to run a script in a new terminal window and instantly hide it. The code below initially seems to work fine but as a result, if the window is manipulated using its visible property it simply disappears and doesn't seem to be executing. Right clicking on the Terminal app within the Dock displays like there is no terminal window at all.

tell application "Terminal"

    -- New Terminal Window
    set newTab to do script "caffeinate -u -t 900"
    set caffeinateWindow to id of front window

    tell window id caffeinateWindow
        set index to 1
        set visible to false
    end tell

end tell

Upvotes: 0

Views: 3543

Answers (1)

Pat_Morita
Pat_Morita

Reputation: 3535

instead of telling the terminal use applesripts builtin possibilities:

do shell script "caffeinate -u -t 900"

if you ever want to pass parameters then do it like this:

set param to "900"
do shell script "caffeinate -u -t " & param

Note if a parameter might contain spaces you need to escape/quote it like this:

set param to "900"
do shell script "caffeinate -u -t " & quoted form of param

if you still need to hide a window do it like so:

tell application "System events"
    try 
        set visible of application process  "Terminal" to false
    end try
end

Upvotes: 5

Related Questions