Jump to content

Change default Terminal to Hyper.is ?


Recommended Posts

Hi!

I just discovered this terminal application: https://hyper.is/

I find it appealing and I would like to change the default Alfred behaviour to this app.

 

For example when I browse för files in Alfred and press "Open Terminal Here".

This seems to be editable in settings but I'm not sure what to write in the AppleScript section.

 

See this little video to understand more what I want to achieve:

 

Link to comment

As @vitor said, Hyper is not scriptable (there's no applescript dictionary to make it works reliably without keystrokes). However, as a workaround, Hyper is able to open folder and file and it would set it's path to that folder. So, here is what I would do to make it reliable and fast:

 

on alfred_script(q)
	do shell script "open -a Hyper " & text 4 thru -1 of q
end alfred_script

 

Just copy & paste this script in the custom script section in Features > Terminal / Shell of Alfred preferences and this should work

 

Hope this helps!

Edited by GuiB
edit script
Link to comment

@christian_alares, I just thought I should give a more complete script in case you want a script that work with Alfred prefix command to run a terminal command also. I mean, if you want to do something else than using the Open Terminal Here command. Here is a script that would work for both way of actioning the command:

 

on alfred_script(q)
	if (text 1 thru 2 of q) is equal to "cd" then
		do shell script "open -a Hyper " & text 4 thru -1 of q
	else
		do shell script "open -a Hyper ~"
		delay 1
		tell application "System Events" to keystroke q & return
	end if
end alfred_script

 

As @vitor said, this is not the best way since we need to insert a delay command for the keystrokes (the "delay 1" inside here), but seems to be quite great on my side (you may need to play with the delay time if there's a problem on your side). Also, I've use "open -a Hyper ~" in the second method so a new window is opened without using the keystroke method with delay. You may want to change the default path if you want (this would set you in your home directory, but change the "~" to something else if you prefer)

 

--- Update

I mention that this is not an optimal way, but I mean that mainly for the second method (when sending a command to Hyper). This is still speedy for the "Open Terminal Here" command and is the same as my first script above

Edited by GuiB
Link to comment

Here is an updated version

 

I wanted to test to find a way so the script wait that the application is ready instead of a definite time before sending the keystrokes. With this version it should be faster and less error prone. Best!

 

on alfred_script(q)
	if (text 1 thru 2 of q) is equal to "cd" then
		do shell script "open -a Hyper " & text 4 thru -1 of q
	else
		do shell script "open -a Hyper ~"
		set appOpen to false
		set nbrOfTry to 0
		delay 0.5
		repeat
			try
				tell application "System Events"
					if exists (window 1 of process "Hyper") then
						set appOpen to true
						exit repeat
					end if
				end tell
			end try
			set nbrOfTry to nbrOfTry + 1
			if nbrOfTry = 20 then exit repeat
			delay 0.5
		end repeat
		if appOpen then tell application "System Events" to keystroke q & return
	end if
end alfred_script

 

Edited by GuiB
Link to comment
  • 11 months later...
  • 1 month later...
  • 1 year later...

Automated translation:

 

I just passed by, but the post here saved me.
I would like to thank the person who asked the question, the person who participated, and the person who made it.
I really appreciate it! !

 

Original:

 

私は通り過ぎただけですが、ここの投稿は私を救いました。

質問してくれた人、参加してくれた人、そしてそれを作った人に感謝したい。

本当に感謝しております !!

Edited by vitor
Added translation
Link to comment
  • 3 months later...

This seems like an old thread, but none of the solutions above seem to be working. When i try @GuiB's solution, the Hyper app does open but it doesn't execute the command i type after ">" in Alfred. Same for the hyperalfred plugin. Any idea how i can fix this?

Link to comment

Try this instead:

on hyper_win()
	set _running to (application "Hyper" is running)
	tell application "Hyper" to activate
	tell application "System Events"
		log (name of first application process whose frontmost is true)
		repeat while (name of first application process whose frontmost is true) is not "Hyper"
			delay 0.05
		end repeat
		
		set _hyper to first application process whose frontmost is true
		-- If Hyper was running, create a new window to run command
		if _running then
			tell _hyper to set _target to (count windows) + 1
			keystroke "n" using {command down}
		else
			set _target to 1
		end if
		
		-- Wait for wanted window count
		tell _hyper
			repeat while (count windows) < _target
				delay 0.05
			end repeat
		end tell
	end tell
end hyper_win

on alfred_script(q)
	my hyper_win()
	tell application "System Events"
		keystroke q
		key code 36
	end tell
end alfred_script

 

Link to comment
  • 11 months later...

Well, most of the posted solution work for me, however they tend to be a bit buggy sometimes. As I soon figured out, the issue was 

keystroke q

when the query contains certain characters like tildes, or backticks, keystroking them one after the other results in wrong input. Simply trying to cd into your iCloud drive already fails, because of the tildes in the folder path.

 

So what I have done instead is to use the clipboard to paste the content instead – this seems to be more robust, with the drawback of "loosing" your current clipboard content, however. (I thought about temporarily storing your current clipboard and then switching it back, but it seems that this causes the script to fail with bigger clipboard contents like files.) I also changed it so a new tab instead of a new window is opened, which unfortunately prevents the identification of a new window being open which makes @deanishe's script so quick. Here is the result:

on hyper_new()
	set _running to (application "Hyper" is running)
	tell application "Hyper" to activate
	tell application "System Events"
		log (name of first application process whose frontmost is true)
		repeat while (name of first application process whose frontmost is true) is not "Hyper"
			delay 0.05
		end repeat

		set _hyper to first application process whose frontmost is true
		-- If Hyper was running, create a new tab to run command
		if _running then keystroke "t" using {command down}
		delay 0.25

	end tell
end hyper_new

on alfred_script(q)
	my hyper_new()
	set the clipboard to q
	tell application "System Events"
		keystroke "v" using {command down}
		delay 0.2
		key code 36
	end tell
end alfred_script

 

Edited by armenaton
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...