Skip to content

Instantly share code, notes, and snippets.

@mkows
Forked from gnachman/iterm.scpt
Last active June 2, 2016 10:47
Show Gist options
  • Select an option

  • Save mkows/bf0d0c9f6ef5532fda8b3d9db2a9802c to your computer and use it in GitHub Desktop.

Select an option

Save mkows/bf0d0c9f6ef5532fda8b3d9db2a9802c to your computer and use it in GitHub Desktop.

Revisions

  1. @Mkowaliszyn Mkowaliszyn revised this gist Jun 2, 2016. 1 changed file with 22 additions and 53 deletions.
    75 changes: 22 additions & 53 deletions iterm.scpt
    Original file line number Diff line number Diff line change
    @@ -1,55 +1,24 @@
    on write_to_file(this_data, target_file, append_data)
    try
    set the target_file to the target_file as string
    set the open_target_file to open for access file target_file with write permission
    if append_data is false then set eof of the open_target_file to 0
    write this_data to the open_target_file starting at eof
    close access the open_target_file
    return true
    on error
    try
    close access file target_file
    end try
    return false
    end try
    end write_to_file
    -- /Applications/Docker/Docker\ Quickstart\ Terminal.app/Contents/Resources/Scripts/iterm.scpt
    set itermRunning to (application "iTerm" is running)
    set scriptPath to quoted form of POSIX path of ((path to me as text) & "::" & "start.sh")
    set user_shell to do shell script "dscl /Search -read /Users/$USER UserShell | awk '{print $2}'"

    tell application "iTerm"
    activate
    if not (exists window 1) or (itermRunning = false) then
    reopen
    end if

    on alfred_script(q)

    -- Write the command to run to a file. This is done because Applescript quoting is impossible to get right, esp. for backslashes.
    write_to_file(q & return, POSIX file "/tmp/alfredscript", false)

    -- Create this file, which prevents iTerm2 from restoring a saved window arrangement.
    do shell script "touch ~/Library/Application' Support/iTerm/quiet'"

    -- Test cases:
    -- 1. iTerm2 running, has windows open. Should open a new window for Alfred command.
    -- 2. iTerm2 running, no windows open. Should open a new window for Alfred command.
    -- 3. iTerm2 not running, set to restores arrangement. Should not restore arrangement but open a new window for the Afred command.
    -- 4. iTerm2 not running. No windows to restore. Should open a single window for the Alfred command.
    -- 5. iTerm2 not running. Has windows to restore. Restores windows and then opens a new window for the Alfred command.

    -- Compose a script. This is necessary because compiling in a 'tell application' command causes the app to be launched, which would happen prior to the creation of the quiet file.
    set theScript to "
    tell application \"iTerm.app\"
    if (exists current window) then
    tell current window to create tab with default profile
    tell current session of current window
    write contents of file \"/tmp/alfredscript\"
    end tell
    else
    create window with default profile
    tell current session of current window
    write contents of file \"/tmp/alfredscript\"
    end tell
    end if
    activate
    end tell"

    -- Invoke the script.
    run script theScript

    -- Clean up
    do shell script "rm -f ~/Library/Application' Support/iTerm/quiet' /tmp/alfredscript"
    end alfred_script
    try
    tell current window
    set newTab to (create tab with default profile)
    tell current session of newTab
    write text "bash --login " & scriptPath
    end tell
    end tell
    on error
    tell current session of (create window with default profile)
    write text "bash --login " & scriptPath
    end tell
    end try
    end tell
  2. @gnachman gnachman revised this gist Feb 28, 2016. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion iterm.scpt
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,7 @@ on alfred_script(q)
    set theScript to "
    tell application \"iTerm.app\"
    if (exists current window) then
    tell current window to create tab with default profile
    tell current window to create tab with default profile
    tell current session of current window
    write contents of file \"/tmp/alfredscript\"
    end tell
  3. @gnachman gnachman revised this gist Feb 28, 2016. 1 changed file with 51 additions and 20 deletions.
    71 changes: 51 additions & 20 deletions iterm.scpt
    Original file line number Diff line number Diff line change
    @@ -1,24 +1,55 @@
    -- Copy this over "/Applications/Docker/Docker Quickstart Terminal.app/Contents/Resources/Scripts/iterm.scpt"
    set itermRunning to (application "iTerm" is running)
    set scriptPath to quoted form of POSIX path of ((path to me as text) & "::" & "start.sh")
    set user_shell to do shell script "dscl /Search -read /Users/$USER UserShell | awk '{print $2}'"
    on write_to_file(this_data, target_file, append_data)
    try
    set the target_file to the target_file as string
    set the open_target_file to open for access file target_file with write permission
    if append_data is false then set eof of the open_target_file to 0
    write this_data to the open_target_file starting at eof
    close access the open_target_file
    return true
    on error
    try
    close access file target_file
    end try
    return false
    end try
    end write_to_file

    tell application "iTerm"
    activate
    if not (exists window 1) or (itermRunning = false) then
    reopen
    end if

    on alfred_script(q)

    try
    tell current window
    set newTab to (create tab with default profile)
    tell current session of newTab
    write text "bash --login " & scriptPath
    end tell
    -- Write the command to run to a file. This is done because Applescript quoting is impossible to get right, esp. for backslashes.
    write_to_file(q & return, POSIX file "/tmp/alfredscript", false)

    -- Create this file, which prevents iTerm2 from restoring a saved window arrangement.
    do shell script "touch ~/Library/Application' Support/iTerm/quiet'"

    -- Test cases:
    -- 1. iTerm2 running, has windows open. Should open a new window for Alfred command.
    -- 2. iTerm2 running, no windows open. Should open a new window for Alfred command.
    -- 3. iTerm2 not running, set to restores arrangement. Should not restore arrangement but open a new window for the Afred command.
    -- 4. iTerm2 not running. No windows to restore. Should open a single window for the Alfred command.
    -- 5. iTerm2 not running. Has windows to restore. Restores windows and then opens a new window for the Alfred command.

    -- Compose a script. This is necessary because compiling in a 'tell application' command causes the app to be launched, which would happen prior to the creation of the quiet file.
    set theScript to "
    tell application \"iTerm.app\"
    if (exists current window) then
    tell current window to create tab with default profile
    tell current session of current window
    write contents of file \"/tmp/alfredscript\"
    end tell
    on error
    tell current session of (create window with default profile)
    write text "bash --login " & scriptPath
    else
    create window with default profile
    tell current session of current window
    write contents of file \"/tmp/alfredscript\"
    end tell
    end try
    end tell
    end if
    activate
    end tell"

    -- Invoke the script.
    run script theScript

    -- Clean up
    do shell script "rm -f ~/Library/Application' Support/iTerm/quiet' /tmp/alfredscript"
    end alfred_script
  4. @gnachman gnachman created this gist Feb 28, 2016.
    24 changes: 24 additions & 0 deletions iterm.scpt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,24 @@
    -- Copy this over "/Applications/Docker/Docker Quickstart Terminal.app/Contents/Resources/Scripts/iterm.scpt"
    set itermRunning to (application "iTerm" is running)
    set scriptPath to quoted form of POSIX path of ((path to me as text) & "::" & "start.sh")
    set user_shell to do shell script "dscl /Search -read /Users/$USER UserShell | awk '{print $2}'"

    tell application "iTerm"
    activate
    if not (exists window 1) or (itermRunning = false) then
    reopen
    end if

    try
    tell current window
    set newTab to (create tab with default profile)
    tell current session of newTab
    write text "bash --login " & scriptPath
    end tell
    end tell
    on error
    tell current session of (create window with default profile)
    write text "bash --login " & scriptPath
    end tell
    end try
    end tell