Jump to content

Integrate Warp terminal with Alfred


Recommended Posts

I hunted around for how to do this and the only thing I could find was how to do it with iTerm. There is some talk about how to do it with Warp but the consensus was that you just can't because Warp "has no AppleScript support".

 

There is actually a way to do it. (Though this is a workaround because you can't use AppleScript to script Warp)

I found this on GitHub; most of the code is unnecessary unless you want to customize tab and window behavior.

 

Here's a simplified version I made. GitHub gist here.

 

on alfred_script(q)
	tell application "System Events"

		tell application "Warp" to activate

		set numWin to count (windows of process "Warp")
		if numWin < 1 then keystroke "n" using command down

		delay 0.5
		keystroke q
		keystroke return

	end tell
end alfred_script

 

About the delay...

If Warp is already running, you don't need the delay. If it's not already running, the minimum delay for it to work is 0.3 (at least on my machine).

I don't know how to check if an app is running using AppleScript, but if I did, I would add an if statement before the activation to set the delay only if the app isn't running yet.

If anyone knows how to do that we can save 0.3 precious seconds.

Edited by Oli.
Link to comment

Welcome @Oli.,

 

6 hours ago, Oli. said:

the minimum delay for it to work is 0.3 (at least on my machine).

 

That is exactly why the consensus is that there is no proper way to do it. What you’re doing is called GUI automation and is very error prone. It is the equivalent of automating a web browser to click certain parts of the interface instead of using an API. You can certainly get it to work, but it is finicky.


For example, when you run that and your machine is under a bit more load, the delay will no longer work. Worse, you can end up sending your typing to the wrong application and pressing ↩. It may even be that part of your command is sent and executed, which can have bad results. Furthermore, you can’t rely on it working correctly on another’s machine. Finally, AppleScript is unreliable when sending keystrokes, e.g. with character that would necessitate a ⇧.


The receiving app (Warp) needs to add automation capabilities for it to work properly. Like iTerm2 does.

 

6 hours ago, Oli. said:

I don't know how to check if an app is running using AppleScript

 

if application "Warp" is running then

-- DO THINGS HERE

end if

Link to comment
38 minutes ago, vitor said:

That is exactly why the consensus is that there is no proper way to do it. What you’re doing is called GUI automation and is very error prone. It is the equivalent of automating a web browser to click certain parts of the interface instead of using an API. You can certainly get it to work, but it is finicky.

 

Thank you for the info! Sending keystrokes like that does seem a little hacky and your browser metaphor is dead on. Maybe Warp will add support for this at some point.

 

I'm not sure how it would be possible for the keystrokes to be sent to the wrong application when it's within the 'tell process "Warp"' block. I'm (obviously) not super familiar with AppleScript, so I'll just trust you on this one.

 

For now this seems to be working and, given I'll only ever be using this for very simple commands, I'm not super worried.

 

38 minutes ago, vitor said:

if application "Warp" is running then

Thank you for this. I'm sure if I use Alfred for 10 years straight those 0.3 seconds saved will add up to the time I spent working on this.

Link to comment
18 hours ago, Oli. said:

'm not sure how it would be possible for the keystrokes to be sent to the wrong application when it's within the 'tell process "Warp"' block.

 

What happens is that the process is telling macOS to type the characters. In other words, the block doesn’t mean “write these characters into this process’s window” it means “type this keys”.


So if Warp has a setting to open without windows, or the window takes to long to come to the front, or another app steals the focus right after, the characters will still be pressed but affect whatever else is in focus.

Link to comment
8 hours ago, vitor said:

What happens is that the process is telling macOS to type the characters. In other words, the block doesn’t mean “write these characters into this process’s window” it means “type this keys”.

 

Ah ok. So does that mean that that block doesn't actually do anything? Like the same thing would happen if you only wrapped it in - tell application "System Events"?

Edited by Oli.
Link to comment
  • 3 months later...
1 hour ago, vitor said:

Alfred’s Terminal support can take arbitrary AppleScript. That’s how the iTerm support works. You can open arbitrary URLs using open location in AppleScript.

 

Yes, that’s what I thought. However, if I got this thread right, their URI scheme isn’t enough to have a reliable integration since we don’t have an option to send text as we can do in iTerm via the write command. Am I correct?

Edited by xilopaint
Link to comment
  • 1 month later...
  • 4 months later...

Hi @V2arK - Thanks a ton for sharing your Warp workflow! I really appreciate it. The workflow is great, and I'm looking forward to using it more!!

 

If you update it down the road, one minor recommendation: It'd be great if the workflow would create the hidden directory for users that don't already have it. When I first tried to use the workflow, I couldn't get it to work for that reason. Apparently, Warp doesn't create the directory unless the user has already created a launch config file in the past. It was easy enough to fix (e.g., I just created a temp one through the file menu). But others may run into this problem if they haven't already created launch config files, too.

 

Thanks again for sharing the workflow!!

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