christian_alares Posted June 30, 2017 Share Posted June 30, 2017 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
vitor Posted June 30, 2017 Share Posted June 30, 2017 Hyper is an Electron app. Hence it has no support for AppleScript, hence this cannot be made to work properly. You can do it, but you’ll basically be telling AppleScript to fake keypresses, which is error prone and should really be done as a last resort. Link to comment
GuiB Posted June 30, 2017 Share Posted June 30, 2017 (edited) 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 June 30, 2017 by GuiB edit script vitor 1 Link to comment
GuiB Posted June 30, 2017 Share Posted June 30, 2017 (edited) @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 June 30, 2017 by GuiB Link to comment
GuiB Posted June 30, 2017 Share Posted June 30, 2017 (edited) 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 July 1, 2017 by GuiB Christin White 1 Link to comment
christian_alares Posted July 3, 2017 Author Share Posted July 3, 2017 (edited) Wow! Haven't checked in for a while. Looks awesome, I'll try this as soon as a I get my computer EDIT: @GuiB works like a charm! Thank you! Edited July 3, 2017 by christian_alares Link to comment
Christin White Posted June 17, 2018 Share Posted June 17, 2018 @GuiBJust wanted to say think for this, I've been really wanting Hyper integration with Alfred for a while now and this is great! Link to comment
gjuchault Posted August 2, 2018 Share Posted August 2, 2018 Hello, As this script never worked for me, I created a small Hyper plugin called hyperalfred that does the job using a text file. :) Link to comment
Takaya Posted May 10, 2020 Share Posted May 10, 2020 (edited) 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 May 10, 2020 by vitor Added translation Link to comment
vitor Posted May 10, 2020 Share Posted May 10, 2020 Welcome @Takaya, this is an English-speaking forum, so please make your post in English. I’ve added an automated translation to your post. Link to comment
abunav Posted September 5, 2020 Share Posted September 5, 2020 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
deanishe Posted September 5, 2020 Share Posted September 5, 2020 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
pseudometa Posted September 5, 2021 Share Posted September 5, 2021 (edited) 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 September 5, 2021 by armenaton Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now