Jump to content

Recommended Posts

Custom Terminal AppleScripts to Integrate iTerm and Alfred

 

iTerm version 3 beta:

The iTerm 3 beta version is the same version we have had for quite some time, which is also known as 2.9. Therefore the v2.9 branch of the script will work just fine with the v3 (aka 2.9) beta and v3 when it is released (any major changes to iTerm's applescript not withstanding).

 

In a blog post released recently (19th August 2015) Vero (Alfred's Mum ;)  ) has announced that as of the next version of Alfred, these custom terminal scripts will be the available mechanism for integrating Alfred and iTerm:

 

"With the upcoming release of Alfred 2.7.2, the default iTerm integration has been replaced by the "Custom" scripts option. This allows for a more up-to-date and more flexible way to handle the iTerm integration, using scripts created by one of our fantastic users, Stuart Ryan."

 

I would like to thank Vítor for helping to clean up the applescripts and submitting some great pull requests :).

 

There are two versions of the script available:

Each of these can be inserted into Alfred by following these steps:

  1. Load the Alfred control panel
  2. Click the Features Button/Tab
  3. Click on the "Terminal/Shell" menu on the left hand side
  4. Select "Custom" under application, and replace the text in the new box that appears with the code as per the version of iTerm you are running.

Alternatively, check out the official YouTube video on how to set them up:

integrate_iterm_alfredapp_custom_termina

 

iTerm 2.1 Branch Custom Terminal Script Features:

The 2.1 branch script is very basic and just corrects the opening of iTerm and creating a new session properly.

 

Change Log:

  • V0.4 Release - Vítor's submitted pull request for simplifying the check for if iTerm is already running. - December 14, 2015 (apologies for the delay in merging)
  • V0.3 Release - Added version number and Vítor has done some great work standardising the scripts. No functionality changes in this release - August 14, 2015 
  • Remove Trailing Whitespace (Vítor Galvão) - August 10, 2015
  • Initial Release - July 31, 2015

 

 

iTerm 2.9 Branch Custom Terminal Script Features:

 

The 2.9 Branch goes a bit further to utilise some of the new features of iTerm 2.9 (which is in beta at the moment and can be downloaded if you are feeling adventurous from the iTerm download page). The script has been designed to achieve the following:

  • Open the correct iTerm binary under /Applications/iTerm.app regardless of if another iterm/iTerm binary exists on the system
  • Always create a new tab/session, unless iTerm is not running, in which case use the default session.
  • If the user has selected to not create a default session on iTerm startup, detect and create a default session.

Change Log

  • V0.6 Release - Vítor's submitted pull request for simplifying the check for if iTerm is already running. - December 14, 2015 (apologies for the delay in merging)
  • V0.5 Release - Added version number and Vítor has done some great work standardising the scripts. No functionality changes in this release - August 14, 2015 
  • Removed bug introduced in last update (quotes around parameter) - August 12, 2015
  • Fix Whitespace (Vítor Galvão) + Fixed Search for iTerm/iTerm 2 Running (Stuart Ryan) - August 10, 2015
  • Updated to Fix Multiple iTerm Binaries - August 6, 2015
  • Initial Release - July 31, 2015

 

Donations

If you love the scripts, and get use out of them every day, if you would like to donate as a thank you to buy me more caffeine giving Diet Coke, some Cake, or to put towards a shiny new gadget you can donate to me via Paypal.

 

donate.png

 

 

Future Updates:

As new versions come out of the 2.9 branch I will keep the scripts up to date, if anyone finds issues with the 2.1 branch please let me know and I will test things out. If there are any changes to the scripts, I will post an update in this thread to let everyone know.

 

If you would like to look at/contribute to the official repository please check it out on Github --> https://github.com/stuartcryan/custom-iterm-applescripts-for-alfred.

 

As always, I welcome ideas/thoughts/feedback etc.

Cheers,
Stuart
Edited by stuartcryan
Link to comment

Hi All,

Minor update to the 2.9 branch to remove a bug introduced in the last update (my bad), I forgot to remove quotes around one of the parameters.

 

This had the effect that when iTerm was already running, you would get a 'q' rather than the command you opted to run.

Cheers,
Stuart
Link to comment

Hi All,

Some minor modifications to the scripts, Vítor has done some great work standardising the scripts and a version number has now been added for ease of future tracking.

 

There are no functionality changes in this release, only code cleanup and some additional comments.

Cheers,
Stuart
Link to comment

Howdy all :)

