GithubHelp home page GithubHelp logo

vitorgalvao / custom-alfred-iterm-scripts Goto Github PK

View Code? Open in Web Editor NEW
3.7K 85.0 453.0 36 KB

AppleScript for iTerm2 Alfred integration.

License: BSD 3-Clause "New" or "Revised" License

AppleScript 100.00%
iterm alfred

custom-alfred-iterm-scripts's Introduction

Custom Alfred iTerm Scripts

AppleScript for iTerm2 Alfred integration.

Use

  1. Copy the script to your clipboard (see below).
  2. Open Alfred Preferences (call Alfred and press ,).
  3. Navigate to Features → Terminal → Custom.
  4. Set Application to Custom.
  5. Select the text in the box.
  6. Paste.
  7. Optionally, change the script’s behaviour by changing the property lines at the top.

Copy the script

To copy the script to your clipboard, run the following command.

curl --silent 'https://raw.githubusercontent.com/vitorgalvao/custom-alfred-iterm-scripts/master/custom_iterm_script.applescript' | pbcopy

custom-alfred-iterm-scripts's People

Contributors

anonfunc avatar fabien-jrt avatar ialexryan avatar jhriv avatar luckman212 avatar richdonnellan avatar sineld avatar smammy avatar stuartcryan avatar vitorgalvao avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

custom-alfred-iterm-scripts's Issues

iTerm 2 3.3.9 Command is not sent

Hi,

I am using the latest version of script

on alfred_script(q)
	tell application "iTerm"
		if application "iTerm" is running then
			try
				tell the first window to create tab with default profile
			on error
				create window with default profile
			end try
		end if

		delay 0.1 -- If we do not wait, the command may fail to send

		tell the first window to tell current session to write text q
		activate
	end tell
end alfred_script

but I've realised when the iTerm is not running, command is not sent. It helps even if there is no window and application is running but if you quit with cmd+q and try, script doesn't send command, just opens iTerm window.

open action Terminal command in new tab

Hi,

I have couple of scripts that I want to be run one after one. I made a workflow to do so but the problem is that happened n the same tab. How can I make alfred to open a new tab every time?

Example:

1- cd /dir1/
1.1- npm install

2- cd /dir2/
2.1 mvn clean install

3 cd /dir3/
3.1 run kafka

Doesn't work with symlinked iTerm.app

This doesn't seem to work with iTerm not installed at the default location. I'm not positive this is the reason, but taking a quick look through the 2.1.1 script and it's my first assumption (any logs or output I can check to verify?). I have installed iTerm 2 with Cask and this doesn't seem to work with your lastpass workflow. Is there a way to "find" an application before trying to send it commands, or at least use a symlink in the Applications folder to find it?

Command not always sent to iTerm in Catalina (with fix)

Alfred 5.0.1 [2067]
iTerm2 3.4.16
macOS Monterey 12.4

Note

First I'll start with this. On my M1 Mac with the specs above, it seems to be working as intended. But for the specs from my older Intel Mac (below) I had to make some adjustments to the script to get it to work properly. So this Issue is more for "if someone else is having this issue" and on Catalina, perhaps this code snippet will help them.

Alfred 5.0.1 [2067]
iTerm2 3.4.16
macOS Catalina 10.15.7 (19H1922)

Issue

Depending on the property settings open_in_new_window and/or open_in_new_tab set to true, the command was not being added to the console.

Fix

-- For the latest version:
-- https://github.com/vitorgalvao/custom-alfred-iterm-scripts

-- Set this property to true to always open in a new window
property open_in_new_window : true

-- Set this property to false to reuse current tab
property open_in_new_tab : true

-- Handlers
on new_window()
  tell application "iTerm" to create window with default profile
  activate
end new_window

on new_tab()
  tell application "iTerm" to tell the first window to create tab with default profile
  activate
end new_tab

on is_running()
  application "iTerm" is running
end is_running

on call_forward()
  tell application "iTerm" to activate
end call_forward

on send_text(custom_text)
  tell application "iTerm" to tell the first window to tell current session to write text custom_text
end send_text

-- Main
on alfred_script(query)
  -- Check to see if iTerm is running
  if is_running() then
    -- Open in new window
    if open_in_new_window then
      new_window()
    -- Open in new tab
    else if open_in_new_tab then
      new_tab()
    else
      -- Reuse current tab
    end if
  -- iTerm is not running, activate new instance
  else
    call_forward()
  end if

  -- Make sure a window exists before we continue, or the write may fail
  repeat until is_running()
    delay 0.01
  end repeat

  send_text(query)
end alfred_scripts

Terminal doesn't "write"

On my system (OSX 10.10.3 & iTerm 2.1.1) the new terminal opens but the text isn't inserted. When I replace the code tell mysession to write text q with tell the last session to write text q, it works. So it seems that mysession isn't being set correctly.

Feature/PR suggestion: add exit_on_run option

Hi Vitor, great script! I modified your code to add an exit_on_run property that will send "exit" to iTerm once it finishes running the query. Happy to send in a PR for this if you'd like.

