dfay Posted January 27, 2015 Share Posted January 27, 2015 (edited) A lot of Alfred workflows and commands have options when certain keys are pressed. These keystrokes can be sent from Alfred Remote with a bit of Applescript. e.g. to hold and release the Control key, add the following: Alfred Preferences > Remote > System Commands (or wherever you want on your remote) > + > Run Script > language: /usr/bin/osascript then paste in the following short Applescript: tell application "System Events" control key down end tell Name this item "Hold Ctrl" but BEFORE YOU TEST IT add a second command following the same procedure: tell application "System Events" control key up end tell Name this 2nd item "Release Ctrl". Now you can test it and make sure it works. If you test the first script before adding the second, you will end up with a practically unusable computer b/c the Control key will be down and you will have no way to release it. I found this out the hard way and had to reboot. IMPORTANT - this does not work with the Option key for reasons that are unclear. You can create an Applescript to hold the Option key down, and it will work, but the corresponding script to release does not work, and you'll be forced to reboot to get your keyboard working normally again. Edited January 31, 2015 by dfay Subject22 and rice.shawn 2 Link to comment
rice.shawn Posted January 27, 2015 Share Posted January 27, 2015 I found this out the hard way and had to reboot. I've done the same thing too many times to admit. Link to comment
Chakshu Posted January 27, 2015 Share Posted January 27, 2015 I'm just trying to figure out how to make Alfred Remote press a regular key on my mac (like "i", "m", "left arrow", "enter", "ESQ" etc.) I'm trying to build my own Plex remote in Alfred Remote but I'm not good att scripts Any advise? Thank you! /M Link to comment
heldausberlin Posted January 27, 2015 Share Posted January 27, 2015 I tried to do this for system volume. But it won't work. What is wrong with "key code 111"? Link to comment
Chakshu Posted January 27, 2015 Share Posted January 27, 2015 I tried to do this for system volume. But it won't work. What is wrong with "key code 111"? Hey! Another total beginner at scripts I suppose? I finally got THIS to work: tell application "System Events" to keystroke "k" So that is the menu button for PLEX: I'm of to try try up-arrow Link to comment
heldausberlin Posted January 27, 2015 Share Posted January 27, 2015 (edited) Hey! Another total beginner at scripts I suppose? I finally got THIS to work: tell application "System Events" to keystroke "k" So that is the menu button for PLEX: I'm of to try try up-arrow So "keystroke k" makes the system louder? Thanks a lot! Edited January 27, 2015 by heldausberlin Link to comment
rice.shawn Posted January 27, 2015 Share Posted January 27, 2015 If you want to access the volume, you might consider using AppleScript to adjust the volume directly rather than doing keystrokes. Here's a reference that you can use for that. heldausberlin 1 Link to comment
Chakshu Posted January 27, 2015 Share Posted January 27, 2015 Okey, I might be out of league here and I probably didn't understand what you meant. I googled a little bit and for volume up I got this to work: tell application "System Events" to key code "103" Remember to set the script in Alfred to osascript (whatever that is) Anyhow as a total beginner it was really fun when I just got "downarrow to work" with this script: tell application "System Events" to key code "125" What a feeling. I'm off building the rest of the remote now with The Google. So "keystroke k" makes the system louder? Thanks a lot! Link to comment
dfay Posted January 27, 2015 Author Share Posted January 27, 2015 Yeah, I'd second Shawn's sentiment - it's best to only use UI Scripting in Applescript when there is no other alternative. It can be fragile, esp. if users have customized their keyboard shortcuts at all. Link to comment
Chakshu Posted January 27, 2015 Share Posted January 27, 2015 I need help with a script that is a little bit to advanced for me... I need to send the command "CMD+SHIFT+LeftArrow" and hold it down for 25ms but Not longer. This is what I use to control the volume on my GLM audio system. (it has a 20ms minimum for safety) I tried to write it myself but it was to complicated for me? Could anyone help me out? ;/ Thanks a lot! /M Link to comment
rice.shawn Posted January 27, 2015 Share Posted January 27, 2015 I need help with a script that is a little bit to advanced for me... I need to send the command "CMD+SHIFT+LeftArrow" and hold it down for 25ms but Not longer. This is what I use to control the volume on my GLM audio system. (it has a 20ms minimum for safety) I tried to write it myself but it was to complicated for me? Could anyone help me out? ;/ Thanks a lot! /M What are you actually trying to do? What program are you trying to interact with? To do exactly what you want, then you do something like: tell application "System Events" --- this is where you put the key down commands delay 0.025 --- this is where you put the key up commands end tell However --- let me rephrase that: HOWEVER, AppleScript's delay command isn't very accurate as it was designed for seconds and not milliseconds, so you're likely not going to get the results that you want. Link to comment
Chakshu Posted January 28, 2015 Share Posted January 28, 2015 What are you actually trying to do? What program are you trying to interact with? To do exactly what you want, then you do something like: tell application "System Events" --- this is where you put the key down commands delay 0.025 --- this is where you put the key up commands end tell However --- let me rephrase that: HOWEVER, AppleScript's delay command isn't very accurate as it was designed for seconds and not milliseconds, so you're likely not going to get the results that you want. Thank you Shawn! I have an application called GLM that changes the volume for my Genelec active digital speakers. It is a completely digital system (No D/A) with the sound running over AES protocol via XLR cables and the volume commands is send to each speaker via CAT5. This means that I can no longer change the volume for my mac using the OS volume control (it is greyed out). I can only change the volume of the sound played on my mac with this hotkey (CMD+SHIFT+Left/rightArrow that tells the GLM software to send a signal via CAT5 to the speakers. The only problem is that if the keyboard shortcut is activated for to long the sound from the speakers will eventually reach 100db and if it is pressed for to short of a time the volume will change hardly at all. It works great with the keyboard (even over VNC) but som remotes can't send the shortcut enough times per second or sometimes it cannot keep sending it for the time it needs to send it. It works pretty good with HippoRemote but that app haven't been updated for over two years and is slowly falling apart when OS X is updated. I can set the volume cmd timer in GLM to 10-50 ms but I prefer it at 50 because it works best when using the keyboard. If I put it to 10ms the volume gets really loud really fast which can enjoy the neighbors if a friend is messing around with my sound not knowing how it works So if I understand correctly the command we could try would look like this? (replacing CMD etc. with the real codes for those keys) tell application "System Events" to key code "CMD+SHIFT+125" delay 0.025 to key code "CMD+SHIFT+125" end tell Then I would have to experiment (with no sound on, hehe) to find the sweet spot for milliseconds. A feature request to Alfred Remote would be to be able to send keystrokes that keeps being send while you have your tump pressed on the button on Alfred remote. So that's it... I know it sounds like a bit of a hazel but it the sound is good . Link to comment
heldausberlin Posted January 28, 2015 Share Posted January 28, 2015 Okay, guys. I guess I'm a noob with AppleScript. I tried to insert your codes into a Remote Workflow. Once as AppleScript and once as Script with "osascript". Nothing happens when I touch the button on the app. What goes wrong? Thanks for helping me. Link to comment
rice.shawn Posted January 28, 2015 Share Posted January 28, 2015 If you want to make this reliable, then you should not base this on time (again, because AppleScript does not understand milliseconds). More importantly, you should see if there is a way to interact with GLM directly rather than using Hotkeys that are setup. So, does GLM support AppleScript? If it provides AppleScript commands to set the volume, then you should definitely use those instead of trying to get the timing right with the delays because your results will be more accurate. I'm not sure that it does. The easiest way to tell would be to open Script Editor and then click File ->Open Dictionary. Then, if there is an entry for the GLM program, you'll see what commands are available. If that isn't there, then you can always try UI scripting (see this Stackoverflow answer: https://stackoverflow.com/questions/23847324/applescript-set-focus-to-a-slider-before-sending-key-presses-up-down). Otherwise, you can try the timing one, which should be the code above... mostly. But it doesn't look like AppleScript recognizes key down instructions for non-modifier keys (at least it doesn't seem to very well). So, the code might have to be more like tell application "System Events" key down shift key down command repeat 125 times keystroke (key code 123) end repeat key up shift key up command end tell So that code will press the left arrow 125 times while holding shift and command down. Link to comment
rice.shawn Posted January 28, 2015 Share Posted January 28, 2015 Okay, guys. I guess I'm a noob with AppleScript. I tried to insert your codes into a Remote Workflow. Once as AppleScript and once as Script with "osascript". Nothing happens when I touch the button on the app. What goes wrong? Thanks for helping me. Are you just trying to do the key strokes or are you putting them inside a code block that talks to "System Events"? Look here for more about "System Events": https://en.wikibooks.org/wiki/AppleScript_Programming/System_Events. Link to comment
dfay Posted January 28, 2015 Author Share Posted January 28, 2015 You'll have a much easier time debugging if you write the scripts in Script Editor, verify that they work there, and then move them into Alfred. Subject22 1 Link to comment
heldausberlin Posted January 28, 2015 Share Posted January 28, 2015 Are you just trying to do the key strokes or are you putting them inside a code block that talks to "System Events"? Look here for more about "System Events": https://en.wikibooks.org/wiki/AppleScript_Programming/System_Events. Thanks. I think I've mixed too much. If I want to use the "set volume …", is it to tell "System events" as well? Link to comment
Subject22 Posted January 28, 2015 Share Posted January 28, 2015 I found this out the hard way and had to reboot. Another good trick for this is to throw in a try block. Link to comment
rice.shawn Posted January 28, 2015 Share Posted January 28, 2015 Thanks. I think I've mixed too much. If I want to use the "set volume …", is it to tell "System events" as well? No. You shouldn't need to do so. Okay. Here is a script that I just wrote for you. on increase_volume(step_size_) set volume_ to output volume of (get volume settings) if volume_ > (100 - step_size_) then set volume output volume 100 else set volume output volume (volume_ + step_size_) end if if output muted of (get volume settings) is true then set volume without output muted end if end increase_volume on decrease_volume(step_size_) set volume_ to output volume of (get volume settings) if 0 > (volume_ - step_size_) then set volume output volume 0 set volume with output muted else set volume output volume (volume_ - step_size_) end if end decrease_volume on mute_volume() set volume with output muted end mute_volume on unmute_volume() set volume without output muted end unmute_volume That defines four functions (handlers): increase_volume, decrease_volume, mute_volume, and unmute_volume. Now, this is not complete because for each action, you need to declare what one you're doing. (I don't know you're writing this as a workflow or as simple run applescripts....). If you want to create a workflow with those actions, then use this script, and then use an argument to figure out which function to call. (Note: step_size_ is a variable which is how much you want to increase the volume. Min volume is 0, max volume is 100. If your step_size_ is 7, then it will increase by 7 each time. This can use integers only). If you want to just have a "run script" for each, then cannibalize the functions. In other words, create a "Volume Up" remote button that is a "Run Applescript". In that box, put in this: on alfred_script() set step_size_ to 7 set volume_ to output volume of (get volume settings) if volume_ > (100 - step_size_) then set volume output volume 100 else set volume output volume (volume_ + step_size_) end if if output muted of (get volume settings) is true then set volume without output muted end if end alfred_script (And adjust the step size to whatever you want). Do the same thing for Decrease Volume, etc... voidPortal 1 Link to comment
heldausberlin Posted January 28, 2015 Share Posted January 28, 2015 O thanks! Now I did it like this (I didn't know it needs just the code without any "alfred_script" or sth else). for mute Button: set volume with output muted for plus Button: set volume output volume (output volume of (get volume settings) + 5) --100% for minus Button: set volume output volume (output volume of (get volume settings) - 5) --100% Link to comment
heldausberlin Posted January 28, 2015 Share Posted January 28, 2015 Okay, thanks for your better script. I've optimized the mute function by this code: if output muted of (get volume settings) is true then set volume without output muted else set volume with output muted end if Link to comment
altryne Posted January 28, 2015 Share Posted January 28, 2015 I guess great minds think the same, I wrote a remote workflow and submitted to packal (waiting to get approved) that will control the volume on your mac http://www.packal.org/workflow/remote-volume-control heldausberlin 1 Link to comment
Chakshu Posted January 28, 2015 Share Posted January 28, 2015 If you want to make this reliable, then you should not base this on time (again, because AppleScript does not understand milliseconds). More importantly, you should see if there is a way to interact with GLM directly rather than using Hotkeys that are setup. So, does GLM support AppleScript? If it provides AppleScript commands to set the volume, then you should definitely use those instead of trying to get the timing right with the delays because your results will be more accurate. I'm not sure that it does. The easiest way to tell would be to open Script Editor and then click File ->Open Dictionary. Then, if there is an entry for the GLM program, you'll see what commands are available. If that isn't there, then you can always try UI scripting (see this Stackoverflow answer: https://stackoverflow.com/questions/23847324/applescript-set-focus-to-a-slider-before-sending-key-presses-up-down). Otherwise, you can try the timing one, which should be the code above... mostly. But it doesn't look like AppleScript recognizes key down instructions for non-modifier keys (at least it doesn't seem to very well). So, the code might have to be more like tell application "System Events" key down shift key down command repeat 125 times keystroke (key code 123) end repeat key up shift key up command end tell So that code will press the left arrow 125 times while holding shift and command down. Wow, thank you Shawn! I'm going to test this first time tomorrow on my day off Link to comment
ulyssesric Posted January 28, 2015 Share Posted January 28, 2015 Now I know that I can send (hold) certain keystroke from Remote; however, it the purpose is only for executing optional actions within a workflow, there should be a better way. For example, can we have an option to "Hold modifier keys" while adding a command or workflow to the remote panel ? Alfred would then know to hold these keys while the remote action is triggered. Please consider this as a feature request. Link to comment
heldausberlin Posted January 28, 2015 Share Posted January 28, 2015 (edited) I guess great minds think the same, I wrote a remote workflow and submitted to packal (waiting to get approved) that will control the volume on your mac http://www.packal.org/workflow/remote-volume-control Great! If you like, you could change the code for the mute button with: if output muted of (get volume settings) is true then set volume without output muted else set volume with output muted end if Then you don't need two buttons for Mute/Unmute. You can tap the same button for both functions. Also you can change the background color in the same green tone. Edited January 28, 2015 by heldausberlin 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