In case you don't follow the Alfred blog (which if you don't you are missing out  :D ), the AlfredApp team have today announced that:

"With the upcoming release of Alfred 2.7.2, the default iTerm integration has been replaced by the "Custom" scripts option. This allows for a more up-to-date and more flexible way to handle the iTerm integration, using scripts created by one of our fantastic users, Stuart Ryan."

 

So, I will be on hand to give as much support as I can during the transition, I am hoping there won't be too many issues as I know there are quite a few users out there already using the custom terminal scripts. You can switch over to the custom terminal scripts now and you will have a seamless transition when Alfred 2.7.2 comes out.

If you have any questions about the scripts please do not hesitate to holler.

Cheers,
Stuart
Link to comment

Hello!

 very nice script!

 

I had a small issue though, this is the timeline of the events:

 

- iterm 2.1.1 is running (with my usual tons of tabs)

- upgraded Alfred to the new version

- configured Alfred to use your custom script

> at this point whenever I type a command (e.g: ls), iTerm opens a new tab but nothing is actually executed

 

Stopping iTerm and launching it again made everything work again.

 

 

Link to comment

Hello!

 very nice script!

 

I had a small issue though, this is the timeline of the events:

 

- iterm 2.1.1 is running (with my usual tons of tabs)

- upgraded Alfred to the new version

- configured Alfred to use your custom script

> at this point whenever I type a command (e.g: ls), iTerm opens a new tab but nothing is actually executed

 

Stopping iTerm and launching it again made everything work again.

OOOH that is a conundrum. Has it all been working OK since then (or you need to manually open iTerm each time for it to work properly?)

 

Also when you say tons of tabs, may I ask how many on average (as I did not test the script with more than about five so might need to test more).

 

After I have the extra info I will have a deeper squiz for you (and thank you for reporting the issue).

Cheers,
Stuart
Link to comment

No, since then it is working perfectly. Only the first time did not work correctly.

 

I think I had about 15 opened tabs at the time. 

 

I tried just now to open tons of them and the problem also did not reappear.

 

 

 

OOOH that is a conundrum. Has it all been working OK since then (or you need to manually open iTerm each time for it to work properly?)

 

Also when you say tons of tabs, may I ask how many on average (as I did not test the script with more than about five so might need to test more).

 

After I have the extra info I will have a deeper squiz for you (and thank you for reporting the issue).

Cheers,
Stuart

 

Link to comment

No, since then it is working perfectly. Only the first time did not work correctly.

 

I think I had about 15 opened tabs at the time. 

 

I tried just now to open tons of them and the problem also did not reappear.

Awesome good to know, I will keep an eye on it, may have just been a glitch in the matrix but if I get another report I will take a closer squiz.

Cheers,
Stuart
Link to comment

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 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.

 

Thanks!

Link to comment

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 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.

 

Thanks!

Hi Maddog,

Can I please ask, what version of iTerm are you running? I will have to have a look as that may require some additional logic (I will have to play with the feature as I have not used it before).

 

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).

Cheers,
Stuart
Link to comment

@stuartcryan -

 

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

 

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.

 

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

 

Thanks.

Edited by Maddog
Link to comment

@stuartcryan -

 

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

 

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.

 

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

 

Thanks.

Good to know on the version I will have a squiz and see what I can come up with.

 

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.

 

And that is strangeeeee I might have a play and see if I need to ask for some extra applescript additions in iTerm to help get around it even in the long term.