Build 3.0.5 Always open new tab

Thanks for this awesome script!

I am using iTerm2 build 3.0.5 with custom_iterm_script_iterm_2.9.applescript, but alfred always open a new tab which I can't find a recent fix for this issue.

Is there any update on the script to set it to always run in current session? Sorry if I missed a fix in the forum or issue.

Many thanks!

Issues with "quake window" feature in iTerm 2.9+

(From http://www.alfredforum.com/topic/7138-custom-terminal-script-integrate-iterm-with-alfred-updated-august-19-2015/#entry38822) I'm using iTerm's "quake window" feature (I have a specific half-screen sized window,that drops when I hit the ~ (tilda) button). This opens a new tab in that profile, but doesn't activate the actual window.

It may be related to this bug I previously opened for iTerm, which is sort-of related to activating the "hotkey profile".

...
...
I'm on the bleeding edge of iTerm I think (Build 2.9.20150812-nightly)

Also, this is funny, but I'm actually experiencing different results whether I'm cmd+tabbing away from that hotkey window or if I'm hiding it with the hotkey itself (I get a new tab in a new window vs. new tab in the hotkey profile window). This actually is related to the iTerm bug I posted, haha

Enable behaviour selection of opening in new/existing tabs etc

(Form http://www.alfredforum.com/topic/7138-custom-terminal-script-integrate-iterm-with-alfred-updated-august-19-2015/#entry38822 ) I would also be interesting to be able to choose whether the script ( opens a new tab / reuses one tab it opened / opens a window / reuses one window it opened ). Don't know if that's feasible, but I would definitely choose a reused tab/window instead of a new one every time.

...
Me:
Also with regards to the new feature, that is possible but it would be on an all or nothing basis, i.e. it can't really be set at run time. So are you looking for a set one off to change how the script as a whole runs or are you looking for something you can change each time it runs depending on what behaviour you would like at that time (just wanting to clarify, as the latter is not really possible).

...
MD:
As for the new feature - would it be like a boolean you configure at the top of the script? Or actually a different script? I want to understand what "all or nothing" means, I don't mind if it's not configurable in a UI, but would love a way to be able to quickly change it in the custom script code.

...
Me:
I was actually thinking along the lines of a boolean in the script itself (rather than a separate script). So I think that would suit exactly what you are looking for.

Implement versioning of some sort

It would be great to have a version number inline with the scripts so people could see if they needed to update (rather than simply a release date).

Also tagging should be used on the repository.

Check whether current terminal window is busy

Hi there,
I just started with the iterm integration in Alfred but I'm struggling a bit with my workflow. I usually have a splitted screen of 3 or 4 terminal windows open all the time. So, I definetly prefer to post the command into one of these. However, it would be great, if Alfred could check whether the window is busy before. Specifically, open SSH sessions are usually windows that I don't want the commands ending up in.
Is there any chance that a functionality like this being realized in future?
Nils

Terminal Command in New Tab

I have several workflows that run terminal commands. I would expect the commands are run in a new tab, as in the official implementation before.

Since the official support was deprecated, quite often the desired command runs in an existing iTerm tab, and a new tab is created in the foreground. Particularly this happens whenever I have split panes open.

This is quite annoying not only because it disrupts my work in the current tab, but it also defeats the purpose why I choose to use a terminal command instead of "run script", as I want to check on the output (download progress, for example).

I am using iTerm2 2.1.1, and my default shell is Fish, if that is at all helpful.

iterm.app is often iterm 2.app in /Applications/

Thanks for this fantastic little script. Just a note that when I donwloaded the latest version of "iTerm3" (aka... 2.9) it defaults to "iTerm 2.app" and calls the app "iTerm 2". Your script points to "iTerm.app" and "iTerm". Easy fix when I noticed the problem, but didn't know if I should submit a PR to fix or if this was something unique to my desktop.

Again, thanks for the work. Really nice to have this.

2.1.1 version doesn't work..

After auto upgrading my alfred, and then getting the 2.1.1 script I can no longer access iterm from alfred.... tried redo doing three times, and restarting alfred and computer but still can't use Iterm from alfred..

Off topic: How are you finding iTerm 2.9?

I'm curious, how are you finding iTerm 2.9 so far? Not sure I want to try it yet but, I noticed you have the second AppleScript there so, thought I'd ask what your thoughts are so far?

Fails to bring iTerm window to front

Hey! I ran into a small issue where if my iTerm app isn't running and I issue any command, say whoami using > in alfred 4, the script launches the iTerm app, but the window app window never appears. It is there though. If I look at the window tab in the top panel or press option+cmd+1 it'll pop up. Unfortunately the command isn't being run.

System: macOS 11.6
iTerm: Build 3.4.10
Alfred: 4.6 [1266]

Needs to wait for unfinished script executions

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!

Running command results in no action

I'm running on macos ventura 13.2.1 (22D68) and just tried using the script but it results in no action taking place. Any ideas why that could be?

Command Appears Before Shell Loads

It might just be me - but it seems like when a command is called, the text is pasted before the shell loads, and then is pasted again after the shell loads. Is anyone else getting this behavior?
Screen Shot 2022-01-30 at 5 22 24 PM

Open in new tab

All commands run in current session (e.g. "open terminal here" from file serach). Is it intended behaviour? In most of cases, opening in new tab makes more sens for me. So, shouldn't onlywindow=False in this line?

Making it work with iterm2 3.3.9

After struggling to find a version of the applescript that works, I found one buried deep in the alfred forums, thought I'd share:

on alfred_script(q)
	if application "iTerm2" is running or application "iTerm" is running then
		run script "
			on run {q}
				tell application \"iTerm\"
					activate
					try
						select first window
						set nowindows to false
					on error
						create window with default profile
						select first window
						set nowindows to true
					end try
					tell the first window
						if nowindows is false then
							create tab with default profile
						end if
						tell current session to write text q
					end tell
				end tell
			end run
		" with parameters {q}
	else
		run script "
			on run {q}
				tell application \"iTerm\"
					activate
					try
						select first window
					on error
						create window with default profile
						select first window
					end try
					tell the first window
						tell current session to write text q
					end tell
				end tell
			end run
		" with parameters {q}
	end if
end alfred_script

text instructions >>>>>>>>>> video instructions

I think it would be quicker to have text instructions that detailed the installation process rather than a video. This may be a personal preference but I hate watching instruction videos, and I know a lot of developers (developers being a large part of the "target market" for this script) who feel the same way.

Doesn't work with iTerm 3.1beta5

Scripting doesn't appear to work properly with iTerm 3.1beta5.

I checked the AppleScript (everything within the alfredscript) and it still seems valid, I can't figure out why it isn't working, but iTerm is NOT launching or activating.

How about a default terminal one

Is there any possibility to make a default terminal one so that the new task can open in the new tab instead of the new window? Thanks!

Script Running Before iTerm2 Initialized

Weird behavior when I change the following two property settings:

-- Set this property to false to reuse current tab
property open_in_new_tab : false

-- Set this property to true if iTerm is configured to launch without opening a new window
property iterm_opens_quietly : true

The script is input into iTerm before the session is initialized and the script does not get a chance to run. This behavior seems to work when the previous setting are reversed.

Fix issue when sending long command

I tried to send some particularly long commands with Alfred, but iTerms always cut my command in half, making it impossible to execute properly. Later, I discovered the reason was that the call_forwrad() was being called before send_text() had completed, causing the issue. Reversing the order of these two resolved the problem. And, adding a delay between them can make it more reliable.

if has_windows() then
      call_forward()
      delay 0.5
      send_text(query)
      exit repeat
    end if

Short code does not work but copying and pasting apple script does

curl --silent 'https://raw.githubusercontent.com/vitorgalvao/custom-alfred-iterm-scripts/master/custom_iterm_script.applescript' | pbcopy

This does not do anything but when I copy and paste the contents of the AppleScript it works.
Any idea why that might be?

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.

Originally posted by @vitorgalvao in #25 (comment)

Filename rename suggestion

My iterm version is v3.2.0, custom_iterm_script_iterm_3.1.1.applescript works fine for me.

It should rename to custom_iterm_script_iterm_3.applescript, it will be better. The same as the other filename.

iTerm Opens Two Tabs

When I run a command in iTerm, two tabs open. The first one is just blank, then the second one is used to enter my command.

Its annoying because I disabled the notification when quitting when a single tab, but not when multiple tabs are open. So when I run a quick terminal command then quit, I always get a Quit iTerm2? confirmation. I wouldn't get this if only one tab was open.

Looks like its an iTerm issue thats been debated here, so sorry if you cant do anything http://www.alfredforum.com/topic/1180-extra-tab-is-open-when-using-iterm2/. Not sure if any script examples there can be used to prevent it in this script.

Also this post may be of interest to run new commands in the current window instead of opening a new tab every time http://www.alfredforum.com/topic/721-executing-iterm2-terminal-commands-in-current-shell/?hl=iterm

Add start project rellease

Hi!
Thank you for the script.
I would propose to make releases. I am using this project as a ghost, like probably, most of the Alfred users.
I love to track new releases and I noticed updates in this script only after reinstall (sic!).
How about making a standard release process and allow users to track releases?

Feature Request: Add Control functions

Was googling around and found Alfred2 Terminal Control (https://github.com/wkoffel/alfred-terminal-control) works with both terminal and iTerm. But since the change to setting iTerm support to Custom in Alfred it appears that the workflow is now broken. The main feature I am looking for is the ability to launch an iTerm Profile from alfred. From what I can tell terminal-control can read the profiles but is unable to launch them. This would be an awesome feature to add.

TIA
-Ron

Nothing happens

When I issue a command, eg ".ls" -- I get nothing. Is there some way to view logs or some such?

I am using alfred 3 and iterm 3

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.