Cheers,
Stuart
Link to comment
  • 3 weeks later...

@stuartcryan -

 

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

 

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.

 

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

 

Thanks.

Howdy my good man,

Any chance you could do a screen recording of the issue (and upload to youtube as private? or alternatively I can set up an SFTP location for you).

 

Just want to get a better idea of how it all works as I haven't used the quake feature before.

Cheers,
Stuart
Link to comment

The actual problem is a bug with iterm (described below in more detail), so I don't really think you can fix it from outside. But FWIW, here's how I setup the "quake window" feature.

 

KCmG8Qd.png

 

I have a special profile called "hotkey window" which is configured in the bottom left corner of the screenshot.

In the profile setting, it's configured to show up at "top of screen".

 

pi651qU.png

 

That's the entire story I use the ~ (tilda) button to show/hide it.

 

The thing is, it's a little buggy (I opened a bug in iterm), when I toggle it off with the hotkey, it's impossible to use cmd+tab to get back to the window. A similar problem happens here, where if it's hidden with the hotkey, it probably cannot be detected so it opens a new iterm window.

 

Note that when it happened to me, I only had the "hotkey window" open, no other iterm windows.

Link to comment

The actual problem is a bug with iterm (described below in more detail), so I don't really think you can fix it from outside. But FWIW, here's how I setup the "quake window" feature.

...

That's the entire story I use the ~ (tilda) button to show/hide it.

 

The thing is, it's a little buggy (I opened a bug in iterm), when I toggle it off with the hotkey, it's impossible to use cmd+tab to get back to the window. A similar problem happens here, where if it's hidden with the hotkey, it probably cannot be detected so it opens a new iterm window.

 

Note that when it happened to me, I only had the "hotkey window" open, no other iterm windows.

Boo that is a shame, something I won't be able to fix for the time being. Hopefully they can resolve that soon in the iTerm binary.

 

If there are any changes as a result after the fix I can look at making those in the AppleScript.

Cheers,
Stuart
Link to comment
  • 2 months later...

Howdy all,

Vitor has done some awesome work and closed out a couple of tasks, I apologise for the delay in merging them but I have been unwell.

 

All back to normal now and new version of the scripts have been released, so I suggest you get your hands on the latest version from:

Thank you Vitor for your efforts, you rule!
Cheers,
Stuart
Link to comment
  • 2 months later...

Howdy everyone,

Just to clarify for you (as a few people have asked, and I myself had a bit of confusion surrounding this).

 

The iTerm 3 beta version is the same version we have had for quite some time, which is also known as 2.9. Therefore the v2.9 branch of the script will work just fine with the v3 (aka 2.9) beta and v3 when it is released (any major changes to iTerm's applescript not withstanding).

 

As always if you have any issues please log a request over at Github.

 

I am hoping to get some time to work on these scripts soon.

Cheers,
Stuart
Link to comment
  • 10 months later...
  • 3 months later...
  • 1 year later...
  • 1 year later...
  • 1 year later...
  • vitor changed the title to Integrate iTerm with Alfred
  • 1 year later...

@vitor How do I get Alfred to open Warp terminal?

 

 

Why isn't Alfred opening Warp?

 

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

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

-- Handlers
on new_window()
	tell application "Warp" to create window with default profile
@@ -36,8 +39,10 @@ on alfred_script(query)
	if has_windows() then
		if open_in_new_window then
			new_window()
		else
		else if open_in_new_tab then
			new_tab()
		else
			-- Reuse current tab
		end if
	else
		-- If Warp is not running and we tell it to create a new window, we get two
		-- One from opening the application, and the other from the command
		if is_running() then
			new_window()
		else
			call_forward()
		end if
	end if
	-- Make sure a window exists before we continue, or the write may fail
	repeat until has_windows()
		delay 0.01
	end repeat
	send_text(query)
	call_forward()
end alfred_script

 

Edited by GatorMapi
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...